Skip to content

Instantly share code, notes, and snippets.

@jipyua
Created January 16, 2024 01:36
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 jipyua/2b55aa37990d0e0fc81c7d1202e848b8 to your computer and use it in GitHub Desktop.
Save jipyua/2b55aa37990d0e0fc81c7d1202e848b8 to your computer and use it in GitHub Desktop.
Performs a basic Word API call using TypeScript.
name: Track Change
description: Performs a basic Word API call using TypeScript.
host: WORD
api_set: {}
script:
content: |
$("#run").click(() => tryCatch(run));
$("#run2").click(() => tryCatch(run2));
$("#run3").click(() => tryCatch(run3));
async function run() {
// Gets the current selection and changes the font color to red.
await Word.run(async (context) => {
const document = context.document;
document.load("changeTrackingMode");
await context.sync();
const mode = document.changeTrackingMode; // in 1.4 set
if (mode == Word.ChangeTrackingMode.trackAll || mode == Word.ChangeTrackingMode.trackMineOnly) {
context.document.changeTrackingMode = Word.ChangeTrackingMode.off; // if track change is on, then first turn it off;
}
await context.sync();
});
}
async function run2() {
// Gets the current selection and changes the font color to red.
await Word.run(async (context) => {
const contentcontrol = context.document.body.contentControls.getFirst();
contentcontrol.insertText("abc", "Start");
await context.sync();
});
}
async function run3() {
// Gets the current selection and changes the font color to red.
await Word.run(async (context) => {
const contentcontrol = context.document.body.contentControls.getFirst();
contentcontrol.getRange("After").select("End");
await context.sync();
context.document.changeTrackingMode = Word.ChangeTrackingMode.trackAll;
await context.sync();
});
}
/** 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 executes a code snippet that prints the selected text to the console. Make sure to enter and select text before clicking "Print selection".
</section>
<button id="run" class="ms-Button">
<span class="ms-Button-label">TurnOff TrackChanges</span>
</button>
<button id="run2" class="ms-Button">
<span class="ms-Button-label">InsertText To Control</span>
</button>
<button id="run3" class="ms-Button">
<span class="ms-Button-label">TurnOn TrackChanges</span>
</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/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