【发布时间】:2017-05-11 07:02:12
【问题描述】:
如何列出标题(标题 1、标题 2 等)、找到特定的(按名称)并在其中插入段落?在新的 Word JS API 中是否可行?
更新。谢谢瑞克!在此处粘贴代码可以解决问题:
await Word.run(async (context) => {
try {
let paragraphs = context.document.body.paragraphs;
context.load(paragraphs, ['items']);
// Synchronize the document state by executing the queued commands
await context.sync();
let listItems: HeroListItem[] = [];
for (let i = 0; i < paragraphs.items.length; ++i) {
let item = paragraphs.items[i];
context.load(item, ['text', 'style', 'styleBuiltIn']);
// Synchronize the document state by executing the queued commands
await context.sync();
if (item.style === 'Heading 1' || item.style === 'Heading 2') {
listItems.push({
primaryText: item.text + ' (' + item.style + ')'
});
if (item.text === 'Late days') {
let newLineItem = item.getNextOrNullObject();
context.load(item, ['text', 'style', 'styleBuiltIn']);
newLineItem.insertParagraph('<<<<<<<<<<<<<<My inserted text>>>>>>>>>>>>>>', 'After');
}
}
}
this.setState({listItems: listItems});
} catch (error) {
this.setState({listItems: [{primaryText: 'error:' + error}]});
}
});
【问题讨论】:
标签: office365 office-js word-addins