【发布时间】:2016-10-28 01:49:42
【问题描述】:
在我的 Word 加载项中,我通过将所选文本替换为包含注释的 OOXML 来插入 cmets。
打开“跟踪更改”后,Word 会将其注册为 3 个操作:删除+插入+注释。它甚至插入了一个分节符,但我不确定这是否相关。
有没有办法只将其注册为评论操作,例如使用 Word 功能插入评论时?
使用rangeObject.insertOoxml 我尝试在字符串的开头和结尾插入,但没有任何运气,因为两个 OOXML 插入似乎不相关(这是有道理的):
Word.run(function (context) {
var range = context.document.getSelection();
var preBody = '<?xml version="1.0" encoding="UTF-8"?><pkg:package xmlns:pkg="http://schemas.microsoft.com/office/2006/xmlPackage"><pkg:part pkg:name="/_rels/.rels" pkg:contentType="application/vnd.openxmlformats-package.relationships+xml" pkg:padding="512"><pkg:xmlData><Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="word/document.xml" /></Relationships></pkg:xmlData></pkg:part><pkg:part pkg:name="/word/_rels/document.xml.rels" pkg:contentType="application/vnd.openxmlformats-package.relationships+xml" pkg:padding="256"><pkg:xmlData><Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments" Target="comments.xml" /></Relationships></pkg:xmlData></pkg:part><pkg:part pkg:name="/word/document.xml" pkg:contentType="application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml"><pkg:xmlData><w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"><w:body><w:p>';
var postBody = '</w:p></w:body></w:document></pkg:xmlData></pkg:part><pkg:part pkg:name="/word/comments.xml" pkg:contentType="application/vnd.openxmlformats-officedocument.wordprocessingml.comments+xml"><pkg:xmlData><w:comments xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"><w:comment w:id="0" w:author="Some User" w:date="2016-10-26T10:11:05" w:initials="SU"><w:p><w:r><w:t>My comment</w:t></w:r></w:p></w:comment></w:comments></pkg:xmlData></pkg:part></pkg:package>';
var before = preBody + '<w:commentRangeStart w:id="0" />' + postBody;
var afterBody = '<w:commentRangeEnd w:id="0" /><w:r><w:commentReference w:id="0" /></w:r>';
var after = preBody + afterBody + postBody;
range.insertOoxml(before, Word.InsertLocation.start);
range.insertOoxml(after, Word.InsertLocation.end);
return context.sync().then(function () {
console.log('OOXML added to the beginning and the end of the range.');
});
})
.catch(function (error) {
console.log('Error: ' + JSON.stringify(error));
if (error instanceof OfficeExtension.Error) {
console.log('Debug info: ' + JSON.stringify(error.debugInfo));
}
});
【问题讨论】: