如何使用 Word JS API 删除插入的 OOXML 注释? [英] How to delete an inserted OOXML comment using Word JS API?

查看:34
本文介绍了如何使用 Word JS API 删除插入的 OOXML 注释?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 insertOoxml 方法在 Word 文档中插入注释.评论插入成功.

I am trying to insert comments inside word document using insertOoxml method. The comment gets inserted successfully.

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

I want to delete this manually-inserted comment based on one of the user's actions. For example, when they switch from one feature of my add-in to another. I am trying to delete the comment parts from the Ooxml string using regex match and replace for this to work.

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 对象,所以只想确认

It throws a GeneralException error. I think I'm corrupting the XML object somewhere, so just wanted to confirm

一个.这是解决我的问题的正确方法/解决方法吗?
湾我在这里缺少什么来替换?
C.有没有其他复杂的解决方案?

a. Is this a right approach/workaround to my problem?
b. What am I missing to replace here?
c. Is there any other sophisticated solution possible?

推荐答案

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

The regex method is not safe. The approach is still the same. Use XML parser and delete the relevant nodes from the XML DOM tree.

代码示例:

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)

这篇关于如何使用 Word JS API 删除插入的 OOXML 注释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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