Skip to content

Commit c223919

Browse files
[Excel] Add workbook activated preview sample (OfficeDev#537)
* Add workbook activated sample * Update workbook activated sample methods and comments * Apply suggestions from code review Co-authored-by: Alex Jerabek <38896772+AlexJerabek@users.noreply.github.com> * Update excel.xlsx metadata file * Incorporate feedback from code review Co-authored-by: Alex Jerabek <38896772+AlexJerabek@users.noreply.github.com>
1 parent 9953a95 commit c223919

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
order: 5
2+
id: excel-events-workbook-activated
3+
name: Workbook activated event
4+
description: This sample shows how to register a workbook activated event handler.
5+
host: EXCEL
6+
api_set:
7+
ExcelAPI: '1.13'
8+
script:
9+
content: |
10+
$("#register-event-handler").click(() => tryCatch(registerEventHandler));
11+
12+
async function workbookActivated(event: Excel.WorkbookActivatedEventArgs) {
13+
await Excel.run(async (context) => {
14+
// Callback function for when the workbook is activated.
15+
console.log("The workbook was activated.");
16+
});
17+
}
18+
19+
async function registerEventHandler() {
20+
await Excel.run(async (context) => {
21+
const workbook = context.workbook;
22+
23+
// Register the workbook activated event handler.
24+
workbook.onActivated.add(workbookActivated);
25+
26+
await context.sync();
27+
console.log("Added event handler for workbook activated.");
28+
});
29+
}
30+
31+
/** Default helper for invoking an action and handling errors. */
32+
async function tryCatch(callback) {
33+
try {
34+
await callback();
35+
} catch (error) {
36+
// Note: In a production add-in, you'd want to notify the user through your add-in's UI.
37+
console.error(error);
38+
}
39+
}
40+
language: typescript
41+
template:
42+
content: |-
43+
<section class="ms-font-m">
44+
<p>This sample shows how to register a workbook activated event handler.</p>
45+
<p>Once the event handler is registered, a notification prints to the console when the workbook is activated. Try
46+
switching to another application and then switching back to Excel to see the console notification.</p>
47+
</section>
48+
49+
<section class="setup ms-font-m">
50+
<h3>Try it out</h3>
51+
<button id="register-event-handler" class="ms-Button">
52+
<span class="ms-Button-label">Register the event handler</span>
53+
</button>
54+
</section>
55+
language: html
56+
style:
57+
content: |-
58+
section.samples {
59+
margin-top: 20px;
60+
}
61+
62+
section.samples .ms-Button, section.setup .ms-Button {
63+
display: block;
64+
margin-bottom: 5px;
65+
margin-left: 20px;
66+
min-width: 80px;
67+
}
68+
language: css
69+
libraries: |
70+
https://appsforoffice.microsoft.com/lib/beta/hosted/office.js
71+
@types/office-js-preview
72+
73+
office-ui-fabric-js@1.4.0/dist/css/fabric.min.css
74+
office-ui-fabric-js@1.4.0/dist/css/fabric.components.min.css
75+
76+
core-js@2.4.1/client/core.min.js
77+
@types/core-js
78+
79+
jquery@3.1.1
80+
@types/jquery@3.3.1
87 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)