OfficeDev / office-js

A repo and NPM package for Office.js, corresponding to a copy of what gets published to the official "evergreen" Office.js CDN, at https://appsforoffice.microsoft.com/lib/1/hosted/office.js.
https://learn.microsoft.com/javascript/api/overview
Other
685 stars 95 forks source link

How to handle task pane close event #1175

Closed rvskrishnaprasad closed 3 years ago

rvskrishnaprasad commented 4 years ago

Expected Behavior

Able to handle Window closing event.

Current Behavior

We are unable to handle Window closing event in our application. Is there a way we can handle the event?

lindalu-MSFT commented 4 years ago

Hi, please provide the client (Word, PowerPoint, Excel, etc.), exact steps to repro, and files needed to repro. Then I can assign someone to follow-up. This Issue needs more details for us to take action. Thanks!

rvskrishnaprasad commented 4 years ago

Hi, we are facing this issue in Word, Excel and PowerPoint. Here are the steps to re-produce.

  1. Open any window in Task pane.
  2. Click on the close button [X] at top right corner of the window. not able to handle this event in our application. Window_Close1 Window_Close

Please refer below screenshot

lindalu-MSFT commented 4 years ago

Hi @rvskrishnaprasad Thanks for providing the repro steps. I assigned this to our Word/PPT/XL PMs to address.

keyur32 commented 3 years ago

Hi there, sorry for the delay. In Excel, you can use this to configure a shared runtime: https://docs.microsoft.com/en-us/office/dev/add-ins/develop/configure-your-add-in-to-use-a-shared-runtime.

You can then configure your add-in to handle open/close events: https://docs.microsoft.com/en-us/office/dev/add-ins/develop/show-hide-add-in

keyur32 commented 3 years ago

Note: it is currently only available in Excel. It is available on PowerPoint in Preview, and not yet available in Word (but is the plan to add support for all hosts).

saibabu-hitachivantara commented 2 years ago

What I'm trying to achieve is giving a prompt to user when user trying to close the task pane.

Hey @keyur32, I tried with above options, Excel taskpane is running in the background. But I couldn't capture the event when I'm trying to close my taskpane.

Office.addin.onVisibilityModeChanged().then(function (args) { if (args.visibilityMode = "Taskpane") { console.log('test', args.visibilityMode); // Code that runs whenever the task pane is made visible. } else if (args.visibilityMode === "Hidden") { // Show prompt to the user. } });

I'm trying something like this in Excel plugin, I expected that the close event should trigger the visibility mode hidden condition it should prompt the user, but the event itself is not reaching to else when im closing the taskpane.

Can you please suggest on this ??

vanhock commented 6 months ago

Hi! At the date of this comment, Office.addin.onVisibilityModeChanged() doesn't work for Office.js Powerpoint. You could use Page Visibility API.

document.addEventListener("visibilitychange", function() {
    if (document.hidden) {
        console.log("The page is now hidden");
    } else {
        console.log("The page is now visible");
    }
});