Numbers Column or Row Addition/Paste

Hi folks.

I’m doing something in Ruby, then I can pass some parameters into an Applescript which will paste into Numbers.

So here are my questions:

  1. Can I say "set focus in the first empty row in “thisTable”?

  2. How can I paste a full row or column? Tab-delimited items, can they be handled as a full string pasted as such, so that it spans a row’s cells?

Right now it looks like I have to grab cell IDs and iterate through values and cells for a set.

Any insight appreciated.

Cheers

You may try :

tell application "Numbers"
	tell document 1
		tell sheet 1
			tell table "2012"
				set targetRow to 51
				set selection range to range ("A" & targetRow & ":A" & targetRow)
				my pasteAtSelection("01/02/2016	azerty		79,99	12345,67	55	01/02/2016		79,99	12345,67	12345,67	01/02/2016")
			end tell
		end tell
	end tell
end tell


on pasteAtSelection(theString)
	set the clipboard to theString
	tell application "System Events"
		tell process "Numbers"
			set frontmost to true
			keystroke "v" using {command down, option down, shift down}
		end tell
	end tell
end pasteAtSelection

CAUTION : as I am a french user, the string pasted above use french settings.
For an user from the USA I guess that it would be better with :
“02/01/2016 azerty 79.99 12345.67 55 02/01/2016 79.99 12345.67 12345.67 02/01/2016”

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) jeudi 25 octobre 2018 17:17:55

Je suis de Canada. Heh. Alors…

Ya your manual setting of the last row/column is what I wanted to get past. It’s not a big problem as going manual can do this. I’m just curious.

Cheers

If you want to insert the values in a row just above a bottom header row you may use:

tell application "Numbers"
	tell document 1
		tell sheet 1
			tell table "2012"
				set bottomRow to (count row) - 1
				# I assume that there is a bottom header at bottom of the table
				add row below row bottomRow # create a standard new row above the existing header
				# Select the first cell of the added row
				set selection range to range ("A" & (bottomRow + 1) & ":A" & (bottomRow + 1))
				my pasteAtSelection("01/02/2016	azerty		79,99	12345,67	55	02/01/2016		79,99	12345,67	12345,67	02/01/2016")
			end tell
		end tell
	end tell
end tell


on pasteAtSelection(theString)
	set the clipboard to theString
	tell application "System Events"
		tell process "Numbers"
			set frontmost to true
			keystroke "v" using {command down}
		end tell
	end tell
end pasteAtSelection

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) jeudi 25 octobre 2018 19:02:56

Niiiice! :wink:

“set selection range” is throwing an error on Numbers 3.6.2 (all I can upgrade to). Any ideas?

OK, some more questions: How can I sniff out the first empty row or column? The table is larger than the current data.

I’m a bit surprised because I don’t remember a date when “selection range” wasn’t available.
I can’t check because I don’t have version 3.6.2 for years.
I have version 4.3.1 and version 2.3. Both of them contain “selection range”.

Below is a code which scan the cell 1 of rows starting from the lower standard row.
It was tested with Numbers 5.2

tell application "Numbers"
	tell document 1
		tell sheet 1
			tell table 1
				set footers to my getCountOfFooters()
				set bottomRow to (count rows) - footers
				repeat with r from bottomRow to 1 by -1
					# Edit the test if you must check that several cells aren't empty
					if value of cell 1 of row r is not missing value then
						exit repeat
					end if
				end repeat
				# Here r is the index of the lower cell wich is not empty in column 1 of a standard row.
				set targetRow to r + 1
				# Select the first cell of the added row
				set selection range to range ("A" & targetRow & ":A" & targetRow)
				my pasteAtSelection("01/02/2016	azerty		79,99	12345,67	55	02/01/2016		79,99	12345,67	12345,67	02/01/2016")
			end tell
		end tell
	end tell
end tell


on pasteAtSelection(theString)
	set the clipboard to theString
	tell application "System Events"
		tell process "Numbers"
			set frontmost to true
			keystroke "v" using {command down}
		end tell
	end tell
end pasteAtSelection

on getCountOfFooters()
	tell application "System Events" to tell process "Numbers"
		set frontmost to true
		tell menu bar 1 to tell menu bar item 6 to tell menu 1 to tell menu item 14 to tell menu 1
			repeat with ms from 1 to 6
				if value of attribute "AXMenuItemMarkChar" of menu item ms = "✓" then
					set footers to ms - 1
					exit repeat
				end if
			end repeat
			
		end tell
	end tell
	return footers
end getCountOfFooters

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) jeudi 25 octobre 2018 21:29:38

OK, looks like I’m counting rows of the table and asking for lastRow+1, which doesn’t exist. That’s why I was looking for the last blank one.

In any case, thanks for the input. This seems easier manual or inside Postgresql. Heh.

Cheers

I searched in old HD. I retrieved a Numbers 3.6.2.
The function “selection range” is available in it.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) vendredi 26 octobre 2018 11:37:01

Ya I’m in El Capitan.

Does that actually SELECT the range of cells? I got some cells to fill, I was just messing around at that point.

Numbers 3.6.2 behaves the same way under El Capitan and under High Sierra.

I am accustomed to pass a full range descriptor like “A2:C2” when I call set selection range so when I want to select a single cell I use the same syntax :
set selection range to range “A10:A10”
but we may use a shorter instruction:
set selection range to range “A10”
Both select the cell “A10”
The very first one select the cells “A2”, “B2”, “C2”.

When we want to paste a row, there is no need to select the entire row, select the first cell of the row is sufficient.

Maybe I have find the explanation of what you got.
I defined a name of table which doesn’t exist in the document.
For instance,
tell table “missingTable”
set selection range to range “A10”

display :
tell application “Numbers”
set selection range of table “missingTable” of sheet 1 of document 1 to range “E10” of table “missingTable” of sheet 1 of document 1
→ error number -1728 from table “missingTable” of sheet 1 of document 1

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) vendredi 26 octobre 2018 16:02:06