1
我开发了Office加载项,对处理JSON格式数据的Word文档的每个段落的文字和index.html文件中的结果写入该在任务窗格中呈现。这工作正常。我现在试图格式化Word文档中对应于JSON数据中键值的字符串。在办公室使用Word.SearchOptions外接
我在index.html文件我称之为“Office.initialize”和定义基于JSON数据的变量的头部的JS块,并具有与上述功能的效用函数。之后,我得到Word上下文,并根据JSON数据处理Word文件段落,然后尝试搜索Word段落以对格式进行格式化。在这最后一个任务中,我试图重现Michael Mainer 1的片段。但是当我激活这个功能时没有格式化。不幸的是我没有访问控制台,因为我在Mac上,这使得它更难调试。
我会很感激别人给我看,我要去哪里错了。
'功能测试仪(){ Word.run(函数(上下文){
// Create a proxy object for the document's paragraphs
var paragraphs = context.document.body.paragraphs;
// Load the paragraphs' text, which I run regexes on
context.load(paragraphs, 'text');
return context.sync().then(function() {
for (var i = 0; i < paragraphs.items.length; i++) {
var text = paragraphs.items[i].text;
// jquery to iterate over the "notes" objects from the JSON
$.each(notes, function(key, value) {
var regex = new RegExp("\\b" + key + "\\b", "g");
var res = regex.test(text);
// if the regex hits...
if (res == true) {
// This part works fine, using the JSON data to append to the <DIV> with ID = "notes"
document.getElementById('notes').innerHTML += "<button onclick=hide('" + value.seqNo + "')><b>" + key + "</b></button><p class='" + value.seqNo + "' id='" + i + "'>" + value.notes[0].note + "</p>";
// I now go on to searching for these hits within the current paragraph in the Word file
var thisPara = paragraphs.items[i];
// Set up the search options.
var options = Word.SearchOptions.newObject(context);
options.matchCase = false
// Queue the commmand to search the current paragraph for occurrences of the string "key" (coming from the JSON data)
var searchResults = paragraphs.items[i].search(key, options);
// Load 'text' and 'font' for searchResults.
context.load(searchResults, 'text, font');
// Synchronize the document state by executing the queued-up commands, and return a promise to indicate task completion.
return context.sync().then(function() {
// Queue a command to change the font for each found item.
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;
}
// Synchronize the document state by executing the queued-up commands,
// and return a promise to indicate task completion.
return context.sync();
});
}
});
}
});
})
.catch(function (error) {
console.log('Error: ' + JSON.stringify(error));
if (error instanceof OfficeExtension.Error) {
console.log('Debug info: ' + JSON.stringify(error.debugInfo));
}
});
} `
我这样做了(请参阅代码中的“context.load(searchResults,'text,font');”)。我认为问题来自于想要搜索一系列术语,而不仅仅是像Michael Mainer's那样的例子。当我创建一个单独的JS函数来循环访问这个数组,并将它们逐个发送给在Word中搜索的函数时,它就结束了工作。 – tioken