Auto refresh Google Apps Scripts webapp UI?

2019-09-06 14:10发布

问题:

I have created a simple webapp using Google Apps Scripts. The UI simply displays a couple of numbers being continuous updated in a FusionTable. A custom data acquisition system is running locally in my house and sending new results every minute up to the FusionTable. I wish to update the UI on my GAS webapp at one-minute intervals so that the new numbers are displayed.

I have half of the problem solved by running a time-driver trigger to periodically grab data from the FusionTable and store the numbers in my script's own ScriptDb. So this works fine to get my data in the right neighborhood. But I still do not see how to periodically update a textbox in my UI with the new data.

So far I have manually configured a refresh button on the UI that user can click to update the display with the most recent numbers stored in the ScriptDb. I am so close I can taste it!

Anyone have an idea?

回答1:

You can use the UiInstance.addTimer method for auto refresh, but it's not documented and maybe not supported.

Its syntax is UiInstance.addTimer( ServerHandler , interval);

See the sample code below.

function doGet(e) {

  var app = UiApp.createApplication();

  

  app.add(app.createLabel(new Date()).setId("label"));

  

  app.addTimer(app.createServerHandler("update") , 1000);

  

  return app;

}



function update(e){

  var app = UiApp.getActiveApplication();

  app.getElementById("label").setText(new Date());

  app.addTimer(app.createServerHandler("update") , 1000);


  return app;

}

A sample app can be found here.



回答2:

Use a spreadsheet for display. They autoupdate.

Stackoverflow, this is not a thanks but the solution, but for you:

  1. Make a spreadsheet
  2. add a service for posting
  3. have service update spreadsheet
  4. watch spreadsheet


回答3:

Unfortunately, Google Apps Script doesn't yet have a timer facility to update the UI at frequent intervals. However, there is a hack that I wrote a while back and is available in the old Apps Script forum at http://productforums.google.com/forum/#!topic/apps-script/2pqhh_eTIvQ

There have been refinements over that you can search for in stackoverflow, but the basic premise remains the same i.e. you exploit the fact that you can programatically change the value of a checkbox AND trigger its change handler.