Apple Numbers - setting a range for all but the last 3 rows of a column

I would like to set a selected range of cells in a Apple Numbers sheet, where I always want the range to extend down to all but the last 3 rows. The issue is I do not know the number of rows in any sheet. I have seen how to select ranges at AppleScript and Numbers: Defining and Selecting Ranges, but it does not help with my issue.

I have got part of the way in doing a script but I just cannot find the syntax to get the AppleScript to work. Here is where I have got up to:

tell application "Numbers"
       tell first table of the active sheet of the front document
              set countofcells to count of cells of column 2
		
		set countofcells to countofcells - 3
		
		set thisRangeName to ¬
			((name of the second cell of column "B") & ":" & ¬
				((name of the 51st cell of column "B")))
		set the selection range to range thisRangeName
		
	end tell
end tell

The part of the script I am having the problem is the line

“((name of the 51st cell of column “B”)))”

as not every sheet will the last row which needs selecting in Column B be the 51st cell.

Any help would be gratefullly received.

This script will select the top seven cells of column B in a ten-row table.

tell application "Numbers"
	set d1 to document 1
	set s1 to sheet 1 of d1
	set t1 to table 1 of s1
	
	set kr to (row count of t1) - 3
	--> 7
	
	set rn to "B2:B" & kr
	--> "B2:B7"
	
	set selection range of t1 to range rn of t1
	
	-- for confirmation…	
	selection range of t1
	--> range "B2:B7" of table 1 of sheet 1 of document id "6BB77932-443C-46D5-824E-E9C8261932CC"
end tell

You can get the number of rows in a table using the row count command (or columns with column count).

To make the name approach work (as in your example), try this:

	set thisRangeName to ¬
		(name of the second cell of column "B") & ":" & ¬
		(name of cell countofcells of column "B")
		--> B2:B7
1 Like

A minor alternative, of course, would be to use negative indexing. Cell -4 of the column is the one above the last three:

tell application "Numbers"
	tell first table of the active sheet of the front document
		set thisRangeName to ¬
			("B2:" & (name of the -4th cell of column "B"))
		set the selection range to range thisRangeName
	end tell
end tell