JXA:如何在Numbers中创建一个范围

时间:2018-02-01 17:49:08

标签: macos jxa iwork

我试过了:

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

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

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

有什么想法吗?

注意:语法' table.selectionRange = table.ranges [" C6:C7"];'在此处发布为解决方案:How to make a range in Numbers (iWork) using JXA

添加以进一步明确:

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.

1 个答案:

答案 0 :(得分:0)

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

  1. 对当前打开的表的有效引用,
  2. (设置.selectionRange)GUI焦点,例如,使用.activate()方法获得。
  3. (() => {
        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';
    })();