Skip to content

Instantly share code, notes, and snippets.

@sergiubucur
Created July 8, 2019 14:50
Show Gist options
  • Save sergiubucur/b0315ffb38b519fd3b62366330f4bcd3 to your computer and use it in GitHub Desktop.
Save sergiubucur/b0315ffb38b519fd3b62366330f4bcd3 to your computer and use it in GitHub Desktop.
Delete content control
name: Delete content control
description: Delete content control
host: WORD
api_set: {}
script:
content: |
$("#copy").click(() => tryCatch(retrieveXML));
$("#insert").click(() => tryCatch(insert));
$("#remove").click(() => tryCatch(remove));
const Tag = "TEST";
let xmlContent = null;
function retrieveXML() {
return new Promise((resolve, reject) => {
Word.run((context) => {
const range = context.document.getSelection();
context.load(range, "text, inlinePictures/hyperlink");
return context.sync().then(() => {
xmlContent = range.getOoxml();
return context.sync().then(() => {
console.log("content copied");
resolve();
});
});
});
});
}
function insert() {
Word.run((context) => {
const range = context.document.getSelection();
const ctrl = range.insertContentControl();
ctrl.tag = Tag;
ctrl.appearance = "boundingBox";
ctrl.insertOoxml(xmlContent.value, "Replace");
return context.sync();
});
}
function remove() {
Word.run((context) => {
const contentControls = context.document.contentControls.getByTag(Tag);
contentControls.load("text");
return context.sync().then(() => {
if (contentControls.items.length > 0) {
const ctrl = contentControls.items[0];
const range = ctrl.getRange("Content");
range.select("Start");
ctrl.delete(false);
return context.sync();
}
});
});
}
/** 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="copy" class="ms-Button">
<span class="ms-Button-label">Copy</span>
</button>
<button id="insert" class="ms-Button">
<span class="ms-Button-label">Insert</span>
</button>
<button id="remove" class="ms-Button">
<span class="ms-Button-label">Remove</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