问题
I m trying to add large number of rows to table. My Project has requirement for large table addition.
Please let me know if there is a better alternative to maximize performance. Should I use Range object API?
The code is as shown below.
function createSampleSheet(numberOfTimes) {
startTime = performance.now();
Excel.run(function (context) {
var sheets = context.workbook.worksheets;
var sheet = context.workbook.worksheets.getActiveWorksheet();
var expensesTable = sheet.tables.add("A1:H1", true /*hasHeaders*/);
expensesTable.name = "ExpensesTable";
expensesTable.getHeaderRowRange().values = [["Date", "Merchant", "Category", "Category Type", "Class", "NHID", "Collab ID", "WBID"]];
expensesTable.rows.add(null /*add rows to the end of the table*/, [
["1/1/2017", "The Phone Company", "Communications", "BCD","Distinction", "3", "45", "1000036"]
]);
for (i = 1; i <= numberOfTimes; i++) {
expensesTable.rows.add(null /*add rows to the end of the table*/, [
["1/2/2017", "The Mobile Company", "Corporations", "BSD", "First", "2", "36", "1000026"] ]);}
return context.sync()
.then(function () {
endTime = performance.now();
log.logOutput(" " + numberOfTimes + " rows++ " + (endTime - startTime) + " milliseconds OR " + ((endTime - startTime) / 1000) + " seconds")
})
.then(context.sync);
}).catch(function (error) {
console.log("Error: " + error);
BWUI.errorHandler("Upload tables failed : " + error);
if (error instanceof OfficeExtension.Error) {
console.log("Debug info: " + JSON.stringify(error.debugInfo));
}
});
}
回答1:
It will be more efficient to add your rows in as large a block (array of arrays) as possible rather than a single row at a time.
Also if you have any formulas in your workbook you need to be in Manual Calculation mode (unfortunately for some reason you cannot set Calculation Mode from the Office-JS API)
or try to use the nearest equivalent which is
suspendCalculationUntilNextSync()
See my blog post on EXcel JS Read-Write performance for more details: Excel JS Read-Write Performance
回答2:
As Charles mentioned, updating the underlying range of the table body will result in better performance. This does mean that table should be enabled to auto expand, which is the default setting. There is an ongoing investigation and engineering work regarding the table update performance. You can monitor for updates here.
来源:https://stackoverflow.com/questions/47507898/how-to-maximise-performance-for-table-row-addition-of-50k-rows-using-excel-java