Skip to content

Instantly share code, notes, and snippets.

@ci010
Created March 16, 2021 05:54
Show Gist options
  • Save ci010/1d2a2f2d9173e89fa7f6cbc681894631 to your computer and use it in GitHub Desktop.
Save ci010/1d2a2f2d9173e89fa7f6cbc681894631 to your computer and use it in GitHub Desktop.
Print Worksheet.TabId
name: Worksheet.TabId
description: Print Worksheet.TabId
host: EXCEL
api_set: {}
script:
content: >
$("#run").click(() => tryCatch(getActiveWorksheet));
$("#run1").click(() => tryCatch(getInputWorksheet));
async function printTabId(ws: Excel.Worksheet, context:
Excel.RequestContext) {
ws.load("tabId");
await context.sync();
console.log(ws.tabId);
}
async function getActiveWorksheet() {
await Excel.run(async (context) => {
const ws = context.workbook.worksheets.getActiveWorksheet();
await printTabId(ws, context);
});
}
async function getInputWorksheet() {
await Excel.run(async (context) => {
const name = $("#name").val() as string;
const ws = context.workbook.worksheets.getItemOrNullObject(name);
ws.load("isNullObject");
await context.sync();
if (!ws.isNullObject) {
await printTabId(ws, context);
} else {
console.log("Cannot found sheet named " + name);
}
});
}
/** 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>Print Worksheet Tab Id</h3>\n\t<button id=\"run\" class=\"ms-Button\">\n <span class=\"ms-Button-label\">Print Selected Worksheet Tab Id</span>\n </button>\n\t<div style=\"margin: 10px\">\n\t\tWorksheet Name\n <input id=\"name\">\n </div>\n\t\t<button id=\"run1\" class=\"ms-Button\">\n <span class=\"ms-Button-label\">Print Named Worksheet Tab Id</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://unpkg.com/@microsoft/office-js@1.1.47-custom.6/dist/office.debug.js
// @types/office-js
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