Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DiegoSalazar/7385d193222b5a542e84217ddb94a0ef to your computer and use it in GitHub Desktop.
Save DiegoSalazar/7385d193222b5a542e84217ddb94a0ef to your computer and use it in GitHub Desktop.
Performs a basic Word API call using JavaScript with the "common API" syntax (compatible with Office 2013).
name: Basic API call (Office 2013)
description: >-
Performs a basic Word API call using JavaScript with the "common API" syntax
(compatible with Office 2013).
host: WORD
api_set: {}
script:
content: |
$("#run").click(run);
function run() {
Office.context.document.getSelectedDataAsync(Office.CoercionType.Text, (asyncResult) => {
if (asyncResult.status === Office.AsyncResultStatus.Failed) {
console.error(asyncResult.error.message);
} else {
console.log(`The selected data is "${asyncResult.value}".`);
}
});
}
$("#ins").click(() => {
Word.run(async (context) => {
const html = `
<p class=MsoNormal>
<span class=msoDel>
<ins cite="mailto:Me!"
datetime="${new Date().toISOString()}">Inserted!</ins>
</span>
</p>
`;
context.document.body.insertHtml(html, Word.InsertLocation.end);
});
});
$("#track").click(async () => {
const trackRevisions = Office.context.document.settings.get('trackRevisions')
console.log({ trackRevisions })
Office.context.document.settings.set('trackRevisions', !trackRevisions)
Office.context.document.settings.saveAsync(result => {
console.log(`saveAsync: ${result.status}`)
})
})
Word.run(async (context) => {
context.document.body.load("paragraphs");
await context.sync();
const numParas = context.document.body.paragraphs.items.length;
console.log(`${numParas} paragraphs`);
const html = context.document.body.getHtml();
await context.sync();
console.log(html.value);
context.document.body.paragraphs.items.forEach((p, i) => {
console.log(i + 1);
console.log(p.text);
});
});
language: typescript
template:
content: "<section class=\"ms-font-m\">\n\tThis sample executes a code snippet that prints the selected text to the console. Make sure to enter and select text\n\tbefore clicking \"Print selection\".\n</section>\n<button id=\"run\" class=\"ms-Button\">\n <span class=\"ms-Button-label\">Print selection</span>\n</button>\n\n<button id=\"ins\" class=\"ms-Button\">\n <span class=\"ms-Button-label\">Insertion</span>\n</button>\n\n<br />\n\n<button id=\"track\" class=\"ms-Button\">\n <span class=\"ms-Button-label\">Toggle Tracking</span>\n</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/beta/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