gpt4 book ai didi

javascript - MS Word Web 插件 javascript 同时搜索多个字符串

转载 作者:行者123 更新时间:2023-11-30 20:59:30 25 4
gpt4 key购买 nike

我已经能够运行 Github 项目了 Word-Add-in-JS-Redact成功地。现在,我需要更改代码。为此需要帮助。

目前,该代码在 Word 文档中查找多个出现的单个字符串并突出显示这些。但是,我需要通过单击按钮在文档中搜索多个字符串(也可以在 js 文件中进行硬编码),然后突出显示这些字符串。例如,我想同时在文档中找到字符串 'this''that',并突出显示它们。

搜索单个字符串的当前代码:

Word.run(function (context) {
//search string
var searchInput = "hello";
// Search the document.
var searchResults = context.document.body.search(searchInput, {matchWildCards: true});

// Load the search results and get the font property values.
context.load(searchResults, "");

// Synchronize the document state by executing the queued-up commands,
// and return a promise to indicate task completion.
return context.sync()
.then(function () {
var count = searchResults.items.length;
// // Queue a set of commands to change the font for each found item.
for (var i = 0; i < count; i++) {
searchResults.items[i].font.highlightColor = '#FFFF00'; //Yellow
}
return count;
})
.then(context.sync)
.then(reportWordsFound);

到目前为止,我已经尝试了几种方法,但都没有成功:

  1. 在搜索字符串的循环中运行 context.document.body.search(searchInput,,并尝试使用 +push。此尝试出错,提示我无法将多个上下文结果添加到单个对象。

  2. 我尝试对 WildCards 运算符进行创新,但没有适合的方法。许多帖子都在谈论 JS 正则表达式 \(string1|string2/),但这在 Word 上下文中似乎无效。

最佳答案

我终于可以解决这个问题了,方法是创建一个父函数,该函数在循环中调用搜索函数。我的错误是在搜索函数本身内部创建了循环。

这是工作代码:

function searchStrings(){
searchString('hello world');
searchString('Goodbye');
}
RedactAddin.searchStrings = searchStrings;

function searchString(input) {
// Run a batch operation against the Word object model.
Word.run(function (context) {
//...... (same as the original) ...............

关于javascript - MS Word Web 插件 javascript 同时搜索多个字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47275221/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com