五秒钟后自动关闭Google Apps脚本UiApp [英] Automatically close a Google Apps Script UiApp after five seconds

查看:102
本文介绍了五秒钟后自动关闭Google Apps脚本UiApp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在一定秒数后自动关闭此UiApp:

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;
}

谢谢!

推荐答案

调用doc.show(app)时会显示您创建的Ui,唯一可以更新或关闭它的方法是使用以a结尾的处理函数. return app.

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.

因此,由于只能返回"一次,因此无法从创建UI的同一函数中执行所需的操作.

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;
}

您可以使用相同的过程以任何方式修改UiApp实例,更改标签文本,添加小部件...任何您想要的东西.

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

这篇关于五秒钟后自动关闭Google Apps脚本UiApp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆