Numbers.app: Effective way to Remove Empty Rows.

Hello,

The following simple script removes empty rows in Numbers.app. It works, but I would like to know if there is any way to improve its speed?


tell application "Numbers" to tell document 1
	tell active sheet to tell table 1
		repeat with i from (count rows) to 1 by -1
			set isEmpty to true
			repeat with aCell in (cells of row i)
				if not (value of aCell) is missing value then
					set isEmpty to false
					exit repeat
				end if
			end repeat
			if isEmpty then remove row i
		end repeat
	end tell
end tell

Maybe, something like the following snippet? This should be much more efficient if the table has hundreds or thousands of rows:


set replicatedList to {}

tell application "Numbers" to tell document 1
	tell active sheet to tell table 1
		repeat with i from 1 to (count cells of row 1)
			set end of replicatedList to missing value
		end repeat
		repeat with i from (count rows) to 1 by -1
			if (value of cells of row i) is replicatedList then remove row i
		end repeat
	end tell
end tell

Hi KniazidisR.

The first thing I’d try is to get the cells’ contents all at once, as a list of lists, and work through the AppleScript lists rather than the individual cells in the table. There may be other things you can do too, but this would be a start.


tell application "Numbers"
	set activeTable to table 1 of active sheet of document 1
	set rowCellValues to value of cells of rows of activeTable
end tell
repeat with i from (count rowCellValues) to 2 by -1
	set isEmpty to true
	repeat with thisValue in item i of rowCellValues
		if (thisValue's contents is not missing value) then
			set isEmpty to false
			exit repeat
		end if
	end repeat
	if (isEmpty) then tell application "Numbers" to remove row i of activeTable
end repeat

Thanks, Nigel.

It is much more efficient indeed. As I see it, nested repeat loops inside a tell-block application are a very bad combination. It seems, now I know what the most efficient script should be to remove empty rows in a Numbers table:


tell application "Numbers"
	set activeTable to table 1 of active sheet of document 1
	set rowCellValues to value of cells of rows of activeTable
end tell

set replicatedList to {}
repeat with i from 1 to (count items of item 1 of rowCellValues)
	set end of replicatedList to missing value
end repeat

repeat with i from (count rowCellValues) to 2 by -1
	if (item i of rowCellValues) is replicatedList then tell application "Numbers" to remove row i of activeTable
end repeat