1

Is there a way to pass a value back to a HTML page while a server side function is running?

In the past when I was using the UI service, early in the function I would just create a label in the UI like:

app.createLabel('Working, hang on!');

And it would appear while the script was running, letting them know the script was running. When it got to certain parts of the script I would then be able to update/create a new message like:

app.createLabel('Halfway there, hold on.');

Or

app.createLabel('Fetched values, processing them');

etc., to tell them where we are if the script was running for a long time. An added bonus was that it could also update them on what direction the script was taking, so for example if a room booking script found that the first room was unavailable, it could pass a message back such as:

app.createLabel('First room was busy, checking other rooms');

And so on, and so forth.

Is there a way to do this with the HTML service? It seems my only options are success and failure handlers updating the HTML page when the function is complete, which don't seem to be what I want, as it's not a 'Real Time' message.

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
HDCerberus
  • 2,113
  • 4
  • 20
  • 36

1 Answers1

1

You can still use ui.alert() in sheets and docs, and sheets also has toast().

To keep the message within a sidebar or dialog that you've created using the HTML Service, you can set up a separate poller function on the client, to retrieve messages from the server every once in a while. Your long-running function could queue up a message via a function call on the server, which would be picked up by the poller on its next cycle, and displayed to the user.

See How to poll a Google Doc from an add-on for an example of a poller.

Community
  • 1
  • 1
Mogsdad
  • 44,709
  • 21
  • 151
  • 275
  • Interesting suggestion. Unfortunate that it adds a layer of complexity on to the relatively simple way that I had it previously, but it certainly looks as though it would work. Though you mention Docs & Sheets bound scripts in the example you've listed, I don't see any reason this wouldn't also work in a stand alone script, correct? – HDCerberus May 30 '15 at 17:05