0

Is there another way to update the gui than return app;?

I want to set the text on a label before doing an url fetch, like started downloading, and after it completes turn the label into download complete.

function EventHandler(e) {
  var app = UiApp.getActiveApplication();
  var url = e.parameter.URLInput;
  var label = app.getElementById("label");
  label.setText("Download started");
  try{
    var file = UrlFetchApp.fetch(url).getBlob();
   } catch(err){
    label.setText(err);
  }
  label.setText("Download finished");
  return app;
}

The label stays empty until UrlFetchApp is completed, and then the label's content is 'Download finished'. Adding return app; before the fetch ends the function.

MarijnS95
  • 4,703
  • 3
  • 23
  • 47

1 Answers1

1

You have to use a clientHandler to set Text to your label in the doGet function, the clientHandler executes immediately when you click the button.

Here is a test app that shows how it works: (online test available here with simulated download)

function doGet(){
  var app = UiApp.createApplication();
  var label = app.createLabel('---empty---').setId('label');
  app.add(label)
  var handler = app.createServerHandler('EventHandler');
  var cHandler = app.createClientHandler().forTargets(label).setText('starting download');
  var btn = app.createButton('start',handler).addClickHandler(cHandler);
  app.add(btn);
  return app;
}



function EventHandler(e) {
  var app = UiApp.getActiveApplication();
  var url = e.parameter.URLInput;
  var ulabel = app.getElementById("label");
  ulabel.setText("Download started");
  try{
    //var file = UrlFetchApp.fetch(url).getBlob();
  } catch(err){
    label.setText(err);
  }
  ulabel.setText("Download finished");
  return app;
}

note : you can use the same client handler to do lots of other usefull things : disable the button, show a spinner... whatever you like that must happen in the doGet function without delay.


EDIT following your comment

Have you tried using 2 server handlers in parallel ? in the displayHandler you could setup any condition you want, I left it simple in the following example :

function doGet(){
  var app = UiApp.createApplication();
  var label = app.createLabel('---empty---').setId('label');
  app.add(label)
  var handler = app.createServerHandler('EventHandler');
  var displayHandler = app.createServerHandler('displayHandler');
  var btn = app.createButton('start',handler).addClickHandler(displayHandler);
 // you can add other handlers (keypress, hover... whatever) they will all execute at the same time
  app.add(btn);
  return app;
}

function displayHandler(e) {
  var app = UiApp.getActiveApplication();
  var ulabel = app.getElementById("label");
  ulabel.setText("Download started");
  return app;
}

function EventHandler(e) {
  var app = UiApp.getActiveApplication();
  var url = e.parameter.URLInput;
  var ulabel = app.getElementById("label");
  try{
    Utilities.sleep(2000);// simulating download
  } catch(err){
    label.setText(err);
  }
  ulabel.setText("Download finished");
  return app;
}
Serge insas
  • 45,904
  • 7
  • 105
  • 131
  • Hi, thanks for your answer. I already have this for the button press, but how to do this for a keypress? The clienthandler is pretty limited in what it can do, and afaik I can only check for keyCode == 13 inside a server eventhandler. I already use the disabling of the button and textbox :) – MarijnS95 Oct 04 '13 at 08:22
  • When I answer a post here I never know what's the "tech level" of the guy who wrote the question (especially when it's a new SO user )... this time I was obviously talking to a a person who knows what he is talking about :-) my pleasure ! Nevertheless I suggested another possible way to do what you want, tell me if it does. – Serge insas Oct 04 '13 at 11:45
  • Hi Serge, thanks for your reaction! I simply implemented this in my program and it does exactly what I want! I also made the button activate the serverhandler for the display, so the response time stay's the same (it looks a bit awkward when pressing return takes a while before the textbox and button get deactivated and the label text changed, while pressing the button does this in an instant). – MarijnS95 Oct 04 '13 at 13:43
  • glad it helped , you're right about reaction time, it's indeed probably better if everything stays coherent. – Serge insas Oct 04 '13 at 14:00