Skip to content

Instantly share code, notes, and snippets.

@xuruiyao-msft
Created June 17, 2022 10:13
Show Gist options
  • Save xuruiyao-msft/3a49f34adccfba16d5e2667b00288450 to your computer and use it in GitHub Desktop.
Save xuruiyao-msft/3a49f34adccfba16d5e2667b00288450 to your computer and use it in GitHub Desktop.
Create a new snippet from a blank template.
name: CCRangeTrack
description: Create a new snippet from a blank template.
host: WORD
api_set: {}
script:
content: >
var ccRange;
var insertRange;
$("#trackCommentContentRange").click(() =>
tryCatch(trackCommentContentRange));
$("#getTrackedRangeAfterRevisement").click(() =>
tryCatch(getTrackedRangeAfterRevisement));
$("#insertTextAtStart").click(() => tryCatch(insertTextAtStart));
$("#checkParitialRange").click(() => tryCatch(checkParitialRange));
async function trackCommentContentRange() {
await Word.run(async (context) => {
let comment = context.document.body.getComments().getFirst();
ccRange = comment.contentRange;
ccRange.track();
ccRange.load();
await context.sync();
console.log(`range content ${ccRange.text}`);
});
}
async function getTrackedRangeAfterRevisement() {
await Word.run(ccRange, async (context) => {
ccRange.load();
await context.sync();
console.log(`updated range content: ${ccRange.text}`);
});
}
async function insertTextAtStart() {
await Word.run(ccRange, async (context) => {
insertRange = ccRange.insertText("Start", "Start");
insertRange.track();
insertRange.load();
await context.sync();
console.log(`updated range content: ${insertRange.text}`);
});
}
async function checkParitialRange() {
await Word.run(ccRange, async (context) => {
insertRange.load();
await context.sync();
console.log(`updated range content: ${insertRange.text}`);
});
}
/** 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>\n\t<button id=\"trackCommentContentRange\" class=\"ms-Button\">\n <span class=\"ms-Button-label\">trackCommentContentRange</span>\n</button>\n</section>\n\n<section>\n\t<button id=\"getTrackedRangeAfterRevisement\" class=\"ms-Button\">\n <span class=\"ms-Button-label\">getTrackedRangeAfterRevisement</span>\n</button>\n</section>\n\n<section>\n\t<button id=\"insertTextAtStart\" class=\"ms-Button\">\n <span class=\"ms-Button-label\">insertTextAtStart</span>\n</button>\n</section>\n\n<section>\n\t<button id=\"checkParitialRange\" class=\"ms-Button\">\n <span class=\"ms-Button-label\">checkParitialRange</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