【问题标题】:How to loop a search over a long string?如何在长字符串上循环搜索?
【发布时间】:2019-09-30 14:47:33
【问题描述】:

为了绕过桌面 Word api 中 255 个字符的搜索限制,我将长字符串分解为 254 个字符的可搜索块并将它们推送到对象“oSearchTerms”中。然后我尝试遍历 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));
        }
    }); 
}

【问题讨论】:

  • 我以前回答过一次。见stackoverflow.com/a/51159442/3077495
  • 谢谢,辛迪。我尝试使用该解决方案,但如果搜索结果返回多个完整搜索词的单个实例,则无法使其正常工作。代替扩展范围,我认为我只需按顺序突出显示所有返回的搜索结果的块。

标签: ms-word office-js


【解决方案1】:

我建议您不要在循环中使用context.sync。这可能会影响性能,并且使代码难以推理。请参阅我的回答:Document not in sync after replace text 和此示例:Word Add-in Stylechecker,以了解避免这种情况的设计模式。如果需要,该模式可以与 ES5 语法一起使用。

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

【讨论】:

    猜你喜欢
    • 2014-10-11
    • 2015-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-17
    • 2012-10-28
    • 1970-01-01
    相关资源
    最近更新 更多