I am working on an add-in with multiple task panes. One of the task panes makes multiple HTTP requests to fetch data from a server and populate the worksheet.
When there is more than one task pane open, if I close the pane that fetches data from the server (and keeps another pane open), the worksheet updates as if the fetching pane is still open. Moreover, if I close the second pane, the worksheet keeps updating.
I thought that it related to SharedRuntime (https://learn.microsoft.com/en-us/office/dev/add-ins/develop/configure-your-add-in-to-use-a-shared-runtime) so I've disabled it but it didn't solve the issue.
I tried to catch events for the pane close/hide (https://learn.microsoft.com/en-us/office/dev/add-ins/develop/show-hide-add-in) in order to stop the worksheet from being updated - couldn't get events to fire.
I also tried to catch the beforeunload
event. (How to capture the browser window close event?) also dead end - didn't work.
I thought it is somehow related to the network activity so I've managed to reproduce the issue with only local activity - just by running this function from the pane:
function intervalUpdateCells() {
setInterval(() => {
var values = [
[Math.floor(Math.random() * 1000), Math.floor(Math.random() * 1000), Math.floor(Math.random() * 1000)],
[Math.floor(Math.random() * 1000), Math.floor(Math.random() * 1000), Math.floor(Math.random() * 1000)],
[Math.floor(Math.random() * 1000), Math.floor(Math.random() * 1000), Math.floor(Math.random() * 1000)]
];
// Run a batch operation against the Excel object model
Excel.run(function (ctx) {
var sheet = ctx.workbook.worksheets.getActiveWorksheet();
sheet.getRange("B2:D4").values = values;
sheet.getRange("B1:D1").values = [['A', 'B', 'C']];
return ctx.sync();
})
.catch(errorHandler);
}, 100);
};
So if I run this function (it updates the active worksheet 10 times a second) from pane A while there are panes A & B open. when I'll close pane A the worksheet will continue to update and when I'll close pane B (and have no pane open) the worksheet will keep on updating.
My question: How can I detect that a task pane was closed in order to stop the worksheet from being updated?
EDIT: This is a very basic project that demonstrates the problem: https://github.com/YossiCohen/office-js-pane-not-closing-bug