0

此代码在 Excel 中运行良好,但在 Excel Online 中不起作用。但是,在 Excel Online 中,它会在浏览器日志中生成错误,而且还会在表中插入空行。

我已经学习了这个函数的 API 版本。自 2017 年以来,这似乎在 Excel Online 中有效。

 function onWorksheetAdded(eventArgs) {
 Excel.run(function (context) {
    var table = context.workbook.tables.getItem(INDEX_TABLE_NAME);
    var tableRows = table.rows;
    var tableColumns = table.columns; 
    tableRows.load('items'); tableColumns.load('items');

    return context.sync().then(function () {
        var row = []; for (var j = 0; j < tableColumns.items.length; j++) row.push(0);
        var newRow = [];
        newRow.push(row);
        tableRows.add(0, newRow); //here the mistake, but I can't fix it
        return context.sync();
    });
}).catch(function (error) {
    console.log("Error: " + error);
    if (error instanceof OfficeExtension.Error) {
        console.log("Debug info: " + JSON.stringify(error.debugInfo));
    }
});
}

我在日志中发现了这个:错误:GeneralException:处理请求时出现内部错误。Home.js:204:21 调试信息:{"code":"GeneralException","message":"处理请求时出现内部错误。","errorLocation":"TableRowCollection.add","statement": "var add=rows.add(...);", ...

请帮我解决这个问题。

4

1 回答 1

0

尝试添加 await context.sync(); 在 tableColumns.load('items') 之后看看它是否可以帮助你。

我用下面的代码在我的 Excel Online 上完成了它,它运行良好:

expensesTable.rows.load('items'); expensesTable.columns.load('items');
await context.sync();

var row = []; for (var j = 0; j < expensesTable.columns.items.length; j++) row.push(0);
var newRow = [];
newRow.push(row);
expensesTable.rows.add(0, newRow);
于 2019-07-23T06:47:14.127 回答