Skip to content

Instantly share code, notes, and snippets.

@globalq
Created September 22, 2021 11:01
Show Gist options
  • Save globalq/4d46a0e706d08acdc9fd1639bf9cd3f7 to your computer and use it in GitHub Desktop.
Save globalq/4d46a0e706d08acdc9fd1639bf9cd3f7 to your computer and use it in GitHub Desktop.
This sample shows how to register a workbook activated event handler.
name: Workbook activated event
description: This sample shows how to register a workbook activated event handler.
host: EXCEL
api_set: {}
script:
content: |
$("#register-event-handler").click(() => tryCatch(registerEventHandler));
async function workbookActivated(event: Excel.WorkbookActivatedEventArgs) {
await Excel.run(async (context) => {
// Callback function for when the workbook is activated.
console.log("The workbook was activated.");
});
}
async function registerEventHandler() {
await Excel.run(async (context) => {
const workbook = context.workbook;
// Register the workbook activated event handler.
workbook.onActivated.add(workbookActivated);
await context.sync();
console.log("Added event handler for workbook activated.");
});
}
/** 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\">\n\t<p>This sample shows how to register a workbook activated event handler.</p>\n\t<p>Once the event handler is registered, a notification prints to the console when the workbook is activated. Try\n\t\tswitching to another application and then switching back to Excel to see the console notification.</p>\n</section>\n\n<section class=\"setup ms-font-m\">\n\t<h3>Try it out</h3>\n\t<button id=\"register-event-handler\" class=\"ms-Button\">\n <span class=\"ms-Button-label\">Register the event handler</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
[email protected]/dist/css/fabric.min.css
[email protected]/dist/css/fabric.components.min.css
[email protected]/client/core.min.js
@types/core-js
[email protected]
@types/[email protected]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment