通过Office Javascript API 1.1发表评论

rzo*_*rzo 15 javascript ms-office office365 apps-for-office

我的任务是通过Office Javascript API在与Word 2007或更高版本兼容的Word文档(.docx)中发表评论.

我发现,没有直接的方法可以通过Microsoft API执行此操作.

由于我能够将OOXML传递给Word文档,我认为我可以使用它来发表评论.

我对Word文档结构进行了一些研究,发现注释存储在一个名为"comments.xml"的单独XML文件中,然后通过"document.xml"中的ID引用(我附上了相应的示例).

有没有编辑这个方式comments.xml通过API,以便将Word文档中的注释,或者这是不是可能呢?

示例"document.xml":

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:document
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
    mc:Ignorable="w14 wp14">
<w:body>
    <w:p w:rsidR="00A9590C" w:rsidRDefault="0058668B">
        <w:r>
            <w:t>I am text.</w:t>
        </w:r>
    </w:p>
    <w:p w:rsidR="0058668B" w:rsidRDefault="0058668B">
        <w:commentRangeStart w:id="0"/>
        <w:r>
            <w:t>I am text with comment.</w:t>
        </w:r>
        <w:commentRangeEnd w:id="0"/>
        <w:r>
            <w:rPr>
                <w:rStyle w:val="Kommentarzeichen"/>
            </w:rPr>
            <w:commentReference w:id="0"/>
        </w:r>
    </w:p>
    <w:sectPr w:rsidR="0058668B">
        <w:pgSz w:w="11906" w:h="16838"/>
        <w:pgMar w:top="1417" w:right="1417" w:bottom="1134" w:left="1417" w:header="708" w:footer="708"
                 w:gutter="0"/>
        <w:cols w:space="708"/>
        <w:docGrid w:linePitch="360"/>
    </w:sectPr>
</w:body>
Run Code Online (Sandbox Code Playgroud)

示例"comments.xml":

 <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <w:comments
   xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
   xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
  mc:Ignorable="w14 wp14">
 <w:comment w:id="0" w:author="rz" w:date="2015-05-23T10:30:00Z" w:initials="r">
<w:p w:rsidR="0058668B" w:rsidRDefault="0058668B">
    <w:pPr>
        <w:pStyle w:val="Kommentartext"/>
    </w:pPr>
    <w:r>
        <w:rPr>
            <w:rStyle w:val="Kommentarzeichen"/>
        </w:rPr>
        <w:annotationRef/>
    </w:r>
    <w:r>
        <w:t>Test</w:t>
    </w:r>
    <w:bookmarkStart w:id="1" w:name="_GoBack"/>
    <w:bookmarkEnd w:id="1"/>
</w:p>
 </w:comment>
</w:comments>
Run Code Online (Sandbox Code Playgroud)

Kyl*_*ett 2

这是我在与您一起研究时找到的最多信息:

https://msdn.microsoft.com/en-us/magazine/jj991976.aspx

注意:了解如何从应用程序操作 OOXML 的一种好方法是使用 UI 添加要使用的内容(例如,通过单击插入 | 插图 | SmartArt 插入 SmartArt),获取内容的 OOXML使用 getSelectedDataAsync 然后读取结果。

我会这样做,然后使用 setSelectedDataAsync 将这些结果作为 ooxml 发送,这将回答您的问题。Office 是否足够智能,可以自行创建这些引用?(如果没有,您无法通过 api 执行任何操作)

旧评论(我发现你的第一个前提是正确的。请忽略,或阅读以笑)

看起来您可以使用 setSelectedDataAsync 函数传入注释值并将其应用到 Word 文档中当前选定的内容。以下是两个最相关的片段:

 Office.context.document.setSelectedDataAsync(data [, options], callback(asyncResult));
Run Code Online (Sandbox Code Playgroud)

 Office.CustomXMLNodeType.NodeComment   "comment"   The node is a comment.
Run Code Online (Sandbox Code Playgroud)

从微软的一个示例中使用的 coerciontype 的实现以及 coerciontype 是一个枚举这一事实来看,就像 customexmlnodetype 一样......对我来说,这可行。

 function writeMatrix() {
     Office.context.document.setSelectedDataAsync("test comment"], {CustomXMLNodeType: Office.Office.CustomXMLNodeType.NodeComment}
    function (asyncResult) {
        var error = asyncResult.error;
        if (asyncResult.status === Office.AsyncResultStatus.Failed){
            write(error.name + ": " + error.message);
        }
    });
Run Code Online (Sandbox Code Playgroud)

}

回顾文档,我现在发现 coercionType 作为 object 下的可选参数,而不是所有枚举。那就太蠢了!!

以下是我参考的资料:

看看这个:https://msdn.microsoft.com/en-us/library/office/fp142145.aspx

这是:https://msdn.microsoft.com/EN-US/library/office/fp142154.aspx