How to maximise performance for table row addition of 50K+ rows using Excel JavaScript API office Add-IN

时光毁灭记忆、已成空白 提交于 2019-12-10 23:17:55

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!