Skip to content

Instantly share code, notes, and snippets.

@robinaar
Created April 23, 2018 14:15
Show Gist options
  • Save robinaar/571e02437bbd8240b14991d5e61165af to your computer and use it in GitHub Desktop.
Save robinaar/571e02437bbd8240b14991d5e61165af to your computer and use it in GitHub Desktop.
Performs a basic Word API call using plain JavaScript & Promises. - Shared with Script Lab
name: Basic API call (JavaScript)
description: Performs a basic Word API call using plain JavaScript & Promises.
author: robinaar
host: WORD
api_set: {}
script:
content: |-
$("#run").click(() => tryCatch(run));
function run() {
return Word.run(function (context) {
var range = context.document.getSelection();
range.font.color = "blue";
range.load("text");
return context.sync()
.then(function() {
console.log("The selected text was \"" + range.text + "\".");
});
});
}
/** Default helper for invoking an action and handling errors. */
function tryCatch(callback) {
Promise.resolve()
.then(callback)
.catch(function (error) {
OfficeHelpers.UI.notify(error);
OfficeHelpers.Utilities.log(error);
});
}
language: typescript
template:
content: |-
<p class="ms-font-m">Executes a simple code snippet</p>
<button id="run" class="ms-Button">
<span class="ms-Button-label">Run code</span>
</button>
language: html
style:
content: |-
body {
margin: 0;
padding: 10px;
}
/* Button customization, including overwriting some Fabric defaults */
.ms-Button, .ms-Button:focus {
background: #2b579a;
border: #2b579a;
}
.ms-Button > .ms-Button-label,
.ms-Button:focus > .ms-Button-label,
.ms-Button:hover > .ms-Button-label {
color: white;
}
.ms-Button:hover, .ms-Button:active {
background: #204072;
}
.ms-Button.is-disabled, .ms-Button:disabled {
background-color: #f4f4f4;
border-color: #f4f4f4;
}
.ms-Button.is-disabled .ms-Button-label,
.ms-Button:disabled .ms-Button-label {
color: #a6a6a6;
}
language: css
libraries: |
https://appsforoffice.microsoft.com/lib/1/hosted/office.js
https://appsforoffice.microsoft.com/lib/1/hosted/office.d.ts
[email protected]/dist/css/fabric.min.css
[email protected]/dist/css/fabric.components.min.css
[email protected]/client/core.min.js
@types/core-js
@microsoft/[email protected]/dist/office.helpers.min.js
@microsoft/[email protected]/dist/office.helpers.d.ts
[email protected]
@types/jquery
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment