首页 > 解决方案 > OfficeJs Word 删除作者的评论

问题描述

我正在尝试修改此处找到的代码段如何使用 Word JS API 删除插入的 OOXML 注释?删除特定作者的评论。到目前为止,该片段可以删除文档中的所有评论。知道实现这一目标的最佳方法是什么吗?

  export function removeCommentsFromXML(xmlString){
  let xmlText = ''
  try{

    // initialize DOM parser
    let parser = new DOMParser()
    let namespace = []

    // parse XML string into XML DOM object
    let xmlDoc = parser.parseFromString(xmlString, 'text/xml')

    // get xml namespace prefix for 'pkg'
    namespace['pkg'] = xmlDoc.documentElement.getAttribute('xmlns:pkg')

    // get all 'pkg:part' nodes
    let allChildrenNodes = xmlDoc.getElementsByTagNameNS(namespace['pkg'], 'part')

    // delete comments.xml node in pkg:part
    let currentChildNode = allChildrenNodes[0]
    while (currentChildNode!==null && currentChildNode.getAttribute('pkg:name').match('comments.xml')===null) {
      currentChildNode = currentChildNode.nextSibling
    }
    if(currentChildNode!==null) currentChildNode.parentNode.removeChild(currentChildNode)

    // get document relationship package
    currentChildNode = allChildrenNodes[0]
    while (currentChildNode!==null && currentChildNode.getAttribute('pkg:name').match('word/_rels')===null) {
      currentChildNode = currentChildNode.nextSibling
    }

    // get all relationships
    let relationships = currentChildNode.getElementsByTagName('Relationship')

    // delete comment relationship from relationships
    let currentRelationship = relationships[0]
    while (currentRelationship!==null && currentRelationship.getAttribute('Target').match('comments.xml')===null) {
      currentRelationship = currentRelationship.nextSibling
    }
    if(currentRelationship!==null) currentRelationship.parentNode.removeChild(currentRelationship)

    // get main document
    currentChildNode = allChildrenNodes[0]
    while (currentChildNode!==null && currentChildNode.getAttribute('pkg:name').match('/word/document.xml')===null) {
      currentChildNode = currentChildNode.nextSibling
    }

    // get w namespace
    namespace['w'] = currentChildNode.childNodes[0].childNodes[0].getAttribute('xmlns:w')

    // get commentRangeStart nodes
    let commentRangeStartNodes = currentChildNode.getElementsByTagNameNS(namespace['w'], 'commentRangeStart')
    while(commentRangeStartNodes.length>0) {
      commentRangeStartNodes[0].parentNode.removeChild(commentRangeStartNodes[0])
    }

    // get commentReference nodes
    let commentReferenceNodes = currentChildNode.getElementsByTagNameNS(namespace['w'], 'commentReference')
    while(commentReferenceNodes.length>0) {
      commentReferenceNodes[0].parentNode.removeChild(commentReferenceNodes[0])
    }

    // get commentRangeEnd nodes
    let commentRangeEndNodes = currentChildNode.getElementsByTagNameNS(namespace['w'], 'commentRangeEnd')
    while(commentRangeEndNodes.length>0) {
      commentRangeEndNodes[0].parentNode.removeChild(commentRangeEndNodes[0])
    }

    xmlText = new XMLSerializer().serializeToString(xmlDoc)
  }
  catch(err){
    console.log(err)
  }

  return xmlText
}

评论如下所示:

export const 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>markedWordLocation</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="AUTHOR_TO_BE_REPLACED" w:date="2016-04-27T08:15:00Z" w:initials="PO">
                    <w:p>
                        <w:r>
                            <w:t>feedbackOnWord</w:t>
                        </w:r>
                    </w:p>
                </w:comment>
            </w:comments>
        </pkg:xmlData>
    </pkg:part>
</pkg:package>`;

标签: xmlms-wordoffice365office-jsopenxml

解决方案


推荐阅读