This will require the use of a whose
clause for querying the element array of tables in the selected sheet—essentially, you want to find all Table
objects whose selectionRange
attribute is not null
(which is analogous to asserting that its class is range
). Unfortunately, there seems to be a bug in JXA that prevents comparisons with null
. So while it would be optimal to simply check _not: [{ selectionRange: null }]
in the code below, I found that that approach did not work. Instead, the following appears (albeit not particularly elegantly) to do what you're looking for:
Numbers = Application('Numbers')
selectedTables = Numbers.documents[0].activeSheet.tables.whose({
_not: [{
_match: [ObjectSpecifier().selectionRange.name, '']
}]
})
if (selectedTables.length > 0) {
thisTable = selectedTables[0]
} else {
// No table is selected -- handle accordingly
}
This is a pretty hacky approach (it relies on the fact that selectionRange.name
isn't even a valid key for non-selected tables), but given the buggy state of JXA, it may be the best one can do.
Further Reading: