Created
July 6, 2020 20:25
-
-
Save algomaster99/52f08d3360119d47a850e1a08cba90a8 to your computer and use it in GitHub Desktop.
Create a new snippet from a blank template.
This file contains 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: CustomXML | |
description: Create a new snippet from a blank template. | |
host: WORD | |
api_set: {} | |
script: | |
content: | | |
$("#add-xml").click(() => tryCatch(run)); | |
$("#get-node").click(() => tryCatch(getNode)); | |
$("#delete-xml").click(() => tryCatch(deleteXml)); | |
async function run() { | |
const xml = `<AP xmlns="accordproject.org"><template xmlns="acceptance-of-delivery"><shipper>Aman Sharma</shipper></template></AP>`; | |
await Word.run(async context => { | |
const customXmlParts = context.document.customXmlParts; | |
customXmlParts.add(xml); | |
}) | |
} | |
async function getNode() { | |
await Word.run(async context => { | |
const customXmlParts = context.document.customXmlParts; | |
const AP = customXmlParts.getByNamespace("accordproject.org").getOnlyItemOrNullObject(); | |
await context.sync(); | |
const nodes = AP.query('/AP/template', {}); // how does this work? | |
await context.sync(); | |
console.log(nodes); | |
}) | |
} | |
async function deleteXml() { | |
Office.context.document.customXmlParts.getByNamespaceAsync("accordproject.org", (res) => { | |
if (res.status === Office.AsyncResultStatus.Succeeded) { | |
for (let item of res.value) { | |
item.deleteAsync(); | |
} | |
} | |
}); | |
} | |
/** 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: |- | |
<button id="add-xml" class="ms-Button"> | |
<span class="ms-Button-label">Add XML</span> | |
</button> | |
<button id="get-node" class="ms-Button"> | |
<span class="ms-Button-label">Get XML Node</span> | |
</button> | |
<button id="delete-xml" class="ms-Button"> | |
<span class="ms-Button-label">Delete XML</span> | |
</button> | |
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/beta/hosted/office.js | |
@types/office-js-preview | |
office-ui-fabric-js@1.4.0/dist/css/fabric.min.css | |
office-ui-fabric-js@1.4.0/dist/css/fabric.components.min.css | |
core-js@2.4.1/client/core.min.js | |
@types/core-js | |
jquery@3.1.1 | |
@types/jquery@3.3.1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment