OfficeDev / office-js

A repo and NPM package for Office.js, corresponding to a copy of what gets published to the official "evergreen" Office.js CDN, at https://appsforoffice.microsoft.com/lib/1/hosted/office.js.
https://learn.microsoft.com/javascript/api/overview
Other
685 stars 95 forks source link

Office.js Word Add-In: Performance Issue with Updating Values in Large Tables #2279

Closed hengchengfei closed 7 months ago

hengchengfei commented 2 years ago

Message from office-js bot: We’re closing this issue because it has been inactive for a long time. We’re doing this to keep the issues list manageable and useful for everyone. If this issue is still relevant for you, please create a new issue. Thank you for your understanding and continued feedback.

Your Environment

Expected behavior

My add-in (https://analysisplace.com/Solutions/Document-Automation) performs document automation (updates content in a variety of Word docs). Many customers want to be able to update text in largish tables. Some documents have dozens of tables (appendices). I have run into the issue where updating these documents is unacceptably slow (well over a minute) due to the table updates.

Update time by table size:

function updateTableCells() {
    Word.run(function (context) {   
        var arrValues = context.document.body.tables.getFirst().load("values");
        return context.sync().then(
            function () {
                var rows = arrValues.values.length;
                var cols = arrValues.values[0].length;
                console.log(getTimeElapsed() + "rows " + rows + "cols " + cols);
                var arrNewValues = [];
                for (var row = 0; row < rows; row++) {
                    arrNewValues[row] = [];
                    for (var col = 0; col < cols; col++) {
                        arrNewValues[row][col] = 'r' + row + ':c' + col;
                    }
                }
                console.log(getTimeElapsed() + 'Before setValues ') ;
                context.document.body.tables.getFirst().values = arrNewValues;
                return context.sync().then(
                    function () {
                        console.log(getTimeElapsed() + "Done");
                });
            });
    })
        .catch(OfficeHelpers.Utilities.log);
}
grangeryy commented 2 years ago

@AbidRahman-MSFT do you have any suggestion on this?

AbidRahman-MSFT commented 2 years ago

Thanks @hengchengfei for reporting the issue here. I can reproduce the same issue on a Mac as well - I will discuss with our engineers and get back to you to see if there is any way we can improve the performance for updating larger tables.

AbidRahman-MSFT commented 2 years ago

Hey @hengchengfei - this is currently a limitation of the API. Please try the following workaround to populate table values using the 1.0 API set, this should greatly improve performance:

    console.log(new Date().getMilliseconds() - start.getMilliseconds() + "ms Before setValues ");
    //table.values = arrNewValues;
    table.select()
    await context.sync();
    Office.context.document.setSelectedDataAsync(arrNewValues, function (asyncResult) {
      if (asyncResult.status == Office.AsyncResultStatus.Succeeded) {
        console.log(new Date().getMilliseconds() - start.getMilliseconds() + "ms Done");
      }
    }); 
hengchengfei commented 2 years ago

@AbidRahman-MSFT Thank you.

I tried but it taked more time.

4tti commented 2 years ago

hello, we are facing very similar issue on desktop version (windows, however I guess it will be similar on Mac) In our case, the performance is depending on the visible area of the page, see the behavior in attached video.

https://user-images.githubusercontent.com/19533927/174286878-8abc5b8d-06de-40bb-91a8-79a8abc69656.mp4

Word version: image

Gist: https://gist.github.com/jakuboppelt/039ae39ca0d4a056ca310a9143fbf729

4tti commented 7 months ago

@gergzk Why was this closed? It is real issue (or at least was). @wangyun-microsoft, @grangeryy, @AbidRahman-MSFT

wangyun-microsoft commented 7 months ago

Hi @4tti,

The issue was closed by Bot because it has been inactive for a long time. It did this to keep the issues list manageable and useful for everyone.

Is this still a problem for you?

4tti commented 7 months ago

No problem, just verified it is much faster now. Although there is small delay before it starts it is better than before. Thanks :)

wangyun-microsoft commented 7 months ago

Great to hear that it has improved. If this continues to be a problem, please open a new issue to track the new ask.

Thanks.