Office.js Word 加载项:在大型表中更新值的性能问题

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

总结:

背景:

我的加载项 (https://analysisplace.com/Solutions/Document-Automation) 执行文档自动化(更新各种 Word 文档中的内容)。许多客户希望能够在较长的 table 秒内更新文本。有些文档有几十个 table(附录)。由于 table 更新,我 运行 发现更新这些文档的速度慢得令人无法接受(超过一分钟)。

更新时间 table 尺寸:

示例 Office.js 代码(脚本实验室):

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

示例词VBA代码:

VBA 性能类似于没有 ScreenUpdating = False 的 Office.js 性能。使用 ScreenUpdating = False,性能是即时的。

Sub PopulateTable()
   Application.ScreenUpdating = False
    Dim nrRow As Long, nrCol As Long
    Dim tbl As Word.Table
    Set tbl = ThisDocument.Tables(1)
    For nrRow = 1 To 32
        For nrCol = 1 To 10
            tbl.Cell(nrRow, nrCol).Range.Text = "c" & nrRow & ":" & nrCol
        Next nrCol
    Next nrRow
End Sub

解释性能缓慢的文章:参见"Improving Performance When Automating Tables":https://msdn.microsoft.com/en-us/library/aa537149(v=office.11).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-3

Posts表示Office.js中没有"ScreenUpdating = False": and 听起来我们不会很快看到它。

Post 与通过 getOoxml() 和 insertOoxml() 更新 tables 有关:Word Office.js: issues with updating tables in ContentControls using getOoxml() and insertOoxml()

这可能不是您要找的答案,但我一直在使用一个词插件来验证软件,我们正在谈论更新 500-1000 行,其中有很多小的格式更改。

无论如何,我发现有一件有用的事情是在对 table 进行更改之前滚动文档中的其他位置。只是看着它的行为就会减慢 10-20 倍。它并不总是即时的,而是近在咫尺的。