问题
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:
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"]
来源:https://stackoverflow.com/questions/30701351/how-to-make-a-range-in-numbers-iwork-using-jxa