0

For the Sample Code below would like to be able to set the label 'statMsg' to a different value(time or a counter or a status) once every minute. But do not have 'app' access from testTrigger():

//Trigger every minute
function createTriggerInvite() {
  ScriptApp.newTrigger('testTrigger')
      .timeBased()
      .everyMinutes(1)
      .create();
}
function testTrigger(){
  //NOT WORKING
  var app=UiApp.getActiveApplication();
  app.getElementById('statMsg').setText('Time');
  return app;
}

function doGet(e) {
  var dbgtime=true;
  dbgtime=false;
  //var dbg=true;
  createTriggerInvite(); 
  if (dbg==true)log('doget','Starting');
  var app = UiApp.createApplication();
  var stat=app.createLabel('Status...').setWidth(50)
           .setStyleAttribute('color', 'white')
           .setHorizontalAlignment(UiApp.HorizontalAlignment.LEFT)
           .setId('statMsg'));
  app.add(stat);
  return app;
}

1 Answers1

0

UiApp widgets can't be updated by anything else than events happening in the UiApp instance, not by triggers or other external app.

There is a way to achieve auto update in UiApp but it uses a different approach.

The idea is to change the state of a widget that will fire an event handler. The timing will be defined in a sleep call.

Here is an example (already published here, the OP explains better the reason I used this) that uses a checkBox to initiate the event that calls the server handler :

function doGet() {
  var app = UiApp.createApplication().setTitle('Counter/Timer');
  var Panel = app.createAbsolutePanel().setStyleAttribute('padding','35');
  var counter = app.createHTML().setId('counter').setHTML('<B>Timer = wait</B>').setStyleAttribute('fontSize','40px');// set start display
  var clo = app.createTextBox().setName('clo').setId('clo').setValue('0').setVisible(false);//set start value in seconds
  var handler1 = app.createServerHandler('doSomething').addCallbackElement(Panel);
  var chk1 = app.createCheckBox('test1').addValueChangeHandler(handler1).setVisible(true).setId('chk1').setVisible(false);
  app.add(Panel.add(chk1).add(counter).add(clo));
  chk1.setValue(true,true);// start the process
  return app}

function doSomething(e) {
  var app = UiApp.getActiveApplication();
  var xx = Number(e.parameter.clo);
  var disp = app.getElementById('counter')
  xx++ ;// replace by xx-- to count downwards
  if(xx>600){ // 10 minutes timeout for example
  disp.setHTML('<B> GAME OVER ;-)</B>').setStyleAttribute('fontSize','80px').setStyleAttribute('color','RED')
  return app
  }
  var cnt = app.getElementById('clo').setValue(xx)
  disp.setHTML('<B>'+T(xx)+'</B>')
  Utilities.sleep(1000); // instead of sleeping do something !
// below comes the "active" part
  var chk1 = app.getElementById('chk1').setValue(false,false)
  var chk1 = app.getElementById('chk1').setValue(true,true)
  return app;
}

function T(val){
  var min = parseInt(val/60);
  var sec = val-(60*min);
  if(sec<10){sec='0'+sec}
  if(min<10){min='0'+min}
  var st = '>  '+min+':'+sec
  return st
}

demo online here

Community
  • 1
  • 1
Serge insas
  • 45,904
  • 7
  • 105
  • 131