替换文本后文档不同步

时间:2018-01-12 14:08:25

标签: office-js

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

' {{test}},[[test]],{test}'结果在
 2,3,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);
    }
};

2 个答案:

答案 0 :(得分: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);
}

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

答案 1 :(得分: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();
    });
}