Setting the value of a Numbers cell using AppleScript

applescriptnumbers

I'm trying to write a numbers script (to do this, as it happens), in which I need to change the value of a cell.

However, if I do something like

tell application "Numbers"
    activate
    tell document 1
        tell sheet 3
            set the value of cell "E1" to 1000
        end tell
    end tell
end tell

then I get the error

 error "Numbers got an error: Can’t set cell \"E1\" of sheet 3 of document 1 to 1000." number -10006 from cell "E1" of sheet 3 of document 1

but this contains no information about why it can't set that cell. The document, the sheet and the cell all exist.

I guess this might depend on my particular spreadsheet, but is this the correct way to set the value of a cell, and if so, what else can I do to understand why it's not working?

Best Answer

I'm in front of a Mac again and gave your code a go and also got the same error, so it's probably somewhat misleading in terms of what it actually means.

However, I did play around with your script a bit and was able to get the code to work by using the following code:

tell application "Numbers"
  tell table 1 of sheet 3 of document 1
    set the value of cell 1 of column "E" to 1000
  end tell
end tell

However, now that I've got this to work, I just wanted to flag you seem to be hard coding the cell etc. That is, if the position changes then the script isn't going to be dynamic. Whether this is an issue for you or not will depend on what you're trying to do and how your Numbers spreadsheet is designed.