0

I tried:

table.selectionRange = table.ranges["C6:C7"];

Gets: Error -1728: Can't get object.

I am able to use the table object: Eg: table.rowCount();

Any ideas?

Note: Syntax 'table.selectionRange = table.ranges["C6:C7"];' was posted as a solution here: How to make a range in Numbers (iWork) using JXA

Added for further clarity:

Logger.logInfo("#Rows " + table.rowCount());
Logger.logInfo("Current Range " + table.selectionRange.name());
Logger.logInfo("#Cols " + table.columnCount());
table.selectionRange = table.ranges["C6:C7"];
Logger.logInfo("New Range " + table.selectionRange.name());

Gives:

/* 2018/02/02 @ 19:36:27.020: Info    : BlockPriceUpdateYPF: #Rows 34 */
/* 2018/02/02 @ 19:36:27.023: Info    : BlockPriceUpdateYPF: Current Range C5:G31 */
/* 2018/02/02 @ 19:36:27.025: Info    : BlockPriceUpdateYPF: #Cols 15 */
Result: Error -1728: Can't get object.
Crashmeister
  • 375
  • 1
  • 14

1 Answers1

0

The syntax above is correct, but one needs both:

  1. a valid reference to a currently open table, and
  2. (to set a .selectionRange) GUI focus, obtained, for example, using the .activate() method.
(() => {
    const
        app = Application('Numbers'),
        ws = app.windows,
        w = ws.length > 0 ? (
            ws.at(0)
        ) : undefined,
        sheets = w ? w.document.sheets : [],
        sheet = sheets.length > 0 ? (
            sheets.at(0)
        ) : undefined,
        tables = sheet ? sheet.tables : [],
        table = tables.length > 0 ? (
            tables.at(0)
        ) : undefined;

    app.activate();

    // READ the selection range (name property of table.selectionRange())
    // return table ? (() => {
    //     const maybeSelection = table.selectionRange();
    //     return (typeof maybeSelection) === 'function' ? (
    //         maybeSelection.name()
    //     ) : 'No range selected'
    // })() : 'No active table found in Numbers';

    // or SET the selection:
    return table ? (
        table.selectionRange = table.ranges['D10:D12'],
        'Range now selected: ' + table.selectionRange.name()
    ) : 'No active table';
})();
houthakker
  • 688
  • 5
  • 13
  • My original post stated that I did have a valid table object. Able to get table.rowCount(). – Crashmeister Feb 03 '18 at 00:26
  • The code above is working on Sierra. The only additional precaution which comes to mind, and which would differentiate the .rowCount() and .selectionRange() methods, is activation of the document, which may not yield a value for selectionRange if it doesn't have GUI focus. – houthakker Feb 04 '18 at 15:58
  • And therein may lie the problem. This script runs every 15 minutes to update stock prices and does not bring Numbers into the foreground so as not to disrupt what the user is doing on the computer. I was updating cell by cell, which is very slow, but will have to leave it that way. Interrupting the user every 15 minutes is not an option. Thanks. – Crashmeister Feb 05 '18 at 18:09
  • That makes sense. I've amended the answer above in case others find their way here with a similar problem. – houthakker Feb 06 '18 at 19:47