Skip to content

Instantly share code, notes, and snippets.

@crazygao
Last active August 20, 2019 07:18
Show Gist options
  • Save crazygao/1e6062de945d7dd223f63cfb378c493d to your computer and use it in GitHub Desktop.
Save crazygao/1e6062de945d7dd223f63cfb378c493d to your computer and use it in GitHub Desktop.
name: Comment Sample Script
description: ''
host: EXCEL
api_set: {}
script:
content: >
$("#add-comment").click(() => tryCatch(addComment));
$("#get-comment-at-cell").click(() => tryCatch(getCommentAtCell));
$("#add-commentReply").click(() => tryCatch(addCommentReply));
$("#delete-commentReply").click(() => tryCatch(deleteCommentReply));
$("#toggle-resolved").click(() => tryCatch(toggleResolved));
$("#delete-comment").click(() => tryCatch(deleteComment));
//$("#GetProperty-firstcomment").click(() =>
tryCatch(getfirstcommentproperties));
$("#GetProperty-firstcommentreply").click(() =>
tryCatch(getfirstcommentreplyproperties));
$("#GetProperty-firstcomment").click(() => tryCatch(getAllCommentProperty));
// Add comment on a selected range
async function addComment(context: Excel.RequestContext) {
var count = context.workbook.comments.getCount();
await context.sync();
console.log("Comment count before add:" + count.value);
var range = context.workbook.getSelectedRange();
context.workbook.comments.add("text of the comment", range);
await context.sync();
count = context.workbook.comments.getCount();
await context.sync();
console.log("Comment count after add: " + count.value);
}
async function getCommentAtCell(context: Excel.RequestContext) {
var workbook = context.workbook;
var activeCell = workbook.getActiveCell();
var comment = workbook.comments.getItemByCell(activeCell);
context.load(comment);
await context.sync();
//console.log(comment.id);
return comment;
}
async function toggleResolved(context: Excel.RequestContext) {
var comment = await getCommentAtCell(context);
context.load(comment);
comment.load("resolved");
await context.sync();
comment.resolved = !comment.resolved;
await context.sync();
console.log("Comment Resolved: " + comment.resolved);
}
async function deleteComment(context: Excel.RequestContext) {
var comment = await getCommentAtCell(context);
var count = context.workbook.comments.getCount();
await context.sync();
console.log("Comments count before add: " + count.value);
comment.delete();
await context.sync();
count = context.workbook.comments.getCount();
await context.sync();
console.log("Comment count after add: " + count.value);
}
// Add a comment reply on a specified cell
async function addCommentReply(context: Excel.RequestContext) {
var comment = await getCommentAtCell(context);
var count = comment.replies.getCount();
await context.sync();
console.log("Comment reply count before add: " + count.value);
comment.replies.add("text of the reply");
await context.sync();
count = comment.replies.getCount();
await context.sync();
console.log("Comment reply count after add: " + count.value);
}
// Delete a comment reply
async function deleteCommentReply(context: Excel.RequestContext) {
var range = context.workbook.getSelectedRange();
var comment = context.workbook.comments.getItemByCell(range);
context.load(comment);
await context.sync();
comment.replies.getItemAt(0).delete();
await context.sync();
}
// Get first comment reply properties.
async function getfirstcommentreplyproperties(context: Excel.RequestContext)
{
var range = context.workbook.getSelectedRange();
var comment = await getCommentAtCell(context);
context.load(comment);
await context.sync();
var reply = comment.replies.getItemAt(0);
context.load(reply);
await context.sync();
console.log(JSON.stringify(reply));
var locationRange = reply.getLocation();
locationRange.load("address");
await context.sync();
console.log("Comment reply location: " + locationRange.address);
}
async function getAllCommentProperty(context: Excel.RequestContext) {
var comments = context.workbook.comments;
context.load(comments);
await context.sync();
console.log("Comments count: " + comments.items.length);
await comments.items.forEach(async (comment, index, array) => {
context.load(comment);
await context.sync();
console.log(JSON.stringify(comment));
});
}
/** Default helper for invoking an action and handling errors. */
async function tryCatch(callback) {
try {
await Excel.run(callback);
} catch (error) {
OfficeHelpers.UI.notify(error);
OfficeHelpers.Utilities.log(error);
}
}
language: typescript
template:
content: |-
<h3>Comment</h3>
<button id="add-comment" class="ms-Button">
<span class="ms-Button-label">Add Comment</span>
</button>
<br/><br/>
<button id="get-comment-at-cell" class="ms-Button">
<span class="ms-Button-label">Get Comment At Cell</span>
</button>
<br/><br/>
<button id="delete-comment" class="ms-Button">
<span class="ms-Button-label">Delete Comment</span>
</button>
<br/><br/>
<button id="GetProperty-firstcomment" class="ms-Button">
<span class="ms-Button-label">Get properties of All comment</span>
</button>
<br/><br/>
<button id="toggle-resolved" class="ms-Button">
<span class="ms-Button-label">first comment toggle resolved</span>
</button>
<br/><br/>
<h3>Comment Reply</h3>
<button id="add-commentReply" class="ms-Button">
<span class="ms-Button-label">Add Comment Reply</span>
</button>
<br/><br/>
<button id="delete-commentReply" class="ms-Button">
<span class="ms-Button-label">Delete Comment Reply</span>
</button>
<br/><br/>
<button id ="GetProperty-firstcommentreply" class="ms-Button">
<span class="ms-Button-label">Get properties of first comment reply</span>
</button>
<br/><br/>
<button id ="changeResolved-firstcommentreply" class="ms-Button">
<span class="ms-Button-label">Change resolved of first comment reply(Forbidden)</span>
</button>
<br/><br/>
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
https://appsforoffice.microsoft.com/lib/beta/hosted/office.d.ts
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
@microsoft/office-js-helpers@0.7.4/dist/office.helpers.min.js
@microsoft/office-js-helpers@0.7.4/dist/office.helpers.d.ts
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