在
将表行和列添加到文档或工作表中后,是否可以循环访问表行和列。
我正在使用以下代码添加一个表,我希望这段代码成功后,我应该能够访问行和列,应该能够在一些转换后替换单元格的值。波纹管是我用来创建表的代码。
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
}
});
}
}
);
}
是的,这是在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