Office.js Word 加载项:更新大表中的值时出现性能问题 [英] Office.js Word Add-In: Performance Issue with Updating Values in Large Tables

查看:19
本文介绍了Office.js Word 加载项:更新大表中的值时出现性能问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

总结:

  • 更新大型 Word 表格(大于 10 x 10)中的值非常缓慢.随着表的大小,性能会呈指数级下降.
  • 我正在使用 myTable.values = arrNewValues.我也试过myTable.addRows("end", rows, arrNewValues).其中 arrNewValues 是二维数组.
  • 我也尝试过通过 getOoxml() 和insertOoxml(),但遇到了我无法解决的其他问题解决,但具有良好的性能.
  • 性能缓慢似乎是由ScreenUpdating"引起的(VBA 中存在同样的问题,可通过 ScreenUpdating=false 解决).我认为添加暂时关闭 ScreenUpdating 的功能非常重要.
  • 还有其他方法可以提高表更新性能吗?
  • Updating values in large Word tables (larger than 10 by 10) is very slow. Performance gets exponentially worse with table size.
  • I'm using myTable.values = arrNewValues. I've also tried myTable.addRows("end", rows, arrNewValues). Where arrNewValues is a 2D array.
  • I've also tried using updating via getOoxml() and insertOoxml(), but ran into other issues I haven't been able to resolve, but has good performance.
  • Slow performance seems to be caused by "ScreenUpdating" (same issue exists in VBA and is solved via ScreenUpdating=false). I believe it is critically important to add the ability to temporarily turn off ScreenUpdating.
  • Is there another way to improve table updating performance?

背景:

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

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.

按表大小更新时间:

  • 2 行 x 10 列:0.33 秒
  • 4 行 x 10 列:0.52 秒
  • 8 行 x 10 列:1.5 秒
  • 16 行 x 10 列:5.5 秒
  • 32 行 x 10 列:20.8 秒
  • 64 行 x 10 列:88 秒

示例 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,性能立竿见影.

VBA performance is similar to the Office.js performance without ScreenUpdating = False. With ScreenUpdating = False, performance is instant.

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

解释性能缓慢的文章:请参阅自动化表时提高性能":https://msdn.microsoft.com/en-us/library/aa537149(v=office.11​​).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-3

Article explaining slow performance: see "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

表明 Office.js 中没有ScreenUpdating = False"的帖子:ScreenUpdating Office-js taskpane相当于 Application.ScreenUpdating 属性在 office-js Excel 插件中听起来我们很快就看不到了.

Posts indicating there is no "ScreenUpdating = False" in Office.js: ScreenUpdating Office-js taskpane and Equivalent to Application.ScreenUpdating Property in office-js Excel add-in Sounds like we won't see it any time soon.

通过 getOoxml() 和 insertOoxml() 发布与更新表相关的帖子:Word Office.js:使用 getOoxml() 和 insertOoxml() 更新 ContentControls 中的表格的问题

Post related to the updating tables via getOoxml() and insertOoxml(): Word Office.js: issues with updating tables in ContentControls using getOoxml() and insertOoxml()

推荐答案

这可能不是您要找的答案,但我一直在使用 word add in 来验证软件,我们正在讨论更新500-1000 行,有很多小的格式变化.

This is probably not the answer you're looking for, but I have been working with a word add in for validation of software, and we are talking about updating 500-1000 rows with lots of little formatting changes.

无论如何,我发现有帮助的一件事是在对表格进行更改之前滚动文档中的其他位置.仅仅看它的行为就会使速度减慢 10-20 倍.它并不总是即时的,而是近在咫尺的.

Anyway one thing I found that helped is to scroll somewhere else in the document before you make the changes to the table. Just the act of looking at it will slow it down 10-20x. It's not always instant but near.

这篇关于Office.js Word 加载项:更新大表中的值时出现性能问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆