简体   繁体   English

提高使用office-js搜索Microsoft Word的性能

[英]Improving the performance of searching Microsoft Word using office-js

I have the following code to search a paragraph in a document for some text and if any occurrences are found then select the first one. 我有以下代码在文档中的某个段落中搜索某些文本,如果发现任何出现的内容,请选择第一个。

function navigateToWord(paragraphId, text){
    Word.run(function (context) {

        var ps = context.document.body.paragraphs;
        context.load(ps, 'items');

        return context.sync()
            .then(function (){
                let p = ps.items[paragraphId];

                let results = p.search(text);
                context.load(results, 'items');

                return context.sync().then(function(){
                    if(results.items.length>0){
                        results.items[0].select();
                    }
                }).then(context.sync);
            });

    });
}

This works but it is very slow, especially on larger documents on Word Online (Word Desktop performs slightly better). 此方法有效,但速度非常慢,尤其是在Word Online上较大的文档上(Word Desktop的性能稍好)。 How can I improve it? 我该如何改善?

I plan on invoking this code quite a few times ( with different input parameters ), is there a way to cache the loaded properties, so that the second time I invoke the same code, I don't have to wait as long? 我计划多次调用此代码( 使用不同的输入参数 ),是否可以缓存加载的属性,以便第二次调用相同的代码时不必等待那么长时间?

You are loading much more than you need. 您的负载远远超出了您的需求。 First a minor point: specifying 'items' in the load command is unnecessary. 首先要注意一点:不需要在load命令中指定“项目”。 'items' are automatically loaded when you have a context.load for a collection object. 当您具有集合对象的context.load时,会自动加载“ items”。 So context.load(ps, 'items'); 所以context.load(ps, 'items'); is equivalent to context.load(ps); 等效于context.load(ps); More importantly, by not specifying any other properties, load defaults to loading all the properties including the text, so all of the text of all of your paragraphs is going over the wire. 更重要的是,通过不指定任何其他属性,默认情况下加载将加载所有属性,包括文本,因此所有段落的所有文本都将遍历整个网络。 It is a best practice to specify the properties that you need in the load command. 最佳实践是在load命令中指定所需的属性。 In your case, however, you don't need any, so you should put a dummy string as the second parameter to load. 但是,在您的情况下,您不需要任何内容​​,因此应将虚拟字符串作为要加载的第二个参数。 This blocks loading of any properties. 这会阻止任何属性的加载。 The following code works and should be much faster, especially in Word Online: 以下代码可以工作,并且应该更快,尤其是在Word Online中:

function navigateToWord(paragraphId, text){
  Word.run(function (context) {

    var ps = context.document.body.paragraphs;
    context.load(ps, 'no-properties-needed');

    return context.sync()
        .then(function (){
            let p = ps.items[paragraphId];

            let results = p.search(text);
            context.load(results, 'no-properties-needed');

            return context.sync().then(function(){
                if(results.items.length>0){
                    results.items[0].select();
                }
            }).then(context.sync);
        });

    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM