Remove Excel workbook onSelectionChange event

546 Views Asked by At

I am working on a Excel Web Add-In using Office.js. I added onSelectionChange event for a workbook and later trying to remove it, but this event still works. Here you can find an example that I created with ScriptLab, that shows that event can not be removed: https://gist.github.com/shss1985/afd1c00484acfec33740cdbcda3f78b5

Is there a way to remove it correctly?

Thanks.

2

There are 2 best solutions below

2
On

To remove the handler you have to use the same RequestContext as was used to add it. For details about how you do this, see Remove an event handler.

0
On

Removing them looks like this --> But, must be called in the same context so my issue is really that I need to be able to check if they are already registered, as this remove doesn't remove previously setup event handlers when my page refreshes and gets new context

Note: the var lines actually register the event, so you can comment out the remove to test.

var var_onActivated = ws.onActivated.add(Client_WOs_Do_onActivated);
var var_onDeactivated = ws.onDeactivated.add(Client_WOs_onDeactivated);

await var_onActivated.remove();
await var_onDeactivated.remove();