Skip to content

Instantly share code, notes, and snippets.

@barisbikmaz
Last active April 4, 2023 08:22
Show Gist options
  • Save barisbikmaz/2fdd7c03f205557cade6e3d29511a0f6 to your computer and use it in GitHub Desktop.
Save barisbikmaz/2fdd7c03f205557cade6e3d29511a0f6 to your computer and use it in GitHub Desktop.
name: Style API - Removing Style
description: ''
host: WORD
api_set: {}
script:
content: |
/**
* USE CASE:
* Removing Style from the current document
*
* Works as expected.
* But the style is not deleted instantly from the list and in the UI until I
* manually refresh the styles lint.
*/
$("#remove-style").click(() => tryCatch(removeStyle));
function targetDocumentStyleName() {
return $("input[name=removeStyleName]").val() as string;
}
async function removeStyle() {
const styleName = targetDocumentStyleName();
console.log(`⚙️ Removing ${styleName} ...`);
await Word.run(async (context) => {
const styles = context.document.getStyles();
const style = styles.getByNameOrNullObject(styleName);
style.delete();
await context.sync();
console.log("✔️ Style removed!");
});
}
/** 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=\"samples ms-font-m\">\n\t<h3>\U0001FA84 Removing a Style from document</h3>\n\n\t<div class=\"ms-TextField\">\n\t\t<label class=\"ms-Label\">Style Name</label>\n\t\t<input name=\"removeStyleName\" class=\"ms-TextField-field\" type=\"text\" value=\"ofaw\" />\n\t</div>\n\n\t\t<button id=\"remove-style\" class=\"ms-Button\">\n\t\t\t<span class=\"ms-Button-label\">➖ Remove </span>\n\t</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/beta/hosted/office.js
@types/office-js-preview
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
lodash@4.17.15
@types/lodash
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment