word桌面中的body.search问题 [英] body.search Issue in word desktop

查看:86
本文介绍了word桌面中的body.search问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

context.document.body.search()会引发InvalidArgument异常.使用Word Online似乎也是如此.

context.document.body.search() is throwing an InvalidArgument exception in Word 2016 for desktops when the text to search for is larger than 255 characters. The same seems to be working with Word Online.

var searchResults = context.document.body.search("TextMoreThan255Chars");
context.load(searchResults);
return context.sync().then(function() {

});

推荐答案

您遇到的是Word(对于台式机)的限制.例如,一些更多信息可在Greg Maxey的网站上找到. VBA的建议可以适应Office JS方法.

What you're experiencing is a limitation in Word (for the desktop). Some more information is available on Greg Maxey's site, for example. The VBA suggestion can adapted for an Office JS approach.

关键是将搜索词分解成少于255个字符的限制.搜索术语的第一部分(如果找到),搜索下一部分.如果找到了它,请检查它是否紧跟在第一个找到的范围之后.重复直到搜索完所有部分.

The key point is to break the search term down into pieces less than the 255 character limit. Search for the first part of the term, if it's found, search the next part. If it's found, check that it's immediately following the first found range. Repeat until all parts have been searched.

下面的示例代码在我的测试中很适合我-但是我不是JS人员,因此它的结构可能不是最佳的.它至少应提供有关如何执行任务的基础知识.最重要的概念是使用range对象:任何找到的range的副本都需要扩展到文档正文范围的末尾.这为搜索术语的下一个块提供了基础范围.

The sample code that follows works for me in my test - but I'm not a JS person, so it may not be optimally structured. It should at least provide the basics of how to go about performing the task. The most important concept is working with the range objects: a duplicate of any found range needs to be expanded to the end of the document body range. That provides the basis range for searching the next chunk of the term.

如果找到下一个零件,请确定它是否紧邻原始找到的范围(compareLocationWith).如果是这样,请继续循环播放,直到处理完整个搜索词为止.

If a next part is found, determine whether it's immediately adjacent to the original found range (compareLocationWith). If it is, continue looping until the entire search term has been processed.

最后,突出显示找到的术语.

At the end, the found term(s) are highlighted.

async function basicSearch() {
    await Word.run(async (context) => {
        let maxNrChars = 254;
        let searchterm = "";
        let shortSearch = true; //search string < 255 chars
        let fullSearchterm = "Video provides a powerful way to help you prove your point. When you click Online Video, you can paste in the embed code for the video you want to add. You can also type a keyword to search online for the video that best fits your document. Aösdlkvaösd faoweifu aösdlkcj aösdofi "
        let searchTermNrChars = fullSearchterm.length;
        let nrSearchCycles = Number((searchTermNrChars / maxNrChars).toFixed(0));
        let nrRemainingChars = searchTermNrChars - (nrSearchCycles * maxNrChars);
        //console.log("Number of characters in search term: " + searchTermNrChars
        //    + "\nnumber of search cycles required: " + nrSearchCycles
        //    + "\nremaining number of characters: " + nrRemainingChars);

//numerous ranges are required to extend original found range
        let bodyRange = context.document.body.getRange();
        bodyRange.load('End');
        let completeRange = null;
        let resultRange = null;
        let extendedRange = null;
        let followupRange = null;

        let cycleCounter = 0;
        let resultText = "";
        if (searchTermNrChars > maxNrChars) {
            searchterm = fullSearchterm.substring(0, maxNrChars);
            cycleCounter++;
            shortSearch = false;
        }
        else { searchterm = fullSearchterm; }

        let results = context.document.body.search(searchterm);
        results.load({ select: 'font/highlightColor, text' });

        await context.sync();

        // short search term, highlight...
        if (shortSearch) {
            for (let i = 0; i < results.items.length; i++) {
                results.items[i].font.highlightColor = "yellow";
            }
        }
        else {
            //console.log("Long search");
            for (let i = 0; i < results.items.length; i++) {
                resultRange = results.items[i];
                resultRange.load('End');
                extendedRange = resultRange.getRange('End').expandTo(bodyRange.getRange('End'));

                await context.sync();

                //search for the remainder of the long search term
                for (let cycle = 1; cycle < nrSearchCycles; cycle++) {
                    searchterm = fullSearchterm.substring((cycle * maxNrChars), maxNrChars);
                    //console.log(searchterm + " in cycle " + cycle);
                    let CycleResults = extendedRange.search(searchterm);
                    CycleResults.load({ select: 'text, Start, End' });
                    await context.sync();
                    followupRange = CycleResults.items[0];

                    //directly adjacent?
                    let isAfter = followupRange.compareLocationWith(resultRange);
                    if (isAfter.value == Word.LocationRelation.adjacentAfter) {
                        resultRange.expandTo(followupRange);
                        extendedRange = resultRange.getRange('End').expandTo(bodyRange.getRange('End'));
                    }
                    await context.sync();
                }

                if (nrRemainingChars > 0) {
                    console.log("In remaining chars");
                    searchterm = fullSearchterm.substring(searchTermNrChars - nrRemainingChars);
                    console.log(searchterm);
                    let xresults = extendedRange.search(searchterm);
                    xresults.load('end, text');
                    await context.sync();

                    let xresult = xresults.items[0];

                    let isAfter = xresult.compareLocationWith(resultRange);

                    await context.sync();
                    console.log(isAfter.value);
                    if (isAfter.value == Word.LocationRelation.adjacentAfter) {
                        completeRange = resultRange.expandTo(xresult);
                        completeRange.load('text');
                        //completeRange.select();
                        await context.sync();
                        resultText = completeRange.text.substring(0, fullSearchterm.length);
                        console.log("Result" + cycleCounter + ": " + resultText);
                    }  
                }
                else {
                    //No remeaining chars
                    resultRange.load('text');
                    //resultRange.select();
                    await context.sync();
                    resultText = resultRange.text.substring(0, fullSearchterm.length);
                    completeRange = resultRange; 
                }

                //long search successful?
                if (resultText == fullSearchterm) {
                    completeRange.font.highlightColor = "yellow";
                }
                else {
                    console.log("Else! " + resultText + "  /  " + fullSearchterm);
                }
                completeRange = null;
            }
        }  
    });

这篇关于word桌面中的body.search问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆