2

As some existing threads suggest (eg, one, two, three), current Dialog Box does not provide an API to send regularly messages from the host page (eg, task pane) to the Dialog box.

So I have to look for a workaround: we reserve a variable message in localStorage, then we make the Dialog box check regularly if the value of message changes. It is like manually implementing an event listener by localStorage.

Does anyone know how to implement that in a sure and efficient way (given JavaScript isn't a multi-threaded language)? I don't want this listener to be costly, and what would be the appropriate interval to set?

Edit 1:

I tried the StorageEvent that @PatrickEvans suggested. For both the add-in and the dialog site, we need to implement a sendMessage... and a receiveMessage... by the variables messageFromHostToSite and messageFromSiteToHost in storage.

It worked in Excel Online in Chrome, and in Excel Online in IE 11. However, it did NOT work in Excel 2016 for Windows 7 or 10; we can set items to localStorage and get them back, changing localStorage seems to not fire anything.

So could anyone confirm that StorageEvent is indeed not supported in Excel 2016 for Windows 7 or 10? In that case, I am still looking for other workarounds...

Edit 2:

I finally made a simplified example: here is the xml file, which calls the add-in and the site. It works well in Excel Online, but does NOT fire events in Excel for Windows.

enter image description here

SoftTimur
  • 5,630
  • 38
  • 140
  • 292
  • 2
    Check to see if the [StorageEvent](https://developer.mozilla.org/en-US/docs/Web/API/StorageEvent) is supported, as it will [fire a callback whenever the storage (in this case localStorage) is changed](https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API/Using_the_Web_Storage_API#Responding_to_storage_changes_with_the_StorageEvent). – Patrick Evans Jun 14 '17 at 10:26
  • Please see my edit 1 – SoftTimur Jun 19 '17 at 20:42
  • We need to see some of your code. – akahunahi Jun 19 '17 at 22:56
  • One difficulty to put it in a playground is that two parts are involved: the add-in and the dialog web page. @Ligemer – SoftTimur Jun 19 '17 at 23:00
  • @SoftTimur, a playground would be great, but not required ;-) We can figure out your code, but we need to see what you're doing first before we can help any further. – akahunahi Jun 20 '17 at 00:20
  • @Ligemer the reason why I added external links to my code rather than using playgrounds such as script lab is to stay in exactly the same domain. Their source code can be easily inspected. – SoftTimur Jun 21 '17 at 23:49

1 Answers1

1

I had similar issues communicating between apps. In my testing, Excel Online storageEvent works because the communication is between browser tabs of the same "instance". With Excel for Windows, 2 add-ins are in separate browser "instances"; which don't communicate storageEvents. Worse yet was Office for Mac (WebKit); which did not communicate storageEvents and also could not read storage changes from the other app unless they were restarted -- I believe this is because the storage values were cached for the instance (not written to disk and not visible to the other instance/app).

So, I believe Excel Online could use StorageEvents, Excel for Windows would require polling (e.g. check every second), and Excel for Mac may require a server-based solution, such as WebSockets.

Andrew Hall
  • 549
  • 6
  • 17
  • Thank you for the information, especially for Excel for Mac that I have not tested yet. Have you occurred [this problem](https://stackoverflow.com/questions/44735768/cannot-switch-between-popup-window-and-excel-for-mac)? Is there a workaround? – SoftTimur Jun 24 '17 at 11:18
  • I have not used the Dialog API, I can only speculate that the issue may be similar to my app-to-app communication issues because I think the Dialog API opens in a separate browser instance (as opposed to just a separate tab) and therefore complicates the localStorage (and indexedDB and cookie) sharing. – Andrew Hall Jun 24 '17 at 15:59
  • I almost give up the idea to make storageEvent work... Have you used `window.open` to popup a window from Office add-in? If so, I would like to know if you can switch between the popup window and Excel... – SoftTimur Jun 24 '17 at 23:03
  • I have not tried, but in general, you should be able to communicate between the parent/child windows: https://stackoverflow.com/questions/1830347/quickest-way-to-pass-data-to-a-popup-window-i-created-using-window-open/1830388#1830388 – Andrew Hall Jun 25 '17 at 14:54
  • By `window.open`, I am sure the communication works, but I find it odd that we cannot switch between the popup window and Excel for Mac ... – SoftTimur Jun 25 '17 at 20:13