enuchi / React-Google-Apps-Script

This is your boilerplate project for developing React apps inside Google Sheets, Docs, Forms and Slides projects. It's perfect for personal projects and for publishing complex add-ons in the Google Workspace Marketplace.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Best Practice for Server --> Client via Scriptlets, onSelectionChange

semireg opened this issue · comments

commented

If I add a property to a template in ui.js such as:

  const template = HtmlService.createTemplateFromFile('sidebar');
  template.uuid = 'abc123';
  const html = template.evaluate();
  html.setTitle('My Sidebar');
  SpreadsheetApp.getUi().showSidebar(html);

How do I access "uuid" in React via Scriptlets?

I've tried modifying sidebar/index.html

<script>
      window.uuid = JSON.parse(<?= uuid ?>);
      console.log(`window.uuid:${window.uuid}`);
</script>

But I'm not seeing anything. It's probably obvious... 😄

On another note... is there any way to push data from server to client when onSelectionChange is called?

@semireg did you ever work out how to solve this?

On another note... is there any way to push data from server to client when onSelectionChange is called?

Hi @enuchi - can you make any suggestions about how to cause updates to a sidebar UI when the user changes selection and an onSelectionChange() server side SimpleTrigger is called?

Not sure. You'll have to get creative because I doubt there's an official way to do this.

You could try writing to the properties service from the trigger and polling changes from the UI?

Thanks @enuchi - how would you suggest implementing the polling on the client side?

Something like setInterval(() => {serverFunctions.checkIfUpdated.then(...)}, 3000)

https://developer.mozilla.org/en-US/docs/Web/API/setInterval

I've got polling working fine, and it calls back a serverFunction to get the active sheet & row, but I can't see how to use the PropertiesService from the client code?

Or would that just be to hold the "updated state" in the server code and then on interval the client would call that back & grab any updated state (if it was updated) - calling via a server function?

Correct

thanks
FWIW this node package worked a treat for the polling/interval
https://www.npmjs.com/package/@use-it/interval