您可以在MS Word中以OOXML注入的注释中以编程方式创建HTML链接吗?

时间:2017-06-23 15:30:11

标签: xml openxml docx office-js office-addins

更新问题更具体......

我可以使用几种不同的策略(请参阅Insert OOXML comment with track changes)或下面的代码(传递comment.xml的文件位置和'msg'或以下代码)将自定义注释插入到Word文档中评论待插入。)

但我无法看到如何在OOXML中包含可点击链接。

我可以显示纯文本链接,如果它们是'msg'的一部分......但是传递HTML会破坏XML并引发错误。

Word注释中的链接实际存储在comment.xml.rels中,并且仅在实际注释的XML中引用...因此您可能必须将数据附加到comment.xml.rels以便以编程方式创建链接。

任何人都处理过此问题,并且能够包含可点击的链接吗?

令人沮丧的是......一旦出现包含链接的纯文本注释,您只需在链接后键入一个空格即可点击。我试图避免那个手册(和丑陋的)步骤。

function insertComment2(fileName, msg) {

  var selectedText = "";
  var searchText = "";
  var replaceText = "";
  var myXML = "";

   Office.context.document.getSelectedDataAsync(
 Office.CoercionType.Text,function (result) {
   if (result.status == Office.AsyncResultStatus.Failed) {
       console.log('Action failed. Error: ' + result.error.message);
   }
   else {
      selectedText = result.value;
      console.log('Selected data: ' + selectedText);
      var myOOXMLRequest = new XMLHttpRequest();
      myOOXMLRequest.open('GET', fileName, false);
      myOOXMLRequest.send();
      if (myOOXMLRequest.status === 200) {
          myXML = myOOXMLRequest.responseText;
          console.log("XML before insertion: " + myXML);
      }
      // selectedText = getDataFromSelection();
      var searchRangeTxt = "<w:r><w:t xml:space=\"preserve\">selectedText</w:t></w:r>"
      var replaceRangeTxt = "<w:r><w:t xml:space=\"preserve\">" + selectedText + "</w:t></w:r>"

      myXML = myXML.replace(searchRangeTxt, replaceRangeTxt);

      msg = "<![CDATA[" + msg + "]]>";

      var searchText = "<w:p><w:r><w:t>comment</w:t></w:r></w:p>";
      var replaceText = "<w:p><w:r><w:t>" + msg + "</w:t></w:r></w:p>";
      myXML = myXML.replace(searchText, replaceText);

      // var timeStamp = Date().toLocaleString();
      var currentdate = new Date();
      var timeStamp =    currentdate.getFullYear() + "-"
                      + ("0" + (currentdate.getMonth() + 1)).slice(-2)  + "-"
                      + ("0" + currentdate.getDate()).slice(-2) + "T"
                      + currentdate.getHours() + ":"
                      + currentdate.getMinutes() + ":"
                      + currentdate.getSeconds() + "Z";

      console.log('Trying to get time and think it is: ' + timeStamp);

      var searchDate = "<w:comment w:id=\"0\" w:author=\"Proofreading International\" w:date=\"DATE\" w:initials=\"PRI\">";
      var replaceDate = "<w:comment w:id=\"0\" w:author=\"Proofreading International\" w:date=\"" + timeStamp + "\" w:initials=\"PRI\">";

      myXML = myXML.replace(searchDate, replaceDate);

      Office.context.document.setSelectedDataAsync(myXML, { coercionType: Office.CoercionType.Ooxml }, function (asyncResult) {
               if (asyncResult.status == "failed") {
                   console.log("Action failed with error: " + asyncResult.error.message);
                   console.log('Failure in inserting XML!' + myXML);
               }
               else {
                 console.log('success in inserting XML!' + myXML);
               }
           });
       }
  });

}

更新...

插入以下'comment.xml'文件在注释中创建一个可扩展的超链接...但它是一个空的超链接,并且不会获取该部分的ID引用...

使用普通/空Word文档,插入注释不会创建comments.xml.rels文件。只有在手动创建带链接的注释时才会创建该文件。

<?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/comments.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="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink" Target="http://www.google.com/" TargetMode="External"/>
            </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 xml:space="preserve">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="Proofreading International" w:date="DATE" w:initials="PRI">
                  <w:p>
                  <w:r>
                    <w:t>
                      New comment
                    </w:t>
                  </w:r>
                  <w:hyperlink Id="rId2" w:history="1">
                    <w:r>
                    <w:rPr>
                    <w:rStyle w:val="Hyperlink"/>
                    </w:rPr>
                    <w:t>Google</w:t>
                    </w:r>
                  </w:hyperlink>
                </w:p>
                </w:comment>
            </w:comments>
        </pkg:xmlData>
    </pkg:part>
</pkg:package>

1 个答案:

答案 0 :(得分:0)

我建议你做的是:

  1. 手动插入带有链接的评论,就像您想要的那样。
  2. 选择包含评论的范围,然后使用API​​获取与之关联的OOXML。以下是如何执行此操作的示例:
  3. var myOOXML="";
         function getOOXML() {
            Office.context.document.getSelectedDataAsync("ooxml", {}, function (result) {
                myOOXML = result.value;
                document.getElementById("txtTest").value = result.value
            })
    
    
         }

    1. 抓住OOXML并尝试使用tips on this article将其最小化。还要查找文本和网址,以便用您需要的内容替换它。

    2. 最后,既然你知道需要替换的文本,请确保使用与此类似的代码在任何需要的地方插入OOXML。

    3.   Word.run(function (ctx) {
      
                   var myTempOOXML = document.getElementById("txtTest").value;
                   //replaces the selected range with OOXML
                   ctx.document.getSelection().insertOoxml(myTempOOXML, "replace");
                   return ctx.sync();
      
      
               }).catch(function (e) { app.showNotification(e.message)})
               

      希望这会让你走上正确的道路...... 谢谢! 涓。