Skip to content

Instantly share code, notes, and snippets.

@richie5um
Created August 19, 2021 09:45
Show Gist options
  • Save richie5um/0bc008ade2d09eec1546863cfc31bbd7 to your computer and use it in GitHub Desktop.
Save richie5um/0bc008ade2d09eec1546863cfc31bbd7 to your computer and use it in GitHub Desktop.
Inserts, updates, and retrieves content controls.
name: Content control basics
description: 'Inserts, updates, and retrieves content controls.'
host: WORD
api_set: {}
script:
content: >
$("#output-paragraph-controls").click(() =>
tryCatch(outputParagraphContentControls));
$("#output-selection-controls").click(() =>
tryCatch(outputSelectionContentControls));
$("#output-controls").click(() => tryCatch(outputContentControls));
async function outputContentControls() {
await Word.run(async (context) => {
let contentControls = context.document.contentControls;
await loadContentControls(context, contentControls);
await context.sync();
});
}
async function outputParagraphContentControls() {
await Word.run(async (context) => {
let selection = context.document.getSelection();
context.load(selection.paragraphs, "*");
await context.sync();
let contentControls = selection.paragraphs.getFirst().contentControls;
await loadContentControls(context, contentControls);
await context.sync();
});
}
async function outputSelectionContentControls() {
await Word.run(async (context) => {
let contentControls = context.document.getSelection().contentControls;
await loadContentControls(context, contentControls);
await context.sync();
});
}
async function loadContentControls(ctx, contentControls) {
ctx.load(
contentControls,
"appearance," +
"cannotDelete," +
"cannotEdit," +
"color," +
"id," +
"placeHolderText," +
"removeWhenEdited," +
"title," +
"text," +
"type," +
"style," +
"tag," +
"font/size," +
"font/name," +
"font/color"
);
await ctx.sync();
for (let i = 0; i < contentControls.items.length; i++) {
console.log(
"Property values of the first content control:" +
" ----- appearance: " +
contentControls.items[i].appearance +
" ----- cannotDelete: " +
contentControls.items[i].cannotDelete +
" ----- cannotEdit: " +
contentControls.items[i].cannotEdit +
" ----- color: " +
contentControls.items[i].color +
" ----- id: " +
contentControls.items[i].id +
" ----- placeHolderText: " +
contentControls.items[i].placeholderText +
" ----- removeWhenEdited: " +
contentControls.items[i].removeWhenEdited +
" ----- title: " +
contentControls.items[i].title +
" ----- text: " +
contentControls.items[i].text +
" ----- type: " +
contentControls.items[i].type +
" ----- style: " +
contentControls.items[i].style +
" ----- tag: " +
contentControls.items[i].tag +
" ----- font size: " +
contentControls.items[i].font.size +
" ----- font name: " +
contentControls.items[i].font.name +
" ----- font color: " +
contentControls.items[i].font.color
);
}
}
/** 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: |-
<section class="ms-font-m">
This sample demonstrates an issue with getSelection().contentControls.
</section>
<section class="samples ms-font-m">
<h3>Try it out</h3>
<button id="output-controls" class="ms-Button">
<span class="ms-Button-label">Output content controls</span>
</button>
<button id="output-paragraph-controls" class="ms-Button">
<span class="ms-Button-label">Output Paragraph content controls</span>
</button>
<button id="output-selection-controls" class="ms-Button">
<span class="ms-Button-label">Output Seleciton content controls</span>
</button>
</section>
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