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

标签 ms-word office365 openxml office-js

我正在尝试在 office.js 中制作一个 Word 插件,用于在文档中插入评论。在我看来,实现这一目标的唯一方法是使用 OOXML。

我可以插入评论,但我的问题是,当我这样做时,插入了一个段落分隔符,从这张图片中可以看出。

enter image description here

据我所知,归结为如果我只插入一些文本,正文的内容看起来像下面这样工作正常

<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>

抱歉这篇文章特别长。遗憾的是新用户被限制插入链接和图片:(

最佳答案

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

关于ms-word - 使用 office.js 在 Word 中插入评论,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36892507/

相关文章:

ms-word - 如何从 Word 2007 文档链接到 chm 文件主题?

javascript - SCRIPT5022 : The collection has not been initialized. 尚未请求或请求尚未执行

Powershell交换模块远程邮箱问题

c# - 如何使用 C# 和 Open XML SDK 按部分拆分 Word 文档?

openxml - 在哪里可以了解有关 Excel 专用 OpenXML 元素的更多信息?

c# - Openxml - 锁定所有内容控件

vba - 用于删除 Word 文档中所有重复文本实例的宏

vba - 应用于 Word 修订版的更改使两个段落合二为一

c# - Word Interop Delete 导致参数错误

python - 使用带有 365 电子邮件地址的 SMTPLIB SSL 电子邮件时出错