简体   繁体   中英

Automatically close a Google Apps Script UiApp after five seconds

I want to automatically close this UiApp after a certain number of seconds:

function showConfirmationDialogue() {
  var app = UiApp.createApplication().setHeight('80').setWidth('400');
  app.setTitle('test');
  var panel = app.createVerticalPanel();
  app.add(panel);

  var doc = SpreadsheetApp.getActive();
  doc.show(app);

  // this part doesn't seem to work
  Utilities.sleep(5000);
  app.close();
  return app;
}

Thanks!

The Ui you create is shown when you call doc.show(app) and the only way you can update it or close it is to use a handler function that ends with a return app .

So it is not possible to do what you want from the same function that creates the UI since it is "returned" only one time.

I know only one trick that can achieve what you want that is using a handler trigger source that will call a closing handler function automatically using a "special" property of the checkBox widget . Here is the code, it uses a checkBox that you can of course make invisible in your final code.

function showConfirmationDialogue() {
  var app = UiApp.createApplication().setHeight('80').setWidth('400');
  app.setTitle('test');
  var panel = app.createVerticalPanel();
  app.add(panel);
  var handler = app.createServerHandler('closeWindow');
  var chk = app.createCheckBox('checkBox to set invisible in real function').setValue(false,true).addValueChangeHandler(handler);
  app.add(chk);
  chk.setValue(true,true)//.setVisible(false);
  var doc = SpreadsheetApp.getActive();
  doc.show(app);
}

function closeWindow(){
  Utilities.sleep(5000);
  var app = UiApp.getActiveApplication().close();
  return app;
}

You can use the same procedure to modify the UiApp instance in any way, change a Label text, add a widget... anything you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM