Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save crazygao/17572ab0d08864c5319f07269e71996b to your computer and use it in GitHub Desktop.
Save crazygao/17572ab0d08864c5319f07269e71996b to your computer and use it in GitHub Desktop.
name: Insert or update comment with mentions. (1)
description: ''
host: EXCEL
api_set: {}
script:
content: >
$("#add-comment-withmention").click(() => tryCatch(addComment));
$("#update-comment-withmention").click(() => tryCatch(updateComment));
$("#update-commentreply-withmention").click(() =>
tryCatch(updateCommentReply));
$("#get-comment-withmention").click(() => tryCatch(getItemInfo));
async function addComment() {
await Excel.run(async (ctx) => {
var comment = null;
{
var person1 = {
id: 0,
name: "Philip Yu",
email: "phyu@microsoft.com"
};
var person2 = {
id: 1,
name: "Tuan",
email: "yigao@microsoft.com"
};
var person3 = {
id: 2,
name: "Shanbo lu",
email: "shlu@microsoft.com"
};
var content =
'<at id="0">Philip Yu</at> <at id="2">Shanbo lu</at> please double check the monthly sales data. <at id="1">Tuan</at>, will you help review them?';
var commentBody = {
richContent: "",
mentions: []
};
commentBody.richContent = content;
commentBody.mentions[0] = person1;
commentBody.mentions[1] = person2;
commentBody.mentions[2] = person3;
var address = ctx.workbook.getActiveCell();
var comment1 = ctx.workbook.comments.add(address, commentBody, "Mention");
ctx.load(comment1);
await ctx.sync();
console.log(JSON.stringify(comment1));
}
});
}
async function updateComment() {
await Excel.run(async (ctx) => {
var comment = null;
{
var person1 = {
id: 0,
name: "Philip Gao",
email: "yigao@microsoft.com"
};
var person2 = {
id: 1,
name: "Shanbo lu",
email: "shlu@microsoft.com"
};
var content =
'<at id="0">Philip Gao</at> Hi <at id="1">Shanbo lu</at> this is testing for updating comment with menitons';
var commentBody = {
richContent: "",
mentions: []
};
commentBody.richContent = content;
commentBody.mentions[0] = person1;
commentBody.mentions[1] = person2;
var comment1 = ctx.workbook.comments.getItemAt(0);
comment1.updateMentions(commentBody);
ctx.load(comment1);
await ctx.sync();
console.log(JSON.stringify(comment1));
}
});
}
async function updateCommentReply() {
await Excel.run(async (ctx) => {
var comment = null;
{
var person1 = {
id: 0,
name: "Philip Gao",
email: "yigao@microsoft.com"
};
var person2 = {
id: 1,
name: "Shanbo lu",
email: "shlu@microsoft.com"
};
var content =
'<at id="0">Philip Gao</at> Hi <at id="1">Shanbo lu</at> this is testing for updating comment reply with menitons';
var commentBody = {
richContent: "",
mentions: []
};
commentBody.richContent = content;
commentBody.mentions[0] = person1;
commentBody.mentions[1] = person2;
var comment1 = ctx.workbook.comments.getItemAt(0);
ctx.load(comment1);
var reply = ctx.workbook.comments.getItemAt(0).replies.getItemAt(0);
reply.updateMentions(commentBody);
ctx.load(reply);
await ctx.sync();
console.log(JSON.stringify(reply));
}
});
}
async function getItemInfo() {
await Excel.run(async (ctx) => {
var comment = null;
{
var comment1 = ctx.workbook.comments.getItemAt(0);
ctx.load(comment1);
// ctx.load(comment1);
await ctx.sync();
var mentions1 = comment1.mentions;
console.log(JSON.stringify(mentions1));
console.log(JSON.stringify(comment1));
}
});
}
/** 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=\"samples ms-font-m\">\n\t<h3>Try it out</h3>\n\t<p>Press the following buttons separately.</p>\n\t<p>\n\t\t<button id=\"add-comment-withmention\" class=\"ms-Button\">\n <span class=\"ms-Button-label\">Add a comment with mentions</span>\n </button>\n\t\t<p>\n\t\t\t<button id=\"update-comment-withmention\" class=\"ms-Button\">\n <span class=\"ms-Button-label\">update the first comment with mentions</span>\n </button>\n\t\t\t<p>\n\t\t\t\t<button id=\"update-commentreply-withmention\" class=\"ms-Button\">\n\t\t\t\t<span class=\"ms-Button-label\">update the first comment reply with mentions</span>\n\t\t</button>\n\t\t\t<p>\n\t\t\t\t<button id=\"get-comment-withmention\" class=\"ms-Button\">\n\t\t\t\t\t\t\t<span class=\"ms-Button-label\">Get comment with mentions</span>\n\t\t\t\t\t</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/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