5

添加到文档或工作表中后,是否可以遍历表行和列。

我正在使用以下代码添加一个表,并且我希望此代码成功我应该能够访问行和列应该能够在一些转换后替换单元格的值。Bellow 是我用来创建表格的代码。

     var tableData = new Office.TableData();
            var headers = [placeholder.columns.map(function (c) { return c.column; })];
            tableData.headers = headers
            tableData.rows = rows;
            var document = Office.context.document;

            document.setSelectedDataAsync(tableData, function (result) {
                var placeholder = Office.context.document.settings.get(results.binding.id);
                    if (officeCallSucceded(result, true)) {
                        document.bindings.addFromSelectionAsync(Office.BindingType.Table, function (result) {
                            if (officeCallSucceded(result)) {
                               //SOME  LOGIC FOR BINDING HERE TO ADD //EVENT handlers to the table just added
                            }
                        });
                    }
                }
                );
            }
4

1 回答 1

0

是的,这是在 Excel中检索表格的任何单个行的代码:

Excel.run(function (ctx) { 
    // substitute 'Table1' with the table you want and '0' with your row index
    var myRow = ctx.workbook.tables.getItem('Table1').rows.getItemAt(0);
    myRow.load('values');
    return ctx.sync().then(function() {
        console.log(myRow.values);
    });
});

要连续替换内容:

Excel.run(function (ctx) { 
    var myNewRow = [["a", "b", "c"]];
    // substitute 'Table1' with the table you want and '0' with your row
    var row = ctx.workbook.tables.getItem('Table1').rows.getItemAt(0);
    row.values = myNewRow;
    return ctx.sync();
});

在 Word 中有一个类似的 TableRowCollection.getItem 方法,但它仍处于预览阶段:https ://github.com/OfficeDev/office-js-docs/blob/WordJs_1.3_Openspec/word/resources/tablerowcollection.md#getitemindex-number

于 2016-07-14T16:46:10.853 回答