Apple Script - Copy and Paste Between Two Tables in Numbers

Hi,


I'm reasonably new to apple scripts, but I can really see it's advantages.


I have a large number of numbers files, each containing a data table. I'd like to analyse these files using my own table (as shown in the photo below).


So far I've managed to use automator and apple script to copy my analysis table into the numbers documents, save the document etc. But I just can't figure out how to copy data between the two tables.


Eg. I'd like to copy data from cell F87 (Table 1), into cell D2 (Table 2). Photo's below!

There are a few other things I'd like to copy and paste, but if someone could please show me the code I will be able to adapt it for the other pieces of information.


Thanks in advance!

MacBook Air 13", 10.14

Posted on Feb 1, 2019 4:41 AM

Reply
Question marked as Top-ranking reply

Posted on Feb 1, 2019 4:53 AM

No promises (this was written for Numbers 2.3), but see if this works:


tell application "Numbers"
	activate
	tell document 1
		tell sheet 1
			tell table 1
				set selection range to range "F87"
			end tell
		end tell
	end tell
end tell

copyit()

tell application "Numbers"
	activate
	tell document 1
		tell sheet 1
			tell table 2
				set selection range to range "D2"
			end tell
		end tell
	end tell
end tell

pasteit()

on copyit()
	tell application "System Events"
		tell application "Numbers" to activate
		keystroke "c" using {command down}
	end tell
end copyit

on pasteit()
	tell application "System Events"
		tell application "Numbers" to activate
		keystroke "v" using {command down}
	end tell
end pasteit


The use of the 'handlers' (copyit, pasteit) means that you can do other copy/pastes in the same script without having to keep typing out the code.


Similar questions

6 replies
Question marked as Top-ranking reply

Feb 1, 2019 4:53 AM in response to Apple_Script

No promises (this was written for Numbers 2.3), but see if this works:


tell application "Numbers"
	activate
	tell document 1
		tell sheet 1
			tell table 1
				set selection range to range "F87"
			end tell
		end tell
	end tell
end tell

copyit()

tell application "Numbers"
	activate
	tell document 1
		tell sheet 1
			tell table 2
				set selection range to range "D2"
			end tell
		end tell
	end tell
end tell

pasteit()

on copyit()
	tell application "System Events"
		tell application "Numbers" to activate
		keystroke "c" using {command down}
	end tell
end copyit

on pasteit()
	tell application "System Events"
		tell application "Numbers" to activate
		keystroke "v" using {command down}
	end tell
end pasteit


The use of the 'handlers' (copyit, pasteit) means that you can do other copy/pastes in the same script without having to keep typing out the code.


Feb 1, 2019 7:21 AM in response to Apple_Script

Tested in Numbers 5.3 (won't work in versions of Numbers 10 years old):


tell application "Numbers"
	tell front document
		tell active sheet
			set table "Table 2"'s cell "D2"'s value to table "Table 1"'s cell "F87"'s value
		end tell
	end tell
end tell



Assuming both tables are in the active sheet of the frontmost open document then that's all there is to it!


If you need to address a specific open document that may not be the frontmost one then you would:


tell document "MyDocumentName.numbers"


I've found you generally need the .numbers extension in the name.


If you need to address a specific sheet that may not be the active one then you would:


tell sheet "MySheetName"


If you are copying values of a large number of cells into a new table, then this will be a little slow. If you are often doing that just post and can give an example of bulk transfer using tab-delimited values pasted in from the clipboard.


Lot's of good stuff on scripting Numbers at this site.


SG


Feb 1, 2019 10:11 AM in response to Apple_Script

Just following on from this.. Currently I have the code:


tell application "Numbers"

tell front document

tell active sheet

set table "Table 2"'s cell "D2"'s value to table "Table 1"'s cell "F87"'s value

end tell

end tell

end tell


Lets say that Cell F87 was blank, but data was in E87. I tried writing a code as follows, but it didn't seem to work - anyone got any ideas? I've put my adjustments in Bold.


tell application "Numbers"

tell front document

tell active sheet

if "Table 1"'s cell "F87" is not "" then

set table "Table 2"'s cell "D2"'s value to table "Table 1"'s cell "F87"'s value

else

set table "Table 2"'s cell "D2"'s value to table "Table 1"'s cell "E87"'s value

end if

end tell

end tell

end tell


Hope I'm not asking too many questions regarding apple scripts! I'm just really enjoying using it and learning about how it works.


All the best

Feb 1, 2019 6:20 PM in response to Apple_Script

You can do this:


tell application "Numbers"
	tell front document
		tell active sheet
			if table "Table 1"'s cell "F87"'s value is not missing value then
				set table "Table 2"'s cell "D2"'s value to table "Table 1"'s cell "F87"'s value
			else
				set table "Table 2"'s cell "D2"'s value to table "Table 1"'s cell "E87"'s value
			end if
		end tell
	end tell
end tell



Unlike in formulas where you use '' in AppleScript you need to use missing value.


Also, you forgot the value after:


 if "Table 1"'s cell "F87"


It needs to be:


 if table "Table 1"'s cell "F87"'s value


Keep up the scripting. Used properly, it can save time working in Numbers.


SG

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Apple Script - Copy and Paste Between Two Tables in Numbers

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.