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

1.9k Views Asked by At

I'm using JXA for automating a process using Numbers app. What I need is to select a range of cells to apply width, but JXA doesn't allow me to get them.

According to apple documentation I only need to use make or push the created object inside of array, but any ones work. This is my code and the 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 to Javascript) I can do it assigning text that represent the range:

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

But if I do something like that with Javascript, it does not work:

Option 4:

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

I have searched for it, but I have not found anything that help me (good references are about AppleScript and the few references that contain something about JXA are about Mail).

Thank you for your help (any link with documentation or any ides to try will be appreciate).

1

There are 1 best solutions below

1
On BEST 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

is actually setting the table's selection range to the table's range named "A2:A20".

Here is the equivalent JavaScript:

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

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