You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

AppleScript Numbers duplicate command?

I wish to simply copy a formula in one cell and paste it into a range of cells. For instance, cell F2 contains the formula, and in essence I wish to autofill or copy the formula in F2 and paste it into the range of cells F3:F434 using the duplicate command (from Standard Suite). Tried this but it aborts the app.


(tell table X of sheet Y of Z)

duplicate cell "F2" to range "F3:F434"


Causes:

AppleScript Execution Error

Numbers got an error: Cells can not be copied. (-1717)


I'm sure a silly syntax or reference error, but can't find examples of duplicate command anywhere.

Thanks in advance.

MacBook Air 15″

Posted on Aug 29, 2024 11:21 AM

Reply
14 replies

Aug 29, 2024 1:55 PM in response to smrc8081

The complexity of automating these steps seems to exceed its usefulness.


I'd need to automate:

  1. Select table Matrix > Draft Picks
  2. Unhide header row 2
  3. Set properties Organize > Categories to OFF, Filters to OFF
  4. Copy formula in F2 and Paste into F3:F434
  5. Sort Now
  6. Re-enable Categories & Filters


Easier to just put these instructions/reminders in a display dialog and execute them manually.


However, one tip you might have that would prove highly useful would be:


I have a table in another sheet with 4 columns and 434 rows. I need to populate 168 rows during an initialization step (same script as above). I am under the impression the only way between AppleScript and Numbers is to stuff cells one at a time. I figured out a mass erase can be accomplished by merging this block of 4x434 cells and clearing the value at once, then unmerging the cells. That works.


But subsequently I am putting 4 cells of data in each row, one cell at a time. Seems would be more efficient to define a record of 4 fields (all TEXT), and put that entire record into a row at once. Would still need to iterate a row at a time but it would seem to be optimal to do 4 cells at once (1 whole row) instead of 4 cells one at a time, row after row. Or even better, define a big block of 168 rows containing 4 fields in each row, and stuffing that entire block into the Numbers table. But that seems too ambitious to be true.


Ideas?

Aug 29, 2024 12:34 PM in response to Camelot

Here is a test script in its entirety (except name of MainFile), gets same delete error. I agree, shouldn't delete anything.


set matrixSheet to "Matrix"

set trackerTable to "Draft Picks"

set FormulaCell to "F2"

set trkRegion to "F3:F434"


tell application "Numbers"

set myDoc to open alias MainFile

tell table trackerTable of sheet matrixSheet of myDoc

> set value of every cell of range trkRegion to (get formula of cell FormulaCell)

end tell -- Tracker

end tell -- Numbers


Aug 31, 2024 7:33 PM in response to smrc8081

smrc8081 wrote:

I am under the impression the only way between AppleScript and Numbers is to stuff cells one at a time.


Yes, natively, AppleScript is inefficient at setting values of a large number of cells in Numbers (unlike, I believe, in Excel).


However, GUI-scripting can do the job nicely.


Try this script, which copies and pastes through the clipboard:



copyIt("Sheet 1", "Table 1", "A2:D6")
pasteIt("Sheet 1", "Table 2", "A2:D6")


to copyIt(shtName, tblName, rngName)
	tell application "Numbers"
		tell document 1
			tell table tblName of sheet shtName
				set selection range to range rngName
				tell application "Numbers" to activate
				tell application "System Events" to keystroke "c" using command down
			end tell
		end tell
	end tell
end copyIt

to pasteIt(shtName, tblName, rngName)
	tell application "Numbers"
		delay 0.1 -- may need to vary this
		tell document 1
			tell table tblName of sheet shtName
				set selection range to range rngName
				tell application "Numbers" to activate
				tell application "System Events" to keystroke "v" using command down
			end tell
		end tell
	end tell
end pasteIt



I tested it on tables on Sheet 1 that look like these:




Both values and formulas copy as expected.


Just change the references within copyIt and pasteIt to suit your needs. For example to copy a formula from F2 to F3:F434 you would do something like this:


copyIt("Sheet 1", "Table 1", "F2")

pasteIt("Sheet 1", "Table 1", "F3:F434")


(don't modify the rest of the script when you change the ranges, etc. in the first two lines)


To allow the paste you may need to go to System Settings > Privacy & Security > Accessibility and add the application you are using to the list of applications allowed to control your computer. I added Script Editor because I ran the script from there. You may need to add Script Debugger, or whatever you are using.


SG


Aug 29, 2024 11:45 AM in response to smrc8081

duplicate is a standard command that (should) exist in every AppleScriptable application. However, the actual implementation is highly application-specific.


In the case of Numbers, a cell has many parameters that don't necessarily make sense when you say duplicate. Some things can be overlooked (e.g. row and column numbers), but what about internal links and named cells (if some other cell references cell F2 and you somehow 'duplicated' F2 to F3, should the inbound references follow, too?).


For that reason, cells can not be duplicated in this way.

In this case you need to be more specific about which properties of the cell you want to replicate.


Since it sounds like you want to replicate the formula of cell F2 to the other cells, you can do that via:


tell application "Numbers"
	tell table 1 of sheet 1 of document 1
		set value of every cell of range "F3:F6" to (get formula of cell "F2")
	end tell
end tell


Aug 29, 2024 4:07 PM in response to smrc8081

I figured it had to be something specific to the sheet - a filtered table would have that kind of problem.


I'm not sure I understand the statement:


> now it put the formula as literal text into the cells


Are you saying the target cells have a specific value, not a formula?


That's not what I see on my system, indicating something else amiss. What formula is in F2?


AppleScript Numbers duplicate command?

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