0

我试过了:

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

获取:错误 -1728:无法获取对象。

我可以使用表格对象:例如:table.rowCount();

有任何想法吗?

注意:语法'table.selectionRange = table.ranges["C6:C7"];' 已在此处作为解决方案发布:如何使用 JXA 在 Numbers (iWork) 中创建范围

为进一步清晰添加:

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());

给出:

/* 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.
4

1 回答 1

0

上面的语法是正确的,但是两者都需要:

  1. 对当前打开的表的有效引用,以及
  2. (设置 .selectionRange)GUI 焦点,例如,使用 .activate() 方法获得。
(() => {
    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';
})();
于 2018-02-02T01:32:53.650 回答