ms-word - SearchOptions 使用 Word 的序列号 - MS Office Word Addin OfficeJS
问题描述
现在,我通过使用搜索词来定位 Word 文档中的词
context.document.body.search(dict, { ignorePunct: true, matchCase: true, matchWholeWord: true });`
但我想通过它在 Word 文档中的序号位置来定位一个单词。我找不到确切的nth
词。
有可能这样做吗?
解决方案
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 中使用它,这里是您可以导入的要点。
推荐阅读
- php - MySQL:发送 QUERY 数据包时出错。PID=2
- javascript - 默认参数导致无法读取未定义的属性?
- html - 如何使用角度 5 中的 setAttribute 将鼠标悬停事件添加到动态生成的 html 元素
- macos - Word中的宏以添加带有文件路径的文本框
- android - 我的 Android 应用程序在收到来自 firebase 的推送通知时崩溃
- php - 使用 ip 将用户重定向到不同的网站
- ios - 可调整大小的集合视图单元格
- node.js - 如何从外部服务向机器人框架进行身份验证?
- android - 改造无法从模型类解析 JSON
- git - 日期在 1973 年之前的 git commit 失败