How to render a range in Numbers (iWork) using JXA

I am using JXA to automate the process using the Numbers app. I need to select a range of cells to apply the width, but JXA won't let me get them.

According to apple documentation I only need to use make or push the created object inside the array, but everything works. This is my code and Automator error:

Option 1:

var Numbers = Application('Numbers');
Numbers.Range({name: 'A2:A20'}).make();

// -> Error: Can't make or move that element into that container

      

Option 2:

var Numbers = Application('Numbers');
var myRange = Numbers.Range({name: 'A2:A20'});
Numbers.documents[0].sheets[0].tables[0].ranges.push(myRange);

// -> Error: Can't create object. 

      

Option 3:

var Numbers = Application('Numbers');
var myRange = Numbers.Range({name: 'A2:A20'});
Numbers.documents[0].sheets[0].tables[0].selectionRange = myRange;

// -> Automator close with an unexpected error

      

According to AppleScript documentation (syntax is very different from Javascript), I can assign text to represent the range:

set selection range of table 1 to range "H5:K8"

      

But if I do something like this with Javascript, it doesn't work:

Option 4:

var Numbers = Application('Numbers');
Numbers.documents[0].sheets[0].tables[0].selectionRange = 'A2:A20'
// -> Error: Can't convert types.

      

I searched for it, but I didn't find anything to help me (good links are about AppleScript, and some links that contain something about JXA are about Mail).

Thank you for your help (any documentation link or any others to try would be appreciated).

+3


source to share


1 answer


This AppleScript:

tell application "Numbers"
    tell table 1 of sheet 1 of document 1
        set selection range to range "A2:A20"
    end tell
end tell

      

actually sets the table selection range to the table range named "A2: A20".



Here's the JavaScript equivalent:

var Numbers = Application("Numbers")
var table = Numbers.documents[0].sheets[0].tables[0]

table.selectionRange = table.ranges["A2:A20"]

      

+4


source







All Articles