Adding a new 'onSelectionChanged' event during the application load crashes in Excel Online, if during the initialization of the addin, selection changes occur.
Application loads, event handlers are registered and trigger the onSelectionChange function which was registered.
Application loads, adding handler failed and no selectionChangeEvents are triggered.
In some more random cases the event handler could be added, but no events would be triggered.
Adding handlers should work as expected.
I have taken the ScriptLab example for 'Selection Changed' and modified it to imitate the way we use it in our application. Unlike the example, the handler is added as soon as the script is loaded.
https://gist.github.com/alx-andru/c2c93e4e29f60a31880e4c0e2dde63b3
In the case of an error, I try to add the handler again.
A more comprehensive Angular example can be found here, the base code for adding the handler and performing the selection is the same.
https://github.com/alx-andru/office-js-lab
The manifest inside the project points to an azure app service running the example.
As soon as our application is loaded, we'd like to track every selection change and validate the result against our business logic in order to read trigger values. In many cases one or multiple registered ContentControls (we added a binding to it) might be within the selection. We would like to inform the user about which ContentControls are selected and perform validations against those.
During our debugging we came across the issue and narrowed it down to this simplified prototype based on an existing script-lab example.
window>Object
ConsoleDOM
__zone_symbol__currentTask
Object {_zone=Object, runCount=0, ...}
code
"CannotRegisterEvent"
debugInfo
{"code":"CannotRegisterEvent","message":"An internal error has occurred."} {...}
innerError
null
message
"An internal error has occurred."
name
"OfficeExtension.Error"
originalStack
"Error: An internal error has occurred.\n at r.ZoneAwareError (https://script-lab.azureedge.net/bundles/polyfills.2e9078c49c55fa18c57b.bundle.js:1:77489)\n at new r (https://appsforoffice.microsoft.com/lib/1/hosted/excel-web-16.00.js:21:288918)\n at https://appsforoffice.microsoft.com/lib/1/hosted/excel-web-16.00.js:21:272038\n at ZoneDelegate.invoke (https://script-lab.azureedge.net/bundles/polyfills.2e9078c49c55fa18c57b.bundle.js:1:85192)\n at Zone.run (https://script-lab.azureedge.net/bundles/polyfills.2e9078c49c55fa18c57b.bundle.js:1:80018)\n at https://script-lab.azureedge.net/bundles/polyfills.2e9078c49c55fa18c57b.bundle.js:1:77231\n at ZoneDelegate.invokeTask (https://script-lab.azureedge.net/bundles/polyfills.2e9078c49c55fa18c57b.bundle.js:1:85935)\n at Zone.runTask (https://script-lab.azureedge.net/bundles/polyfills.2e9078c49c55fa18c57b.bundle.js:1:80709)\n at drainMicroTaskQueue (https://script-lab.azureedge.net/bundles/polyfills.2e9078c49c55fa18c57b.bundle.js:1:75411)\n at ZoneTask.invoke (https://script-lab.azureedge.net/bundles/polyfills.2e9078c49c55fa18c57b.bundle.js:1:86983)"
stack
"Error: An internal error has occurred.\n at r.ZoneAwareError (https://script-lab.azureedge.net/bundles/polyfills.2e9078c49c55fa18c57b.bundle.js:1:77489)\n at new r (https://appsforoffice.microsoft.com/lib/1/hosted/excel-web-16.00.js:21:288918)\n at https://appsforoffice.microsoft.com/lib/1/hosted/excel-web-16.00.js:21:272038\n at ZoneDelegate.invoke (https://script-lab.azureedge.net/bundles/polyfills.2e9078c49c55fa18c57b.bundle.js:1:85192)\n at Zone.run (https://script-lab.azureedge.net/bundles/polyfills.2e9078c49c55fa18c57b.bundle.js:1:80018)\n at https://script-lab.azureedge.net/bundles/polyfills.2e9078c49c55fa18c57b.bundle.js:1:77231\n at ZoneDelegate.invokeTask (https://script-lab.azureedge.net/bundles/polyfills.2e9078c49c55fa18c57b.bundle.js:1:85935)\n at Zone.runTask (https://script-lab.azureedge.net/bundles/polyfills.2e9078c49c55fa18c57b.bundle.js:1:80709)\n at drainMicroTaskQueue (https://script-lab.azureedge.net/bundles/polyfills.2e9078c49c55fa18c57b.bundle.js:1:75411)\n at ZoneTask.invoke (https://script-lab.azureedge.net/bundles/polyfills.2e9078c49c55fa18c57b.bundle.js:1:86983)"
traceMessages
[]
zoneAwareStack
"Error: An internal error has occurred.\n at r.ZoneAwareError (https://script-lab.azureedge.net/bundles/polyfills.2e9078c49c55fa18c57b.bundle.js:1:77489)\n at new r (https://appsforoffice.microsoft.com/lib/1/hosted/excel-web-16.00.js:21:288918)\n at https://appsforoffice.microsoft.com/lib/1/hosted/excel-web-16.00.js:21:272038\n at ZoneDelegate.invoke (https://script-lab.azureedge.net/bundles/polyfills.2e9078c49c55fa18c57b.bundle.js:1:85192)\n at Zone.run (https://script-lab.azureedge.net/bundles/polyfills.2e9078c49c55fa18c57b.bundle.js:1:80018)\n at https://script-lab.azureedge.net/bundles/polyfills.2e9078c49c55fa18c57b.bundle.js:1:77231\n at ZoneDelegate.invokeTask (https://script-lab.azureedge.net/bundles/polyfills.2e9078c49c55fa18c57b.bundle.js:1:85935)\n at Zone.runTask (https://script-lab.azureedge.net/bundles/polyfills.2e9078c49c55fa18c57b.bundle.js:1:80709)\n at drainMicroTaskQueue (https://script-lab.azureedge.net/bundles/polyfills.2e9078c49c55fa18c57b.bundle.js:1:75411)\n at ZoneTask.invoke (https://script-lab.azureedge.net/bundles/polyfills.2e9078c49c55fa18c57b.bundle.js:1:86983)"
_createDebugInfo
function()
popStackFrame
function()
constructor
function()
toString
function()
```

@ShawnNiu please reply.
Hey @alx-andru thanks for the super detailed description and for reporting this. I was able to repro this issue and, as described, happens only in Excel Online. {TFS:2158100}
Quick clarification: Excel is not crashing, but we are unsuccessful to register the event in such potentially edge conditions. I think that you are doing the right error handling on ultimately subscribing to the events.
We will investigate an update when we have a plan in place.
Thanks,
Juan.
@JuaneloJuanelo thanks for confirming. I also tested it targeting different api versions of office.js and the beta release seems to be more stable in regards of the selection.
Hi @alx-andru
Sorry for the inconvenience, we already recognized the selection change problem under the workbook object, will update the sample in scriptlab accordingly. Thanks for pointing out the problem 馃憤
Please refer to the selection change api in beta first. Let me know if you have any problem with that.
Hey @ShawnNiu this seems to work way better. Every once in a while we still see the issue though
Most helpful comment
Hi @alx-andru
Sorry for the inconvenience, we already recognized the selection change problem under the workbook object, will update the sample in scriptlab accordingly. Thanks for pointing out the problem 馃憤
Please refer to the selection change api in beta first. Let me know if you have any problem with that.