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.
MIT License
1.32k stars 171 forks source link

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

Open semireg opened 1 year ago

semireg commented 1 year ago

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?

jmatthewpryor commented 4 months ago

@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?

jmatthewpryor commented 4 months ago

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?

enuchi commented 4 months ago

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?

jmatthewpryor commented 4 months ago

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

enuchi commented 4 months ago

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

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

jmatthewpryor commented 4 months ago

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?

enuchi commented 4 months ago

Correct

jmatthewpryor commented 4 months ago

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