如何使用Word JS API删除插入的OOXML注释?

时间:2017-06-28 13:47:01

标签: javascript xml ms-word openxml office-js

我正在尝试使用insertOoxml方法在word文档中插入注释。注释成功插入。

我想根据用户的某个操作删除此手动插入的评论。例如,当他们从我的加载项的一个功能切换到另一个功能时。我试图使用正则表达式匹配从Ooxml字符串中删除注释部分,并替换为此工作。

Word.run(async(context) => {
  let body = context.document.body
  let bodyOOXML = body.getOoxml()
  await context.sync()
  let bodyOOXMLText = bodyOOXML.value

  bodyOOXMLText = bodyOOXMLText.replace(/<relationship(.*?)target="comments.xml(.*?)comments">/g, '')
  bodyOOXMLText = bodyOOXMLText.replace(/<w:commentRangeStart(.*?)w:commentRangeEnd(.*?)\/>/g, '')
  bodyOOXMLText = bodyOOXMLText.replace(/<w:comments(.*?)w:comments>/g, '')
  bodyOOXMLText = bodyOOXMLText.replace(/<pkg:part(.*?)comments\+xml(.*?)word\/comments\.xml">(.*?)<\/pkg:part>/g, '')

  body.insertOoxml(bodyOOXMLText, Word.InsertLocation.replace)
  await context.sync()

})

它会引发GeneralException错误。我想我在某个地方破坏了XML对象,所以只想确认

一个。这是我的问题的正确方法/解决方法吗? 湾我在这里更换的是什么? C。还有其他复杂的解决方案吗?

1 个答案:

答案 0 :(得分:0)

正则表达式方法不安全。方法仍然是一样的。使用XML解析器并从XML DOM树中删除相关节点。

代码示例:

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
}

现在可以使用
来插入修改后的XML字符串 body.insertOoxml(xmlText, Word.InsertLocation.replace)