gpt4 book ai didi

javascript - 如何使用 Excel JavaScript API office Add-IN 最大限度地提高表行添加 50K+ 行的性能

转载 作者:行者123 更新时间:2023-11-29 23:30:34 28 4
gpt4 key购买 nike

我正在尝试向表中添加大量行。我的项目需要添加大表。

请让我知道是否有更好的替代方法来最大化性能。我应该使用 Range 对象 API 吗?

代码如下所示。

   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));
}
});

}

最佳答案

将行添加到尽可能大的 block (数组的数组)中而不是一次添加一行会更有效率。
此外,如果您的工作簿中有任何公式,您需要处于手动计算模式(不幸的是,由于某种原因您无法从 Office-JS API 设置计算模式)
或尝试使用最接近的等效项suspendCalculationUntilNextSync()

有关更多详细信息,请参阅我关于 EXcel JS 读写性能的博文: Excel JS Read-Write Performance

关于javascript - 如何使用 Excel JavaScript API office Add-IN 最大限度地提高表行添加 50K+ 行的性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47507898/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com