Ms word 如何在长字符串上循环搜索?

Ms word 如何在长字符串上循环搜索?,ms-word,office-js,Ms Word,Office Js,为了绕过桌面Word api中255个字符的搜索限制,我将长字符串分解为254个字符的可搜索块,并将它们放入一个对象“osearchters”。然后我尝试在oSearchTerms上迭代,搜索文本,高亮显示它,然后搜索下一个块,并执行相同的操作,直到oSearchTerms中的所有项目都高亮显示。问题是它没有循环。它成功地完成了第一次迭代,但停止了 我尝试了大量的context.sync()调用、return true、return context.sync()等,您将看到下面的注释无效 我还应

为了绕过桌面Word api中255个字符的搜索限制,我将长字符串分解为254个字符的可搜索块,并将它们放入一个对象“osearchters”。然后我尝试在oSearchTerms上迭代,搜索文本,高亮显示它,然后搜索下一个块,并执行相同的操作,直到oSearchTerms中的所有项目都高亮显示。问题是它没有循环。它成功地完成了第一次迭代,但停止了

我尝试了大量的context.sync()调用、return true、return context.sync()等,您将看到下面的注释无效

我还应该指出,它没有显示任何错误。循环就是不循环

我必须将其转换为异步函数吗?我希望坚持使用ES5,不要使用胖箭头函数

我错过了什么

var fullSearchTerm = "As discussed earlier, one of the primary objectives of these DYH rules is to ensure that operators have at least one source of XYZ-approved data and documents that they can use to comply with operational requirements The objective would be defeated if the required data and documents were not, in fact, approved and Only by retaining authority to approve these materials can we ensure that they comply with applicable requirements and can be relied upon by operators to comply with operational rules which We believe there are differences between EXSS ICA and other ICA that necessitate approval of EVIS ICA."

function findTextMatch() {
    Word.run(function(context) {
    OfficeExtension.config.extendedErrorLogging = true;
    var oSearchTerms = [];      
    var maxChars = 254;
    var lenFullSearchTerm = fullSearchTerm.length;
    var nSearchCycles = Math.ceil(Number((lenFullSearchTerm / maxChars)));
    console.log("lenFullSearchTerm: " + lenFullSearchTerm + " nSearchCycles: " + nSearchCycles);

    // create oSearchTerms object containing search terms
    // leaves short strings alone but breaks long strings into 
    // searchable 254 character chunks
    for (var i = 0; i < nSearchCycles; i++) {
        var posStart = i * maxChars;
        var mySrch = fullSearchTerm.substr(posStart, maxChars);
        console.log( i +" mySrch: "+ mySrch);
        var oSrch = {"searchterm":mySrch};
        oSearchTerms.push(oSrch);
    }

    console.log("oSearchTerms.length: " + oSearchTerms.length +" oSearchTerm: "+ JSON.stringify(oSearchTerms));

    // Begin search loop
    // iterate over oSearchTerms, find and highlight each searchterm
    for (var i = 0; i < oSearchTerms.length; i++) {
        console.log("oSearchTerms["+i+"].searchterm: " + JSON.stringify(oSearchTerms[i].searchterm));
        var searchResults = context.document.body.search(oSearchTerms[i].searchterm, { matchCase: true });      
        console.log("do context.sync() ");      
        context.load(searchResults);        
        return context.sync()
            .then(function(){
                console.log("done context.sync() ");                
                console.log("searchResults:  "+ JSON.stringify(searchResults));             
                if(typeof searchResults.items !== undefined){                   
                    console.log("i: "+i+ " searchResults: "+searchResults.items.length);
                    // highlight each result
                    for (var j = 0; j < searchResults.items.length; j++) {                          
                        console.log("highlight searchResults.items["+j +"]");       
                        searchResults.items[j].font.highlightColor = "red";
                    }                   
                }
                else{
                    console.log("typeof searchResults.items == undefined");                     
                }                   
                // return true;
                // return context.sync();
            });
            //.then(context.sync);      
            //return true;
    } // end search loop
})
.catch( function (error) {
        console.log('findTextMatch Error: ' + JSON.stringify(error));
        if (error instanceof OfficeExtension.Error) {
            console.log('findTextMatch Debug info: ' + JSON.stringify(error));
        }
    }); 
}
var fullSearchTerm=“如前所述,这些DYH规则的主要目标之一是确保运营商至少有一个XYZ批准的数据和文件来源,他们可以使用这些数据和文件来满足运营要求。如果所需的数据和文件事实上没有,经批准且只有保留批准这些材料的权限,我们才能确保它们符合适用要求,并且运营商可以依赖这些材料来遵守运营规则,我们认为EXSS ICA和其他ICA之间存在差异,需要获得EVIS ICA的批准。”
函数findTextMatch(){
运行(函数(上下文){
OfficeExtension.config.extendedErrorLogging=true;
var-osearchters=[];
var maxChars=254;
var lenFullSearchTerm=fullSearchTerm.length;
var nSearchCycles=Math.ceil(数字((lenFullSearchTerm/maxChars));
log(“lenFullSearchTerm:+lenFullSearchTerm+”nSearchCycles:+nSearchCycles”);
//创建包含搜索词的oSearchTerms对象
//保留短字符串,但将长字符串拆分为
//可搜索的254个字符块
对于(var i=0;i
我建议您不要在循环中使用
上下文.sync
。这可能会影响性能,使代码难以推理。请参阅我对的回答:和此示例:以获取避免这种情况的设计模式。如果您愿意,该模式可以与ES5语法一起使用


如果您实施此模式,您可能会发现问题已经消失,或者至少您可以更清楚地看到原因所在。

我以前回答过一次。谢谢,Cindy。我尝试过使用该解决方案,但如果搜索结果返回的完整搜索实例超过一个,则无法使其正常工作呃,为了不扩大范围,我想我应该依次突出显示所有返回搜索结果的块。