MacMost Q&A Forum • View All Forum QuestionsAsk a Question

How Do I Set the Format Of a Numbers Cell Using Javascript?

Gary, I”m trying to do the same thing the following applescript does only using javascript. I’ve tried hard and I think come close but just don’t seem to be able to come up with the right javascript code to do it. I can get the format using .format() at the end of a line that identifies the cell but have had no success in changing the format. I think I’ve encountered almost every error code that Script Editor has to offer so listing the error codes is probably not helpful.

use AppleScript version “2.4” — Yosemite (10.10) or later
use scripting additions

tell application “Numbers”
tell table 1 of sheet 1 of document 1
set selection range to range “J29:J29”
set format of selection range to currency
end tell
end tell

mac OS High Sierra
version 10.13.3
iMac (21.5-inch, Mid 2011)

Dana Stevens

Comments: 3 Responses to “How Do I Set the Format Of a Numbers Cell Using Javascript?”

    2 years ago

    I played around for a few minutes and it seems pretty straight-forward. Something like this:

    var Numbers = Application("Numbers");
    var table = Numbers.documents[0].sheets[0].tables[0];
    var r = table.ranges['A1:B2'];
    r.format = "currency";

    But I’m using Catalina with the latest version of Numbers. Not sure if it will work for you back in High Sierra.

    Dana Stevens
    2 years ago

    Thank you. The reply was very helpful and shows that I was on the right track but apparently my old iMac (or software) can’t cut it, or I have something set preventing it from doing the job. I received “Script Error: Error on line 4: Error: Invalid cell format.” and “Result: Error -10000: AppleEvent handler failed.” However, I was able to get and set other properties. Cell properties listed as “inherited from range” in the dictionary work fine, others do not. I can work as is.

    Dana Stevens
    1 year ago

    Just in case it is useful to someone. I had the same error experience trying to set the format property of a range of cells in a Numbers spreadsheet using Javascript on my wife’s MacBook Pro (13-inch, Mid 2012) running 10.14.5 Mojave and using Numbers 6.1. Someday I hope to have an up to date system to use but it could take awhile before that happens.

Comments Closed.