Apple Script to copy .numbers file

I am trying to write an apple script that will read a numbers file on my computer and copy its data onto a new numbers file cell by cell. So far I have it working where the cell value and color is correctly copied over, but for some reason the formulas do not display properly.


They all say "The formula contains a syntax error" despite the formulas all visually looking correct when I inspect them.

Can anyone help resolve this issue and make the formulas actually calculate the value? My code is below.



-- Specify the file paths

set sourceFile to "/users/colepropach/Desktop/app/test.numbers"


-- Open Numbers application

tell application "Numbers"

activate

set newDocument to make new document

-- address the document reference

tell newDocument

-- remove any default tables

delete every table of every sheet

end tell

-- Open the source file

open sourceFile

-- Get the document

tell the front document

repeat with sheetIndex from 1 to count sheets

set currentSheet to sheet sheetIndex

tell newDocument

set newSheet to make new sheet

delete every table of newSheet

end tell

-- Get tables in the current sheet

repeat with tableIndex from 1 to count tables of currentSheet

set currentTable to table tableIndex of currentSheet

set rowCount to row count of currentTable

set columnCount to column count of currentTable

tell newDocument

tell newSheet

set newTable to make new table with properties {row count:rowCount, column count:columnCount}

end tell

end tell

-- Get rows in the current table

repeat with rowIndex from 1 to count rows of currentTable

set currentRow to row rowIndex of currentTable

-- Get cells in the current row

repeat with cellIndex from 1 to count cells of currentRow

set currentCell to cell cellIndex of currentRow

set cellValue to value of currentCell

set cellFormula to formula of currentCell

set cellColor to background color of currentCell

tell newDocument

tell newSheet

tell newTable

if cellFormula is not missing value then

set value of cell cellIndex of row rowIndex to cellFormula

else if cellValue is not missing value then

set value of cell cellIndex of row rowIndex to cellValue

end if

if cellColor is not missing value then

set background color of cell cellIndex of row rowIndex to cellColor

end if

end tell

end tell

end tell

end repeat

end repeat

end repeat

end repeat

-- close saving no

end tell

-- quit saving no

end tell

Posted on Jul 12, 2024 11:36 PM

Reply
Question marked as Top-ranking reply

Posted on Jul 13, 2024 6:11 AM

Cutting to the chase, I think the easiest solution is to turn off "use header names as labels".


When using header names as labels, Numbers uses the row header (row 1 in your table) and column header (column A in your table) to create the cell reference. Row 1 contains "First" and "Last" but column A does not contain "Abigail Peterson" so there are no cells that are "$First Abigail Peterson" or "$Last Abigail Peterson". Instead, you are trying to put the formula in column A. If column A did contain "Abigail Peterson" then there would be no need to concatenate the first and last names, it would already be there.


I believe the problem is the source file, which has in column A the formula to concatenate the name, then uses that concatenated name for the labels for the references to the first and last name. It sounds circular but I guess it is not. But it does have to happen in order, you cannot start using the label as a cell reference without Numbers first creating it.



Similar questions

3 replies
Question marked as Top-ranking reply

Jul 13, 2024 6:11 AM in response to Zorco_

Cutting to the chase, I think the easiest solution is to turn off "use header names as labels".


When using header names as labels, Numbers uses the row header (row 1 in your table) and column header (column A in your table) to create the cell reference. Row 1 contains "First" and "Last" but column A does not contain "Abigail Peterson" so there are no cells that are "$First Abigail Peterson" or "$Last Abigail Peterson". Instead, you are trying to put the formula in column A. If column A did contain "Abigail Peterson" then there would be no need to concatenate the first and last names, it would already be there.


I believe the problem is the source file, which has in column A the formula to concatenate the name, then uses that concatenated name for the labels for the references to the first and last name. It sounds circular but I guess it is not. But it does have to happen in order, you cannot start using the label as a cell reference without Numbers first creating it.



Jul 13, 2024 6:24 AM in response to Badunit

Said another way,


In the source file, the formula in column A was created using regular references. Once it had the result "Abigail Peterson", the cells that contain the first and last names then had both a row header and column header so Numbers changed their cell references to header names, using "Abigail Peterson" as part of the "labels". When you copy the formula to your new document, it is trying to use the labels in the formula before Numbers has created them. Numbers cannot create them without the formula working and the formula cannot work if it uses non-existing labels.

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 to copy .numbers file

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