We have run in to, what seems like, a bug in Office.js on Office for Mac.
If you attach an event handler to DocumentSelectionChanged
event that calls Excel.run
the standard Excel "undo" functionality gets disabled. And this remains disabled until the add-in is unloaded (i.e. the event handler is unhooked).
You can replicate this problem by (for example), taking the Excel-Add-in-JS-CollegeCreditsTracker
sample app and inserting the following block of code in app.initialize
method.
Office.context.document.addHandlerAsync(
Office.EventType.DocumentSelectionChanged,
function () {
Excel.run(function (ctx) {
var activeCell = ctx.workbook.getSelectedRange();
activeCell.load(["address", "worksheet", "rowIndex", "columnIndex", "values", "formulas"]);
return ctx.sync().then(function () {
app.showNotification(activeCell.address);
});
}).catch(function (err) {
console.log(err);
});
},
null,
function (asyncResult) {
console.log("Handler added: " + JSON.stringify(asyncResult));
}
);
Note that this works fine on Excel Desktop an Excel Online. Is there a specific reason, such as API version supported on Office for Mac, that this would be failing?
This answer on an unrelated question suggests that there's an alternative way to handle selection change in newer API versions (although it doesn't say how). In which case, is that a possible workaround for this?
We cannot use BindingChanged events because we want to know when the user moves in and out of bindings, when user switches worksheets etc.