Skip to content

Instantly share code, notes, and snippets.

@morgan-tr
Last active October 30, 2019 17:23
Show Gist options
  • Save morgan-tr/cee3978a5b977dbac8792b1e2e6a0aaa to your computer and use it in GitHub Desktop.
Save morgan-tr/cee3978a5b977dbac8792b1e2e6a0aaa to your computer and use it in GitHub Desktop.
Error resetting font highlight color via WordApi 1.1
name: Reset highlights error
description: 'Demonstrates an error with resetting highlights'
host: WORD
api_set: {}
script:
content: |
$("#reset-highlights").click(() => tryCatch(resetHighlights));
$("#setup").click(() => tryCatch(setup));
async function resetHighlights() {
await Word.run(async (context) => {
try {
context.document.body.paragraphs.load("font");
await context.sync();
context.document.body.paragraphs.items.map((paragraph) => {
paragraph.font.set({ highlightColor: null });
});
} catch (error) {
console.log("Error", error);
}
});
}
/** 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);
}
}
async function setup() {
await Word.run(async (context) => {
context.document.body.clear();
context.document.body.insertParagraph("One more paragraph. ", "Start");
context.document.body.insertParagraph("", "Start");
context.document.body.insertParagraph("Inserting another paragraph. ", "Start");
context.document.body.insertParagraph("", "Start");
context.document.body.insertParagraph(
"Video provides a powerful way to help you prove your point. When you click Online Video, you can paste in the embed code for the video you want to add. You can also type a keyword to search online for the video that best fits your document.",
"Start"
);
context.document.body.paragraphs
.getLast()
.insertText(
"To make your document look professionally produced, Word provides header, footer, cover page, and text box designs that complement each other. For example, you can add a matching cover page, header, and sidebar. Click Insert and then choose the elements you want from the different galleries. ",
"Replace"
);
context.document.body.paragraphs.load("font");
await context.sync();
context.document.body.paragraphs.items.map((paragraph) => {
paragraph.font.set({ highlightColor: 'Yellow' });
});
});
}
/** 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\">\n\tThis sample demonstrates that reset highlights doesn't work in WordApi 1.1.\n</section>\n\n<section class=\"setup ms-font-m\">\n\t<h3>Set up</h3>\n\t<button id=\"setup\" class=\"ms-Button\">\n <span class=\"ms-Button-label\">Setup</span>\n </button>\n</section>\n\n<section class=\"samples ms-font-m\">\n\t<h3>Try it out</h3>\n\t<span class=\"ms-font-m\">Reset paragraph highlights</span>\n\t<button id=\"reset-highlights\" class=\"ms-Button\">\n <span class=\"ms-Button-label\">Reset highlights</span>\n </button>\n</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