Refresh Card (sidebar) when new Sheet is added or deleted (Workspace Add-on)

342 views
Skip to first unread message

N A

unread,
Oct 25, 2022, 3:49:56 AM10/25/22
to Google Apps Script Community
Hi,

This is a Workspace Add-on which is using a card to build the sidebar.

Is it possible to refresh the sidebar card when a new sheet is added or deleted? My script run without error except it doesn't update the sidebar card.

The sample code below does the following:

1. Create an Installable Event-trigger onChange
2. Display 'hello' text in sidebar card
3. When new sheet is created
4. It executes the myOnChange() function but does not refresh the sidebar.

Expected output should display "hi" text in the sidebar card.


function onHomepage() {
  createOnChangeTrigger();

  return greetCard('hello');        
}

function createOnChangeTrigger() {
  const triggers = ScriptApp.getUserTriggers(SpreadsheetApp.getActiveSpreadsheet());

  if (triggers.length > 0) {
    return;
  }

  ScriptApp.newTrigger('myOnChange')
    .forSpreadsheet(SpreadsheetApp.getActive())
    .onChange()
    .create();
}

function myOnChange() {
  const card = greetCard('hi');
 
  const newcard = CardService.newNavigation()
    .popToRoot()
    .pushCard(card))

  return CardService.newActionResponseBuilder()
    .setNavigation(newcard)
    .build();
}

function greetCard(text) {
  Logger.log('greet: %s', text)

  let content = CardService.newDecoratedText().setText(text);
  let section = CardService.newCardSection().addWidget(content);

  return CardService.newCardBuilder()
      .addSection(section)
      .build();
}


Does Installable triggers run on a different context that's why it can't update the card?

Thanks

Clark Lind

unread,
Oct 25, 2022, 10:09:58 AM10/25/22
to Google Apps Script Community
What happens if you reverse the order in which you run the functions? 

Also, there is an extraneous ")"  after  .pushCard(card)   see if that is causing any problems.

N A

unread,
Oct 25, 2022, 6:53:28 PM10/25/22
to Google Apps Script Community
Yeah! I forgot to remove the extra ")" I tried to edit my previous post but I can't do that, but anyway, I'm sorry but what do you mean by reverse the order...?

Clark Lind

unread,
Oct 26, 2022, 9:32:50 AM10/26/22
to Google Apps Script Community
Disregard what I said about reversing the order.
Does each function work independently? try making it an async function:

async function onHomepage() {
  await createOnChangeTrigger();

  return greetCard('hello');        
}

N A

unread,
Oct 26, 2022, 6:47:37 PM10/26/22
to Google Apps Script Community
Thank you, will test this one.

Jason Cobble

unread,
Oct 26, 2022, 8:46:38 PM10/26/22
to google-apps-sc...@googlegroups.com
I don't know who's added me or using my account to write you but it isn't me. I can be contacted at jas...@cobbletechnologies.com
In regards to this. 

Jason



--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, 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/f4d2f696-358a-4da5-a567-afae52ee0742n%40googlegroups.com.

N A

unread,
Nov 5, 2022, 8:50:26 AM11/5/22
to Google Apps Script Community
Hi,

I'm sorry I've just tried this but this doesn't work.

Thanks

On Wednesday, October 26, 2022 at 9:32:50 PM UTC+8 cwl...@gmail.com wrote:

N A

unread,
Nov 5, 2022, 11:46:19 PM11/5/22
to Google Apps Script Community
I conclude that I cannot refresh or update the Google Workspace Add-on card from a Trigger function because it looks like the trigger function is running from server.

Clark Lind

unread,
Nov 6, 2022, 8:34:55 AM11/6/22
to Google Apps Script Community
Have you thought about adding an html doGet() function to your script? I don't know if it will work, but here is the concept:

When Spreadsheet is changed, and onChange trigger fires. 
The onChange trigger calls the URLFetch method. 
The URLFetch method calls the published url of the DoGet function.
The doGet function calls the script's onHomePage() function (or other refresh function). 

This is just a concept and I have not tested it. But in theory, you should be able to talk the the cardservice if the cardservice script contains a doGet function.
Reply all
Reply to author
Forward
0 new messages