替换文本后文档不同步

问题描述 投票:1回答:2

我正在尝试替换Word Online文档中的某些文本,但无法使其工作。

'{{test}},[[test]],{test}'会产生'13,2,3'而不是'1,2,3'。

第一个文本似乎被处理了两次。

任何帮助非常感谢!

Office.initialize = function(reason) {

    function ready() {

        var myTags = [
            { "value": "1", "text": "{{test}}" },
            { "value": "2", "text": "[[test]]" },
            { "value": "3", "text": "{test}" }
        ];

        async function FillTag(tag) {

            await Word.run(async function(context) {

                    var options = Word.SearchOptions.newObject(context);
                    options.matchWildCards = false;

                    var searchResults = context.document.body.search(tag.text, options);
                    context.load(searchResults, 'text');

                    await context.sync();

                    searchResults.items.forEach(function(item) {
                        item.insertText(tag.value, Word.InsertLocation.replace);
                    });
                    await context.sync();
                })
                .catch(function(error) {
                    console.log('Error: ' + JSON.stringify(error));
                    if (error instanceof OfficeExtension.Error) {
                        console.log('Debug info: ' + JSON.stringify(error.debugInfo));
                    }
                });
        }

        async function ProcessArray(myTags) {
            myTags.forEach(async function(tag) {
                await FillTag(tag);
            });
        }

        ProcessArray(myTags);
    }

    if (document.readyState !== 'loading') {
        ready();
    }
    else {
        document.addEventListener('DOMContentLoaded', ready);
    }
};
office-js
2个回答
1
投票

这是一个调试建议而不是答案,但可以在以后编辑。请将Script Lab tool from AppSource安装到Word中。您可以在其中找到的示例代码段之一称为“搜索”。代码段中的一个功能是basicSearch。我将搜索文本“在线”替换为“{{test}}”,然后用以下行替换了突出显示黄色文本的行:

results.items[i].insertText("1", Word.InsertLocation.replace);

这很好,所以在简单的场景中,它可以准确地找到并替换“{{test}}”。

你能不能自己尝试一下,然后逐渐改变方法,使其更接近你的方式,看看它开始破坏的时间点?


编辑1/15/18:

@Kim Brandl的回答可能是最好的,假设你真的只有3个搜索字符串。但是,它确实在循环中有一个context.sync。由于每个同步都是到Office主机的往返,因此当输入数量很大和/或加载项在Office Online中运行时(这意味着Office主机是跨越Internet的同一个实例),这可能是一个性能问题机)。

对于读过这个拥有大量输入字符串的人来说,这里有一个解决方案,可以保证整个Word.run不需要超过3个同步。它还直接攻击您要解决的问题的来源,这是一些找到的范围与其他人的相对位置(具体地,一些在其他范围内)。

我在Word-Add-in-Angular2-StyleChecker中使用的策略是首先加载所有范围,然后使用Range.compareLocationWith方法和LocationRelation枚举来查找所需的相对位置信息。最后,使用每个范围与其他范围的相对位置来确定是否/如何处理它。

这是功能。按照Kim的例子,我把整个片段放在这个gist中,你可以导入到Script Lab tool from AppSource。 (参见Kim Brandl的回答中的说明。)

async function FindAndReplace() {

    let myTags = [
        { "value": "1", "text": "{{test}}" },
        { "value": "2", "text": "[[test]]" },
        { "value": "3", "text": "{test}" },
        { "value": "4", "text": "bob" },
        { "value": "5", "text": "bobb" },
        { "value": "6", "text": "ssally" },
        { "value": "7", "text": "sally" }
    ];

    let allSearchResults = [];

    await Word.run(async (context) => {    
        for (let tag of myTags) {    
            let options = Word.SearchOptions.newObject(context);
            options.matchWildCards = false;
            let searchResults = context.document.body.search(tag.text, options);
            searchResults.load('text');

            // Store each set of found ranges and the text that should replace 
            // them together, so we don't have to reconstruct the correlation 
            // after the context.sync.
            let correlatedSearchResult = {
                searchHits: searchResults, 
                replacementString: tag.value
            }           
            allSearchResults.push(correlatedSearchResult);       
        }

        await context.sync();

        // Now that we've loaded the found ranges we correlate each to
        // its replacement string, and then find each range's location relation
        // to every other. For example, 'bob' would be Inside 'xbobx'. 
        let correlatedFoundRanges = [];
        allSearchResults.forEach(function (correlatedSearchResult) {
            correlatedSearchResult.searchHits.items.forEach(function (foundRange) {
                let correlatedFoundRange = {
                    range: foundRange,
                    replacementText: correlatedSearchResult.replacementString,
                    locationRelations: []
                }
                correlatedFoundRanges.push(correlatedFoundRange);                
            });
        });

        // Two-dimensional loop over the found ranges to find each one's 
        // location relation with every other range.
        for (let i = 0; i < correlatedFoundRanges.length; i++) {
            for (let j = 0; j < correlatedFoundRanges.length; j++) {
                if (i !== j) // Don't need the range's location relation with itself.
                {
                    let locationRelation = correlatedFoundRanges[i].range.compareLocationWith(correlatedFoundRanges[j].range);
                    correlatedFoundRanges[i].locationRelations.push(locationRelation);
                }
            }
        }

        // It is not necesary to *explicitly* call load() for the 
        // LocationRelation objects, but a sync is required to load them.
        await context.sync();    

        let nonReplaceableRanges = [];
        correlatedFoundRanges.forEach(function (correlatedFoundRange) {
            correlatedFoundRange.locationRelations.forEach(function (locationRelation) {
                switch (locationRelation.value) {
                    case "Inside":
                    case "InsideStart":
                    case "InsideEnd":

                        // If the range is contained inside another range,
                        // blacklist it.
                        nonReplaceableRanges.push(correlatedFoundRange);
                        break;
                    default:
                        // Leave it off the blacklist, so it will get its 
                        // replacement string.
                        break;
                }
            });
        });

        // Do the replacement, but skip the blacklisted ranges.
        correlatedFoundRanges.forEach(function (correlatedFoundRange) {
            if (nonReplaceableRanges.indexOf(correlatedFoundRange) === -1) {
                correlatedFoundRange.range.insertText(correlatedFoundRange.replacementText, Word.InsertLocation.replace);
            }
        })

        await context.sync();
    });
}

2
投票

在你的ProcessArray()函数中,尝试用forEach语句替换for...of语句,如下所示:

async function ProcessArray(myTags) {
    for (var tag of myTags) {
        await FillTag(tag);
    }
}

似乎forEach语句会触发多个异步调用,而不是每次都在等待FillTag的完成。如果用forEach替换for...of语句,如上所示,您应该得到预期的结果。


更新(附加信息重新编码结构):

@DutchDan - 现在您的初始问题已经解决,这是构建代码的更佳方式。

Office.initialize = function () {
    $(document).ready(function () {        
        FindAndReplace();
    });
};

async function FindAndReplace() {

    var myTags = [
        { "value": "1", "text": "{{test}}" },
        { "value": "2", "text": "[[test]]" },
        { "value": "3", "text": "{test}" }
    ];

    await Word.run(async (context) => {

        for (var tag of myTags) {
            var options = Word.SearchOptions.newObject(context);
            options.matchWildCards = false;

            var searchResults = context.document.body.search(tag.text, options);

            context.load(searchResults, 'text');

            await context.sync();

            searchResults.items.forEach(function (item) {
                item.insertText(tag.value, Word.InsertLocation.replace);
            });

            await context.sync();
        }
    }).catch(errorHandler);
}

注意:您可以使用Script Lab(https://aka.ms/getscriptlab)快速轻松地尝试此代码段。只需安装Script Lab加载项(免费),然后在导航菜单中选择“导入”,并使用以下Gist URL:https://gist.github.com/kbrandl/b0c9d9ce0dd1ef16d61372cb84636898

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.