Who8MyLunch Who8MyLunch - 24 days ago 20
Javascript Question

Auto refresh Google Apps Scripts webapp UI?

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?

Answer

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.