Moving from Microsoft Office VBA to AppleScript:
MacTech's Guide to Making the Transition
Introduction
|
Table of Contents
Page Prev and Page Next buttons at bottom of the page.
|
April, 2007
Page 61
The thought of stuffing that complex and long-winded (in AppleScript) evaluation of the row number right into the repeat loop statement, as VBA does with its shorter terms and dot syntax, is too much to bear contemplation. It's better to work it out as a variable. Recalling the problems we found with columns and rows (check the Alert in Working with Columns and Rows), it is essential to use the application terms directly without variables for a row, but it always works to set a variable to a range or, as here, to a number (or string, date or other basic type).
With long trails of dots in VBA, it is natural when translating into AppleScript to expect to start at the end and work backwards from there, except that you actually need to start off with the "front" VBA object in order to know where to look for the properties that follow. Don't forget that whereas in VBA a Method name might occur somewhere along the way in the same dot syntax and parent-child relation as for a Property, in AppleScript the equivalent command does not use the same 'of' syntax as a property or element, and has to be sought in the Commands section of the appropriate dictionary Suite.
What in VBA is a Property might be an element, or a property of an element, or even a command, in AppleScript. So it can take a bit of time to find everything and work it all out.
We recall that the Cells(r, c) syntax translates to
cell c of row r
in AppleScript, and the result is a range. So we look in range class in the Table Suite for something analogous to the End property – it might be end of range or something like that. But there's nothing at all like that to be seen in range class.
So we should look now in the Commands section of the Table Suite for something that might get the end of a range. There's nothing listed under "end of" there. So check the "get" neighborhood – always a good idea. When they need to come up with a proprietary command when a property won't work, the developers often start the command term with "get".
And there it is – get end. It takes a range as direct object or parameter ("direct" because no specific parameter keyword is needed) and its direction parameter takes an enumeration that is just what we are looking for, specifying toward the top for VBA's xlTop. (In this case it really means "last one, or end, starting at the top", i.e., oriented from the top's point of view. We're actually looking for the bottom row, of course.)
We know that although we can't get rows or every row of a range or sheet, we can, by some strange magic, get count rows (or count every row). (Check Working with Columns and Rows again.) Finally the Row Property of Range (it's a good thing, and to be expected, that AppleScript does not call this property "row": it would conflict with the class row) is evidently first row index property in AppleScript – a nice descriptive term that returns an integer, just what we are looking for.
So now we have everything needed to construct the statement that should return the row number of a cell:
set lastRowNum to first row index of ¬
(get end (cell 1 of row (count rows)) direction toward the top)
The one remaining thing would be to check whether AppleScript lets you use get end on the sheet itself (the target of the tell block) without producing 65535 as the last row of the sheet instead of the last used row, or would require us to explicitly target used range of the sheet instead, But no, AppleScript allows us to take the same shortcut (or coercion) as Sheet.End does in VBA: we get the last used row number, not 65535.
The rest of the script is easy, using cell c of row i a few more times, and getting the value property of each range (cell). delete entire row works just as it should, as long as you remember to do your repeat loop backwards "by -1" equivalent to VBA's Step -1.
A more efficient way to do this is to collect all the duplicates in a range variable and delete them all at the same time. Note that the Union method in VBA requires that both Range objects are set, and the same will be true in AppleScript with the union command.
Dim rCell As Range
Dim rDelete As Range
With ActiveSheet
For Each rCell In .Range("A2:A" & _
.Range("A" & .Rows.Count).End(xlUp).Row)
If rCell.Value = rCell.Offset(-1, 0).Value Then
If rDelete Is Nothing Then
Set rDelete = rCell
Else
Set rDelete = Application.Union(rDelete, rCell)
End If
End If
Next rCell
If Not rDelete Is Nothing Then _
rDelete.EntireRow.Delete
End With
< Previous Page Next Page>
- SPREAD THE WORD:
- Slashdot
- Digg
- Del.icio.us
- Newsvine