'Remove Excel workbook onSelectionChange event

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.



Solution 1:[1]

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.

Solution 2:[2]

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();

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Rick Kirkham
Solution 2 FreeSoftwareServers