iWork Numbers: Cannot Add Rows

I’m struggling with a seemingly simple iWork Numbers scripting problem. I have a custom numbers template which has a sheet containing a table with a single column of sample values. I want to add additional rows to this table:

tell application "Numbers"
	set diodeDocument to make new document with properties {document template:template "diode-template"}
	
	tell diodeDocument
		tell table "Samples" of sheet "Data"
			repeat 10 times
				add row below first row -- fails after 4th time
			end repeat
		end tell
	end tell
end tell

The problem is that the add row below command is failing after the 4th iteration. Does anyone know what’s happening here.

diode-template.zip (485.9 KB)

I’ve found a workaround: add row above last row works correctly.

It’s because in your template the first row is a title row.

Title rows can only be a maximum of 5:

:wink:

Maybe this example could be useful to some novice user passing by here.

tell application "Numbers"
	activate
	
	set diodeDocument to make new document with properties {document template:template "diode-template"}
	
	tell diodeDocument
		tell table "Samples" of sheet "Data"
			-- add a temporary row
			set theRow to add row above row 2
			-- add 10 rows in order
			repeat with iTime from 1 to 10
				set theRow to (add row below theRow)
				set value of cell 1 of theRow to "test" & iTime
			end repeat
			-- delete the temporary row
			delete row 2
			-- select the last created row
			set selection range to row before theRow
		end tell
	end tell
end tell
2 Likes