Skip to content

Instantly share code, notes, and snippets.

@algomaster99
Last active July 6, 2020 20:07
Show Gist options
  • Select an option

  • Save algomaster99/fc10bc8fcf3429f1d3ad1fbadaa3216f to your computer and use it in GitHub Desktop.

Select an option

Save algomaster99/fc10bc8fcf3429f1d3ad1fbadaa3216f to your computer and use it in GitHub Desktop.
Create a new snippet from a blank template.
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>`;
Office.context.document.customXmlParts.addAsync(xml, (res) => {
if (res.status === Office.AsyncResultStatus.Succeeded) {
console.log("sucessfully added");
}
});
}
async function getNode() {
Office.context.document.customXmlParts.getByNamespaceAsync("accordproject.org", (res) => {
if (res.status === Office.AsyncResultStatus.Succeeded) {
res.value[0].getNodesAsync("AP/template", (nodes) => {
if (nodes.status === Office.AsyncResultStatus.Succeeded) {
console.log(nodes.value);
}
});
}
});
}
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/1/hosted/office.js
@types/office-js
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