Numbers Applescripting copy/paste formulas and colouring

Hi All,



I am trying to copy the formula from cell "A1" in "table 1", and pasting it into all cells of "column A" of "table 2", but can't seem to figure it out.


Also I want to change the background cell color, when pasting, but cannot figure out how to do that.


Can anybody help?


Best...Jan

Posted on Jan 3, 2022 10:58 PM

Reply
Question marked as Top-ranking reply

Posted on Jan 4, 2022 7:13 AM

If you want relative references in the pasted formula (formula in row 1 referring to other cells on row 1, formula in row 2 referring to other cells on row 2, etc) then you can try something like this. You need to change "Table 1", "My Sheet Name", "Table 2" to match your setup. Also change the background color as needed from "red" to "yellow" or whatever.


tell front document of application "Numbers"
	set sourceTable to table "Table 1" of sheet "My Sheet Name"
	set destinationTable to table "Table 2" of sheet "My Sheet Name"
	set selection range of sourceTable to range "A1" of sourceTable
	tell application "Numbers" to activate
	delay 0.4
	tell application "System Events" to keystroke "c" using {command down}
	set rowCount to row count of destinationTable
	set targetRange to range ("A1:" & "A" & rowCount) of destinationTable
	set selection range of destinationTable to targetRange
	tell application "Numbers" to activate
	delay 0.4
	tell application "System Events" to keystroke "v" using {command down}
	set background color of targetRange to "red"
end tell


SG

5 replies
Question marked as Top-ranking reply

Jan 4, 2022 7:13 AM in response to jklarsen

If you want relative references in the pasted formula (formula in row 1 referring to other cells on row 1, formula in row 2 referring to other cells on row 2, etc) then you can try something like this. You need to change "Table 1", "My Sheet Name", "Table 2" to match your setup. Also change the background color as needed from "red" to "yellow" or whatever.


tell front document of application "Numbers"
	set sourceTable to table "Table 1" of sheet "My Sheet Name"
	set destinationTable to table "Table 2" of sheet "My Sheet Name"
	set selection range of sourceTable to range "A1" of sourceTable
	tell application "Numbers" to activate
	delay 0.4
	tell application "System Events" to keystroke "c" using {command down}
	set rowCount to row count of destinationTable
	set targetRange to range ("A1:" & "A" & rowCount) of destinationTable
	set selection range of destinationTable to targetRange
	tell application "Numbers" to activate
	delay 0.4
	tell application "System Events" to keystroke "v" using {command down}
	set background color of targetRange to "red"
end tell


SG

Jan 4, 2022 12:16 AM in response to jklarsen

Hi Jan,


Try this:


Select cell A1 of Table 1.

Set its color fill to the colour you want in all cells of column A of Table 2.

With the cell still selected, press command-C to copy the formula and the format of the cell.


Go to Table 2.

Click on Cell A1 of Table 2 t select it.

With that cell selected, scroll to the bottom of Table 2


At the bottom of Table 2, press and hold the shift key, and click the last cell in column A to add it and all cells between it and A1 to the selection.


Press command-V to paste the formula and cell format into the selected cells.


Return to Table 1.

Select cell A1.

Reset the color fill of this cell to the colour is was originally (or to No Fill).


Regards,

Barry

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.

Numbers Applescripting copy/paste formulas and colouring

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