I have a routine that repositions columns on an Excel worksheet. My problem is that the required order of the list can change and I cannot work out how to create the list so it can be processed.
The repeat loop is designed to create the list, if I leave that out the code works of course
The routine at present is:
set HeadOrg to {"symbol", "average_days_to_recovery", "payout_next_ex_date", "short_name", "dividend_annual_current", "dividend_policy_status", "dars_overall", "last_price", "dividend_yield_current"}
set LocOrder to {"," & "1" & "," & "4" & "," & "7" & "," & "2" & "," & "3" & "," & "6" & "," & "5" & "," & "9" & "," & "8"}
log LocOrder
set Orderfinal to {"symbol 1", "short_name 4", "dars_overall 7", "average_days_to_recovery 2", ¬
"payout_next_ex_date 3", "dividend_policy_status 6", "dividend_annual_current 5", "dividend_yield_current 9", "last_price 8"}
set LocOrder to {}
repeat with cnt from 1 to count of HeadOrg
set Head to item cnt of HeadOrg
--log Head
repeat with Cnt2 from 1 to count of HeadOrg
--log cnt & " " & Head & " " & word 1 of item Cnt2 of Orderfinal
--log Head is word 1 of item Cnt2 of Orderfinal
if word 1 of item Cnt2 of Orderfinal is Head then
--log cnt & " " & "\"" & Cnt2 & "\"" & " " & Head
if cnt is 1 then
--set end of LocOrder to "," & "\"" & Cnt2 & "\"" & ","
set end of LocOrder to "," & "\"" & Cnt2 & "\""
--log LocOrder & " " & Head
else
if cnt is not 9 then
--set end of LocOrder to "\"" & Cnt2 & "\"" & ","
set end of LocOrder to "\"" & Cnt2 & "\""
--log LocOrder & " " & Head
--end if
else
--set end of LocOrder to "," & "\"" & Cnt2 & "\"" & ","
set end of LocOrder to "\"" & Cnt2 & "\""
end if
end if
exit repeat
end if
end repeat
end repeat
set LocOrder to LocOrder
log LocOrder as list
tell application "Microsoft Excel"
set usedRngAddr to get address of used range of active sheet
set fml to "=CHOOSECOLS(" & usedRngAddr & LocOrder & ")"
set newRng to evaluate name fml
make new worksheet at after last sheet of active workbook
set value of range usedRngAddr to newRng
end tell
This does not work as the original LocOrder has quotes around each column number the script above also has quotes. I have left in the lines without quotes.
The two lines below are log results for the defined variables and the result of the above code.
(,1,4,7,2,3,6,5,9,8)
(,“1”, “4”, “5”, “2”, “7”, “6”, “3”, “9”, “8”)
So I assume its something to do with the way I am creating the list.