Force add on refresh when sidebar is closed, reset navigation.

861 views
Skip to first unread message

Leandro Zubrezki

unread,
Jun 17, 2021, 5:21:37 PM6/17/21
to Google Apps Script Community
Hi group!

I can't get this to reliable work.

I have a Google Workspace Add-on, that uses the HTML service and sidebar for functionality that requires polling.

I need to make sure that the add on is reloaded when the sidebar is closed, is there a way to do it that always works?

This is the flow: openSidebar_() -> resetNavigation_() -> HTML -> closeSidebar() ->  resetNavigation_()

// Apps Script
function openSidebar_() {
    SpreadsheetApp.getUi().showSidebar(...);
    return resetNavigation_();
}

// Apps Script
function resetNavigation_() {
    const card = buildMainCard();
    const nav = CardService.newNavigation()
        .popToRoot()
        .updateCard(card);
  return CardService.newActionResponseBuilder()
    .setStateChanged(true)
    .setNavigation(nav)
   .build();
}

// HTML Template
google.script.run
    .withSuccessHandler(() => google.script.host.close())
    .withFailureHandler(() => google.script.host.close())
    .closeSidebar();

// Apps Script
function closeSidebar() {
    return resetNavigation_();
}

Clark Lind

unread,
Jun 18, 2021, 8:54:58 AM6/18/21
to Google Apps Script Community
Just so I understand, you are trying to integrate a workspace add-on (Gmail, Cal, Drive) using Card service with an editor add-on (sheets) using html? 

Have you had much success so far? The triggers and actions for WorkSpace addons (WSA) is very limited I believe, and is context-based. I don't know that you can fire a function inside a WSA from outside it. Which workspace is your addon for, Gmail, Drive, Calendar?

Leandro Zubrezki

unread,
Jun 18, 2021, 9:02:41 AM6/18/21
to Google Apps Script Community
It is a Workspace Add-On that works with Sheets. When you need polling, there is no other option that to use the sidebar with HTML, as from there you can run intervals that calls back to Apps Script with google.script.run and get the information you need.

What I need is that once the user closes de Sidebar, either manually with the X button, or I close it from the withSuccessHandler after running a function with google.script.run, to reload the Workspace add on so I can show up to date information to the user.

Clark Lind

unread,
Jun 18, 2021, 9:31:29 AM6/18/21
to google-apps-sc...@googlegroups.com
Ok, sorry. Can you call whatever function is listed in the manifest for the sheets onHomepage trigger? Or is that already set to resetNavigation_()?

You might also try adding a flush() to ensure all Spreadsheet functions have finished:

function closeSidebar() {
    SpreadsheetApp.flush();
    return resetNavigation_();
}

--
You received this message because you are subscribed to a topic in the Google Groups "Google Apps Script Community" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-apps-script-community/UrkZ0AUxSAk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/f76c9572-dafa-4d0d-aa5b-cb5b1248e93cn%40googlegroups.com.

Leandro Zubrezki

unread,
Jun 18, 2021, 9:45:37 AM6/18/21
to Google Apps Script Community
You can call any public function from the HTML, but just because you execute it doesn't mean it actually updates the navigation.

If you return resetNavigation_ from a function that is executed from the Workspace Add on based on a user action, it will update the navigation. But if you for example call resetNavigation_ from a trigger, nothing will happen.

Here the problem is that a user pressing an html button is no a user action in the Workspace Add on, so even if I execute resetNavigation_, nothing happens.

In the example code the only one that actually resets the navigation is the one from openSidebar_, but I need to reload also after closing.

Clark Lind

unread,
Jun 18, 2021, 10:10:48 AM6/18/21
to google-apps-sc...@googlegroups.com
Ok got it. So maybe create a second html file that does nothing but close itself onLoad using javascript. You could modify your openSidebar function to accept an argument so it knows which html file to load..

Maybe:

function openSidebar_(arg) {
if (arg === "show") {  //normal open & show
    SpreadsheetApp.getUi().showSidebar(...);   //show main html file
    return resetNavigation_();
}
If (arg === "close") {
    SpreadsheetApp.getUi().showSidebar(...);   //show second html file that just closes itself via javascript document.onLoad( google.script.host.close() )  or something like that
    return resetNavigation_();
  }
}
This way, you are interacting with the UI again with a method you know already works to reset the addon.   

Hope that helps!

Leandro Zubrezki

unread,
Jun 18, 2021, 10:15:25 AM6/18/21
to Google Apps Script Community
That will not work, it is the same as before, you are calling the function from HTML so the reset of navigation won't actually happen. Thanks anyway, let's see if someone from Apps Script team see this and can help.

Leandro Zubrezki

unread,
Jun 18, 2021, 11:49:05 AM6/18/21
to Google Apps Script Community
Ok hacky but works.

Adding a sheet, writing a value and then removing the sheet forces and reload of the add ons. So in the closeSidebar function, I am also creating a sheet, adding a status message to the first cell and then removing the sheet. That causes the add on to be automatically reloaded when it is opened again 🎉

Clark Lind

unread,
Jun 18, 2021, 12:17:25 PM6/18/21
to google-apps-sc...@googlegroups.com
Cool. Sometimes hacks are all you have. :)  What about appending a row, and basically doing the same thing, would that trigger a reload? I'm asking because I'm also learning :)

Leandro Zubrezki

unread,
Jun 18, 2021, 12:26:20 PM6/18/21
to google-apps-sc...@googlegroups.com
You need to create a new sheet, after that you con do whatever you want.

--
Leandro Zubrezki
Reply all
Reply to author
Forward
0 new messages