如何使用Word JS API删除插入的OOXML注释? [英] How to delete an inserted OOXML comment using Word JS API?
问题描述
我正在尝试使用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
a.这是解决我问题的正确方法/解决方法吗?
b.我想在这里替换什么吗?
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)
the modified XML string can now be inserted using
body.insertOoxml(xmlText, Word.InsertLocation.replace)
这篇关于如何使用Word JS API删除插入的OOXML注释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!