简体   繁体   English

五秒钟后自动关闭Google Apps脚本UiApp

[英]Automatically close a Google Apps Script UiApp after five seconds

I want to automatically close this UiApp after a certain number of seconds: 我想在一定的秒数后自动关闭这个UiApp:

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 . 当您调用doc.show(app)时,会显示您创建的Ui,并且您可以更新它或关闭它的唯一方法是使用以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. 因此,无法从创建UI的同一函数中执行您想要的操作,因为它只返回一次。

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 . 我知道只有一个技巧可以实现你想要的,使用处理程序触发源,它将使用checkBox小部件的“特殊”属性自动调用结束处理函数。 Here is the code, it uses a checkBox that you can of course make invisible in your final code. 这是代码,它使用一个checkBox,当然你可以在最终代码中隐藏它。

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. 您可以使用相同的过程以任何方式修改UiApp实例,更改Label文本,添加小部件......您想要的任何内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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