Automatically close a Google Apps Script UiApp after five seconds

百般思念 提交于 2019-12-11 09:51:54

问题


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!


回答1:


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.



来源:https://stackoverflow.com/questions/21483652/automatically-close-a-google-apps-script-uiapp-after-five-seconds

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!