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

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

问题描述

摘要:

  • 在大型Word表(大于10乘10)中更新值非常慢. 随着表的大小,性能会成倍恶化.
  • 我正在使用myTable.values = arrNewValues.我也尝试过 myTable.addRows("end", rows, arrNewValues).其中arrNewValues是一个 2D阵列.
  • 我还尝试通过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列:.33秒
  • 4行x 10列:.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代码:

在没有ScreenUpdating = False的情况下,VBA的性能类似于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

解释慢速性能的文章:请参见自动化表时提高性能":

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()与更新表相关的帖子:

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

推荐答案

这可能不是您要查找的答案,但是我一直在使用add词来验证软件,我们正在谈论更新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天全站免登陆