使用office.js在Word中插入评论 [英] Insert comment in Word using office.js

查看:100
本文介绍了使用office.js在Word中插入评论的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在office.js中制作一个Word插件,以便在文档中插入注释.在我看来,实现此目标的唯一方法是使用OOXML.

I am trying to make a Word add-in in office.js that inserts comments in the document. It seems to me the only way to achieve this is to use OOXML.

我可以插入评论,但是我的问题是,当我插入评论时,也可以从该图像中看到.

I can insert the comment but my problem is that when I do so a paragraph break is inserted as well as can be seen from this image.

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

A far as I can see it boils down to that if I am only inserting some text the content of the body looks like the following which works fine

<w:p>
    <w:r>
        <w:t>Some text</w:t>
    </w:r>
</w:p>

但是,如果我插入对注释的引用,则会在我插入的内容之后立即导致段落结尾.在这种情况下,身体的内容看起来像这样:

But if I am inserting a reference to a comment it results in a paragraph end right after whatever I am insering. In that case the content of the body looks like this:

<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代码如下:

The javascript code used to replace the highlighted text looks like this:

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可以在这里看到:

The OOXML that is inserted can be seen here:

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

抱歉,帖子过长.遗憾的是,新用户在插入链接和图像时受到限制:(

Sorry for the exceptionally long post. A new user is regretfully restricted in inserting links and images :(

推荐答案

这实际上是API中已确认的错误.此修复程序将在即将进行的Office更新中推出.

this is actually a confirmed bug in the API. A fix for this will be rolled out as part of an upcoming Office update.

这篇关于使用office.js在Word中插入评论的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆