Is it possible to programatically refresh a Workspace Add-On

808 views
Skip to first unread message

Paul Armstrong

unread,
May 20, 2022, 4:01:37 AM5/20/22
to Google Apps Script Community
Hi
A google workspace add-on has a Refresh menu option. I would like to do the same thing programmatically.

The challenge I am finding is while I can re-create the cards I want at the root, I can not replace/ remove the cards at the root. The best I have managed to do is update a card. But if I want to remove it, I can not.

The classes I have been using are Navigate and  UniversalActionResponseBuilder. Both have features to update/replace cards. But not remove the root cards. 

Thank you.



Clark Lind

unread,
May 20, 2022, 7:33:47 AM5/20/22
to Google Apps Script Community
Hi Paul,

I'm not sure what you are trying to do, but theoretically, you could use the scripts PropertyService to store some value like { 'first_run': false} to indicate a boolean. 
Then if say, it is a homepage card, you should be able to determine which card to show/build based on the property service. If it is the first_run, show card A, if it is the second or later run, show card B..  

Otherwise, I don't know how to actually remove a root card. That would be the only workaround I can think of. Assuming I'm even on the right track in understanding what you want to do.. :)

Clark Lind

unread,
May 20, 2022, 8:07:52 AM5/20/22
to Google Apps Script Community
I think it is limited because you are essentially trying to perform a browser-level function (e.g., page refresh). When the condition presents itself, you could always show a card asking the user to please refresh the add-on, and stop all add-on functioning.. lol that would force them to refresh. But is a pain as far as user experience goes. I tested the following and it worked to show the first card only once, then only the second card after that. Again, not sure if this is what you are after, but may give you some ideas.

Gmail add-on example:

function onHomepage() {
    var scriptProperties = PropertiesService.getScriptProperties();
    var runStatus = scriptProperties.getProperty('first_run');
    var cardTitle, cardImgUrl, cardName;

        if (!runStatus || runStatus == 'false') {
          
          scriptProperties.setProperty('first_run', 'true');

          cardTitle = "This is the first run"
          cardName = "First Root"
        } else {
          cardTitle = "This is the second run"
          cardName = "Second Root"
        }

    var cardHeader = CardService.newCardHeader()
        .setTitle(cardTitle)
        .setImageStyle(CardService.ImageStyle.CIRCLE)
        .setImageUrl(cardImgUrl);

    var card = CardService.newCardBuilder()
        .setName(cardName)
        .setHeader(cardHeader)
        .build();

    return card;
}

Paul Armstrong

unread,
Jun 11, 2022, 7:03:24 AM6/11/22
to Google Apps Script Community
Hi
The challenge I have is that the Root Card inside of a Workspace Add-On can not [it appears] be made to refresh its contents.

Removing other cards and rebuilding them is possible. But this is not true for the root card. 

Here is code poping to the Root Card.

static createGoHomeNavigation() {
   var res = CardService.newActionResponseBuilder();
   var nav = CardService.newNavigation().popToRoot();
   res.setNavigation(nav);
   return res.build();
}
This code is in an add-on for an Editor: Docs, Spreadsheets or Slides.  I have looked for some way to update the current/active document to force it to do a refresh of the add-on for me. eg, changing the active document's title or its description, for example, do nothing. 

Any suggested hacks are welcome.

Thank you.
Reply all
Reply to author
Forward
0 new messages