如何在 Office.js 中对新的 Word 文档执行多段搜索(带更新)?
How to executed multiple paragraph searches (with updates) on new Word document in Office.js?
我正在尝试通过 Office.js 更新新 Word 文档中的一组段落。下面的代码是一个异步链,它按预期顺序循环。第一次调用 return 正确搜索 rangeCollection
段落,但所有后续调用 return 都是空 seachResults.items
对象。
不幸的是,我无法进行完整的文档搜索。我已经有了一个已知的段落索引列表,我需要在这些段落中找到要突出显示的已知文本
有什么想法吗?
var processDocAsync = function (context, paragraphs, onComplete) {
// A recursive helper function to work on the n'th paragraph
function getAndProcessParagraph(index) {
// stop processing
if (index == paragraphs.items.length) {
onComplete();
} else {
// Main recursive case
// search the indexth paragraph
var options = Word.SearchOptions.newObject(context);
options.matchCase = false
var searchResults = paragraphs.items[index].search('the', options);
context.load(searchResults, 'text, font');
context.sync().then(function () {
// Highlight all the "THE" words in this paragraph
try {
// Queue a command to change the font for each found item.
if (searchResults.items) {
for (var j = 0; j < searchResults.items.length; j++) {
searchResults.items[j].font.color = '#FF0000'
searchResults.items[j].font.highlightColor = '#FFFF00';
searchResults.items[j].font.bold = true;
}
}
} catch (myError) {
console.log(myError.message);
}
// Synchronize the document state by executing the queued-up commands,
// then move on to the next paragraph
return context.sync().then(getAndProcessParagraph(index + 1));
});
}
}
// Begin the recursive process with the first slice.
getAndProcessParagraph(0);
}
var openProcessedDoc = function (base64str, documentId) {
Word.run(function (context) {
var myNewDoc = context.application.createDocument(base64str);
context.load(myNewDoc);
var paragraphs = context.document.body.paragraphs;
paragraphs.load(paragraphs, 'text, font');
return context.sync().then(function () {
processDocAsync(context, paragraphs, processCompleted)
});
});
};
var processCompleted = function () {
console.log('Process completed');
}
好问题。这是一个典型的例子,说明如何使用 promises 和异步模式在集合中遍历集合。
Please setup script lab (a cool add-in you can use to try code snippets). Jump to 7:46 on this video 查看如何加载 yaml。
load this yaml 在脚本实验室中查看如何执行此操作的示例。该示例并不完全是您所需要的(它基本上遍历了一个段落中的一组单词)但希望您可以针对您的特定场景模仿这种模式。
编码愉快!
我正在尝试通过 Office.js 更新新 Word 文档中的一组段落。下面的代码是一个异步链,它按预期顺序循环。第一次调用 return 正确搜索 rangeCollection
段落,但所有后续调用 return 都是空 seachResults.items
对象。
不幸的是,我无法进行完整的文档搜索。我已经有了一个已知的段落索引列表,我需要在这些段落中找到要突出显示的已知文本
有什么想法吗?
var processDocAsync = function (context, paragraphs, onComplete) {
// A recursive helper function to work on the n'th paragraph
function getAndProcessParagraph(index) {
// stop processing
if (index == paragraphs.items.length) {
onComplete();
} else {
// Main recursive case
// search the indexth paragraph
var options = Word.SearchOptions.newObject(context);
options.matchCase = false
var searchResults = paragraphs.items[index].search('the', options);
context.load(searchResults, 'text, font');
context.sync().then(function () {
// Highlight all the "THE" words in this paragraph
try {
// Queue a command to change the font for each found item.
if (searchResults.items) {
for (var j = 0; j < searchResults.items.length; j++) {
searchResults.items[j].font.color = '#FF0000'
searchResults.items[j].font.highlightColor = '#FFFF00';
searchResults.items[j].font.bold = true;
}
}
} catch (myError) {
console.log(myError.message);
}
// Synchronize the document state by executing the queued-up commands,
// then move on to the next paragraph
return context.sync().then(getAndProcessParagraph(index + 1));
});
}
}
// Begin the recursive process with the first slice.
getAndProcessParagraph(0);
}
var openProcessedDoc = function (base64str, documentId) {
Word.run(function (context) {
var myNewDoc = context.application.createDocument(base64str);
context.load(myNewDoc);
var paragraphs = context.document.body.paragraphs;
paragraphs.load(paragraphs, 'text, font');
return context.sync().then(function () {
processDocAsync(context, paragraphs, processCompleted)
});
});
};
var processCompleted = function () {
console.log('Process completed');
}
好问题。这是一个典型的例子,说明如何使用 promises 和异步模式在集合中遍历集合。
Please setup script lab (a cool add-in you can use to try code snippets). Jump to 7:46 on this video 查看如何加载 yaml。
load this yaml 在脚本实验室中查看如何执行此操作的示例。该示例并不完全是您所需要的(它基本上遍历了一个段落中的一组单词)但希望您可以针对您的特定场景模仿这种模式。
编码愉快!