【发布时间】:2017-08-29 18:05:27
【问题描述】:
我正在开发一个 Office 加载项,它根据 JSON 格式的数据处理 Word 文档的每个段落的文本,并将结果写入任务窗格中呈现的 index.html 文件中的一个。这工作正常。我现在正在尝试格式化 Word 文档中与 JSON 数据中键的命中相对应的字符串。
我在 index.html 文件的头部有一个 JS 块,我在其中调用“Office.initialize”并根据 JSON 数据定义一个变量,并具有与上述功能相关的实用函数。之后是一个函数,我在其中获取 Word 上下文并根据 JSON 数据处理 Word 文件段落,然后尝试搜索 Word 段落本身以格式化命中。在最后一项任务中,我试图从 Michael Mainer 1 复制一个 sn-p。但是当我激活此功能时不会发生格式化。不幸的是,由于我在 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));
}
});
} `
【问题讨论】:
标签: word-addins