首页 > 解决方案 > SearchOptions 使用 Word 的序列号 - MS Office Word Addin OfficeJS

问题描述

现在,我通过使用搜索词来定位 Word 文档中的词

context.document.body.search(dict, { ignorePunct: true, matchCase: true, matchWholeWord: true });`

但我想通过它在 Word 文档中的序号位置来定位一个单词。我找不到确切的nth词。

有可能这样做吗?

标签: ms-wordoffice-jsjavascript-api-for-office

解决方案


Speedy:您可以为此使用 range.split() 方法。(即这适用于 searchResult、正文、段落、内容控制等)假设您想获得段落中的第 4 个单词,您可以这样做:

async function run() {
    await Word.run(async (context) => {
        //the method returns an array with the split ranges...
// the first parameter is an array of the delimiters you want to use, in this case the space. you can add more if needed.
        let myWords = context.document.body.paragraphs.getFirst().split([" "], true, true);
        myWords.load();
        await context.sync();
        if (myWords.items.length >= 3)  // just because i want the 4th word :)
            console.log(myWords.items[3].text);

    });
}

如果您想在 Script lab 中使用它,这里是您可以导入的要点。


推荐阅读