使用 office.js 在 Word 中插入评论

Insert comment in Word using office.js

我正在尝试在 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>

非常抱歉 post。遗憾的是新用户被限制插入链接和图片:(

这实际上是 API 中已确认的错误。此问题的修复将作为即将推出的 Office 更新的一部分推出。