Skip to content

Instantly share code, notes, and snippets.

@NickDarvey
Last active February 16, 2021 13:51
Show Gist options
  • Save NickDarvey/d245054dc6894690d3f28a6ac99036c0 to your computer and use it in GitHub Desktop.
Save NickDarvey/d245054dc6894690d3f28a6ac99036c0 to your computer and use it in GitHub Desktop.
name: Updating customXmlParts
description: Issue
host: WORD
api_set: {}
script:
content: |
$("#run").click(() => tryCatch(run));
async function run() {
await Word.run(async (context) => {
console.log("run----------");
const namespace = "https://schema.example.org/someschema";
const exampleContent1 = '<?xml version="1.0" standalone="no"?><c:Collection xmlns:c="' + namespace + '" xmlns="' + namespace + '"><c:Item>Test 1</c:Item></c:Collection>';
const exampleContent2 = '<?xml version="1.0" standalone="no"?><c:Collection xmlns:c="' + namespace + '" xmlns="' + namespace + '"><c:Item>Test 1</c:Item><c:Item>Test2</c:Item></c:Collection>';
const part = await new Promise<Office.CustomXmlPart>((resolve, reject) =>
Office.context.document.customXmlParts.getByNamespaceAsync(namespace, async x => {
resolve(x.value[0]);
}));
const partDoesNotExists = part === undefined;
if (partDoesNotExists) {
console.log("create new part");
const create = await new Promise<Office.CustomXmlPart>((resolve, reject) =>
Office.context.document.customXmlParts.addAsync(exampleContent1, x => {
console.log(x);
resolve(x.value)
}));
await context.sync();
console.log("created");
const createdResult = await new Promise<string>((resolve, reject) => {
create.getXmlAsync(e => resolve(e.value));
});
console.log(createdResult);
} else {
console.log("amend existing part");
const rootNode = await new Promise<Office.CustomXmlNode>((resolve, reject) => {
part.getNodesAsync("/", x => resolve(x.value[0]));
});
const existing = await new Promise<string>((resolve, reject) => {
rootNode.getXmlAsync(e => resolve(e.value));
});
console.log(existing);
const root = await new Promise<Office.CustomXmlNode>((resolve, reject) => {
part.getNodesAsync("/", x => resolve(x.value[0]));
});
const updated = await new Promise<void>((resolve, reject) => {
//rootNode.setNodeValueAsync(exampleContent2, e=> resolve()); // this line crashes word;
//rootNode.setTextAsync(exampleContent2, e=> resolve()); // this line crashes Word;
root.setXmlAsync(exampleContent2, e => { console.log (e); resolve(); });
});
const updatedResult = await new Promise<string>((resolve, reject) => {
Office.context.document.customXmlParts.getByNamespaceAsync(namespace, async x => {
x.value[0].getXmlAsync(x => resolve(x.value));
});
})
console.log("Updated result: ");
console.log(updatedResult); // is not updated
}
});
}
/** 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="run" class="ms-Button">
<span class="ms-Button-label">Run</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