0
如何列出标题(标题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}]});
}
});
谢谢瑞克!题外话题 - 我应该为数组中的每个元素逐个执行context.sync(),还是应该先加载所有需要的元素,然后再执行一次context.sync? – ZakiMa