Word JS API:扩展范围 [英] Word JS APIs: extending a Range

查看:89
本文介绍了Word JS API:扩展范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在尝试回答此问题时,我真的很希望能够将范围扩展到特定数量人物.在COM API中,我将使用Range.MoveEnd().有没有我在JS API中找不到的等效项?

While working on answering this question I would really like to have been able to extend a Range by a specific number of characters. In the COM API I would have used Range.MoveEnd(). Is there any equivalent that I didn't find in the JS API?

背景:所参考的问题是有关查找超过255个字符的搜索词-这是Word在桌面上的限制.搜索失败.

Background: The question referenced is about finding search terms with more than 255 characters - which is a limit in Word for the desktop. The search fails.

解决这个问题的简单方法是搜索前254个字符,然后用剩余的字符数扩展找到的范围,然后将Range.Text与完整的搜索词进行比较.

The simple way to go about it would be to search the first 254 characters, then expand the found Range by the remaining number of characters and comparing that Range.Text to the full search term.

找不到以这种方式扩展范围的等效项,我不得不诉诸:

Not finding any equivalent for expanding a Range in this manner, I had to resort to:

  • 将搜索词分解为< 255个字符
  • 一张一张地搜索每一张
  • 确定每个搜索到的片段是否与上一个相邻
  • 然后扩展范围以包括相邻的片段
  • 重复直到找到所有碎片

因此,我的问题...

Thus, my question...

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;
            }
        }  
    });

推荐答案

这是我们在原始设计中所拥有的,但实际上已从API中删除,因为它很容易导致意外的结果(即隐藏的字符不一致,脚注)等),而我们手头的资源无法涵盖这些情况.我们决定将其删除.

That was something we had in the original design, but it was actually removed from the API as it can easily lead to unexpected outcomes (i.e. hidden character inconsistencies, footnotes, etc.), and we could not cover those cases with the resources at hand. We decided to remove it.

话虽如此,我认为您可以使用Word.js实现类似于range.MoveEnd()的功能,您只需要定义;;的末尾即可.一种方法是使用range.expandTo(endRange)方法.再次,有趣的是如何获取"endRange",因此下面的示例演示了如果"end"表示段落的末尾(可能在您的情况下就足够了)的话,该如何做.

That been said I think you can achieve something similar to range.MoveEnd() with Word.js, you just need to define to the end of what ;). One way of doing it is to use the range.expandTo(endRange) method. Again, The interesting thing is how to get the "endRange", so the following example shows how to do it if "end" means the end of the paragraph, probably in your scenario will suffice.

async function run() {
    await Word.run(async (context) => {
        //assume the range at the end of your 255 characters.
        var startRange = context.document.getSelection().getRange("end"); 
        //This one is the range at the end of the paragraph including the selection.
        var endRange = context.document.getSelection().paragraphs.getLast().getRange("end");

        var deltaRange = startRange.expandTo(endRange);
        context.load(deltaRange);

        await context.sync();
        // this will print the characters after the range all the way to the end of the paragraph.
        console.log(deltaRange.text);
    });
}

希望这有助于或至少使您朝正确的方向迈进.

hope this helps or at least sets you up in the right direction.

这篇关于Word JS API:扩展范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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