我正在尝试在 office.js 中制作一个 Word 插件,以在文档中插入注释。在我看来,实现这一目标的唯一方法是使用 OOXML。
我可以插入评论,但我的问题是,当我这样做时,会插入一个段落分隔符,并且可以从该图像中看到。
据我所知,归结为,如果我只插入一些文本,正文的内容如下所示,效果很好
<w:p>
<w:r>
<w:t>Some text</w:t>
</w:r>
</w:p>
但是,如果我插入对评论的引用,则会导致段落在我插入的内容之后结束。在这种情况下,正文的内容如下所示:
<w:p>
<w:commentRangeStart w:id="0"/>
<w:r>
<w:t>selectedText</w:t>
</w:r>
<w:r>
<w:commentReference w:id="0"/>
</w:r>
<w:commentRangeEnd w:id="0"/>
</w:p>
用于替换突出显示文本的 JavaScript 代码如下所示:
function insertComment() {
Office.context.document.getSelectedDataAsync(
Office.CoercionType.Text,
function (result) {
if (result.status == "succeeded") {
// Get the OOXML returned from the getSelectedDataAsync call.
var selectedText = result.value;
var comment = getCommentAsOoxml(selectedText);
Office.context.document.setSelectedDataAsync(comment, { coercionType: Office.CoercionType.Ooxml }, function (asyncResult) {
if (asyncResult.status == "failed") {
console.debug("Action failed with error: " + asyncResult.error.message);
}
});
}
});
}
可以在此处看到插入的 OOXML:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<?mso-application progid="Word.Document"?>
<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>
<w:commentRangeStart w:id="0"/>
<w:r>
<w:t>selectedText</w:t>
</w:r>
<w:r>
<w:commentReference w:id="0"/>
</w:r>
<w:commentRangeEnd w:id="0"/>
</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">
<w:comment w:id="0" w:author="jkh" w:date="2016-04-27T08:15:00Z" w:initials="JH">
<w:p>
<w:r>
<w:t>comment</w:t>
</w:r>
</w:p>
</w:comment>
</w:comments>
</pkg:xmlData>
</pkg:part>
</pkg:package>
很抱歉这篇文章特别长。遗憾的是,新用户在插入链接和图像方面受到限制:(