Created
August 22, 2022 09:15
-
-
Save richie5um/e6288d02db0c566c5c5d9cb66cc3407d to your computer and use it in GitHub Desktop.
Create a new snippet from a blank template.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Smz-CommentRemoval | |
description: Create a new snippet from a blank template. | |
host: WORD | |
api_set: {} | |
script: | |
content: | | |
$("#run").click(() => tryCatch(run)); | |
async 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; | |
} | |
async function run() { | |
$("#ooxml").val("Working"); | |
await Word.run(async (context) => { | |
Office.context.document.getSelectedDataAsync(Office.CoercionType.Ooxml, async function(result) { | |
if (result.status == "succeeded") { | |
var ooxml = result.value; | |
var updatedOoxml = await removeCommentsFromXML(ooxml); | |
var ooxmlFriendly = updatedOoxml.replace(/\>\</gi, ">\n<"); | |
$("#ooxml").val(ooxmlFriendly); | |
// $("#ooxml").val("done"); | |
console.log(ooxmlFriendly); | |
Office.context.document.setSelectedDataAsync(updatedOoxml, { coercionType: Office.CoercionType.Ooxml }, function (result) { | |
if (result.status === "succeeded") { | |
console.log("Set SelectedDataAsync"); | |
} else if (result.status == "failed") { | |
console.error("Action failed with error: " + result.error.message); | |
} | |
}); | |
} | |
}); | |
}); | |
} | |
/** Default helper for invoking an action and handling errors. */ | |
async function tryCatch(callback) { | |
try { | |
await callback(); | |
} catch (error) { | |
// Note: In a production add-in, you'd want to notify the user through your add-in's UI. | |
console.error(error); | |
} | |
} | |
language: typescript | |
template: | |
content: "<div>\n\t<button id=\"run\" class=\"ms-Button\">\n <span class=\"ms-Button-label\">Run</span> \n </button>\n</div>\n<textarea id=\"ooxml\" style=\"width: 100%; height: 300px;\"></textarea>" | |
language: html | |
style: | |
content: |- | |
section.samples { | |
margin-top: 20px; | |
} | |
section.samples .ms-Button, section.setup .ms-Button { | |
display: block; | |
margin-bottom: 5px; | |
margin-left: 20px; | |
min-width: 80px; | |
} | |
language: css | |
libraries: | | |
https://appsforoffice.microsoft.com/lib/1/hosted/office.js | |
@types/office-js | |
[email protected]/dist/css/fabric.min.css | |
[email protected]/dist/css/fabric.components.min.css | |
[email protected]/client/core.min.js | |
@types/core-js | |
[email protected] | |
@types/[email protected] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment