Hi everybody,
I am currently working on a script, which will help our staff to automate tasks in a spreadsheet app (RagTime). Now I need a function for my script, which will return the column name for a given position. Here is an example, just to give you an idea about my requirements:
Position: 5 → Column name: “E”
Position: 28 → Column name: “AB”
Position 127 → Column name: “FL”
Well, I already started to code such a function, but unfortunately when I enter multiples of 26 I am running into problems. For example, the script returns the column name “A” for 26 and “B” for 52…
my get_column_name(125)
on get_column_name(position)
set alphabet to {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}
set column_name to ""
set x to position
repeat
set xdiv to x div 26
set xmod to x mod 26
log xdiv & " | " & xmod
if xmod > 0 then
set column_name to (item xmod of alphabet) & column_name
end if
if x > 0 then
set x to xdiv
else if x = 0 then
exit repeat
end if
end repeat
return column_name
end get_column_name
I am really desperate, as I am too tired to solve this problem. Therefore I am asking for help on this forum If anybody could give me a hint how to calculate column names, that would be just great!
And I need a solution made with AppleScript, because I cannot use the ‘do shell script’-command in my project. Otherwise I would just use this piece of Perl code:
my $nr = 5; # fill in position here
my $t = A;
++$t for (1…$nr-1);
print “Position: $nr → Column name: $t\n”;
Thanks so much for your help in advance, I am really looking forward to your answers.
Martin Michel
Model: PowerBook 17" 1.33 GHz 1024 MB RAM
AppleScript: 1.10.7
Browser: Safari 419.3
Operating System: Mac OS X (10.4)