In Microsoft Excel 2004 for Mac, there are some AppleScript issues with columns and rows. The following examples all produce something that is useful and functional, namely a range. The result is never referred to as a column or row, always as a range.
row 5 of (get used range of active sheet) column 7 of range "A1:P26" of active sheet range "G1:G26" of range "A1:P26" of active sheet
However, referring to it as a column, rather than as a range produces no result.
column "G1:G26" of range "A1:P26" of active sheet
So you can refer to columns and rows by index (number), never by address.
Perhaps this will not be unexpected coming from VBA, where you typically refer to
Columns(7)
and
Rows(5). However, it doesn't correspond with what you expect from the dictionary, where column, row, and cell are all subclasses of range and inherit all its properties. That means you ought to be able to do the same with column, row, and cell as you do for range, but you can't. For most purposes, stick to referring to columns and rows by index, and use range when using an address.
Also, you cannot get anything for:
rows of (get used range of active sheet) columns of range "A1:P26" of active sheet
You get a sort of fake single object — a single row or column of no dimensions, rather than a list of items as expected. You cannot then get any individual items from what ought to be a list. Your script errors if you try.
It also means that you cannot do
repeat
loops of this sort:
set theRows to rows of (get used range of active sheet) repeat with theRow in theRows set v to value of theRow end repeat
The script just "jumps" over this as if it's not there (try it in a line-by-line debugger editor and you'll see it happen), and then errors as soon as anything tries to use a result or variable set to anything in the loop.
However, you can get there another way since count (every row) or count rows (the same thing) works. You can't get rows, but you can get (count rows). So you can iterate through a
repeat
loop getting
row i, where
i
is looping from
1 to (count rows).
More unexpectedly, you cannot set a variable to a row or column along the way because once again you get no result. The reference to a row or column seems to lose its connection to the real thing whenever it is evaluated to a variable. The only thing that works is a pure application reference without variables, drilling through each element along the way.
So you must do it this way:
tell (get used range of active sheet) repeat with i from 1 to count rows set v to value of row i end repeat end tell
Wait until you've got the final result that you need before setting a variable to that (the value property in this example), never to the row, column, or cell along the way. It's perfectly all right to set a variable to something referred to as a range, however. This is true whether it's used range or a range by address (range "A1:A46"), including ranges that actually are rows or columns, but not to anything that was defined as, for example,
row 5
or
column 8.
So this is also okay, where the variable
ur
is set to a range:
set ur to (get used range of active sheet) tell ur repeat with i from 1 to count rows set v to value of row i end repeat end tell
But it doesn't work if the variable
theRow
is set to a row:
set ur to (get used range of active sheet) tell ur repeat with i from 1 to count rows set theRow to row i set v to value of theRow end repeat end tell
These "rules," or behaviors, are hard to discover. So do keep this description on hand until you're used to it.
Also, commands will not work on
every
column of someRange
or
rows of
someRange, because those do not resolve to AppleScript lists as they usually do. They are those objects of no consequence mentioned above. So you really do need to use the only dependable technique for accessing single items one at a time in a
repeat
loop with no row or column variables, as illustrated above.
Perhaps VBA collection objects are the reason for this behavior. The fact that you can count rows and columns and refer to them by index — two Methods available to Rows Collection and Columns Collection objects in VBA — but not by address sounds familiar to VBA scripters. As does the fact that you can't resolve the plurals to lists of individual items.
Cells have similar problems if you try to use them via
every cell of someRange. You can set a variable to a cell, though, because it is returned as a range. Use range as much as you can.


