I have been trying to learn the basics for developing an Office add-in for Excel using the tutorial Microsoft provides: Excel Add-in Tutorial. My primary goal was to learn how to execute functions directly from a button on the ribbon. So, I skipped down to the Protect a worksheet sample in the tutorial.
I'm fairly confident I followed the instructions exactly (and even directly pasted the provided code later), but I could not invoke the toggleProtection function in commands.js based off the provided instructions. I spent countless hours debugging and researching the problem. Finally, I overcame the issue by moving the function and accompanying Office.actions.associate() line above the action function in commands.js. The action function is autogenerated when using Yeoman to create the Excel add-in, and the tutorial explicitly states, "Add the following function immediately after the action function."
I noticed the action function uses Office.MailboxEnums, which seems to be specific to the outlook package. So, is the action function supposed to exist for an Excel add-in? If the action function is intended and functional, does anyone know why there would be an issue with the toggleProtection function existing below the action function? Any clarification about this problem would be greatly appreciated. I've pasted the commands.js code below. Thanks!
/*
* Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
* See LICENSE in the project root for license information.
*/
/* global global, console, Excel, Office, OfficeExtension, self, window */
Office.onReady(() => {
// If needed, Office.js is ready to be called
console.log("We're in commands.js!!!");
});
async function toggleProtection(args) {
await Excel.run(async (context) => {
console.log("we've entered toggleProtection!!!");
const sheet = context.workbook.worksheets.getActiveWorksheet();
sheet.load("protection/protected");
await context.sync();
if (sheet.protection.protected) {
sheet.protection.unprotect();
} else {
sheet.protection.protect();
}
await context.sync();
}).catch(function (error) {
console.log("Error: " + error);
if (error instanceof OfficeExtension.Error) {
console.log("Debug info: " + JSON.stringify(error.debugInfo));
}
});
args.completed();
}
Office.actions.associate("toggleProtection", toggleProtection);
/**
* Shows a notification when the add-in command is executed.
* @param event {Office.AddinCommands.Event}
*/
function action(event) {
const message = {
type: Office.MailboxEnums.ItemNotificationMessageType.InformationalMessage,
message: "Performed action.",
icon: "Icon.80x80",
persistent: true,
};
// Show a notification message
Office.context.mailbox.item.notificationMessages.replaceAsync("action", message);
// Be sure to indicate when the add-in command function is complete
event.completed();
}
function getGlobal() {
return typeof self !== "undefined"
? self
: typeof window !== "undefined"
? window
: typeof global !== "undefined"
? global
: undefined;
}
const g = getGlobal();
// The add-in command functions need to be available in global scope
g.action = action;