Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google apps script uiInstance.close()无效_Google Apps Script - Fatal编程技术网

Google apps script uiInstance.close()无效

Google apps script uiInstance.close()无效,google-apps-script,Google Apps Script,我在谷歌电子表格的脚本中有以下代码: var uiInstance; function displayDialog() { uiInstance = UiApp.createApplication() .setWidth(130) .setHeight(130); uiInstance.add(uiInstance.createLabel("foo")); SpreadsheetApp.getUi().showModalDialog(uiInstance, 'bar'); }

我在谷歌电子表格的脚本中有以下代码:

var uiInstance;

function displayDialog() {
 uiInstance = UiApp.createApplication()
  .setWidth(130)
  .setHeight(130);
 uiInstance.add(uiInstance.createLabel("foo"));
 SpreadsheetApp.getUi().showModalDialog(uiInstance, 'bar');
}
此对话框旨在通知用户脚本正在计算某些内容,我希望在脚本完成其工作后再次关闭该对话框。如果我使用

uiInstance.close();
在功能内部或外部,似乎什么也没有发生;对话框将保持打开状态,直到用户将其关闭。我的问题有什么解决方案吗?

您必须“返回”才能有效地关闭uiInstance,需要返回以反映您对Ui所做的任何更改,包括关闭它

试一试


编辑以下评论:

UiApp实例只能从处理程序函数关闭,我原以为这就是您使用它的方式(但我可能错了)

下面是一个小代码示例:

function displayDialog() {
  var uiInstance = UiApp.createApplication()
  .setWidth(130)
  .setHeight(130);
  uiInstance.add(uiInstance.createLabel("foo"));
  var handler = uiInstance.createServerHandler('closeDialog');
  uiInstance.add(uiInstance.createButton('close',handler));
  SpreadsheetApp.getUi().showModalDialog(uiInstance, 'bar');
}

function closeDialog(){
  return UiApp.getActiveApplication().close();
}
还有一个棘手的解决办法,可以“模拟”用户操作。它使用一些小部件的属性在它们更改值时触发处理程序函数。在下面的示例中,我使用了一个复选框来启动流程

当doStuf中的任务完成时,它将关闭对话框

function displayDialog() {
  var uiInstance = UiApp.createApplication()
  .setWidth(130)
  .setHeight(130);
  uiInstance.add(uiInstance.createLabel("foo"));
  var handler = uiInstance.createServerHandler('doStuf');
  var chk = uiInstance.createCheckBox().setValue(true).setId('chk').setVisible(false);
  chk.addValueChangeHandler(handler);
  uiInstance.add(chk);
  chk.setValue(false,true)// This actually calls the doStuf function (using the handler)
  SpreadsheetApp.getUi().showModalDialog(uiInstance, 'bar');
}

function doStuf(){
  Utilities.sleep(5000);// replace with something useful ...
  return UiApp.getActiveApplication().close();
}
使用:


也许我不明白你的意思,但我还是不能让它工作;如果我有一个类似{displayDialog();doStuff();closeDialog();}的函数,那么closeDialog()应该是什么样的呢?我还需要更改displayDialog()吗?谢谢=)!我之所以需要它,是因为“doStuff”任务需要相当长的时间才能完成(>1分钟),我想让用户知道它是否仍在工作
function displayDialog() {
  var uiInstance = UiApp.createApplication()
  .setWidth(130)
  .setHeight(130);
  uiInstance.add(uiInstance.createLabel("foo"));
  var handler = uiInstance.createServerHandler('doStuf');
  var chk = uiInstance.createCheckBox().setValue(true).setId('chk').setVisible(false);
  chk.addValueChangeHandler(handler);
  uiInstance.add(chk);
  chk.setValue(false,true)// This actually calls the doStuf function (using the handler)
  SpreadsheetApp.getUi().showModalDialog(uiInstance, 'bar');
}

function doStuf(){
  Utilities.sleep(5000);// replace with something useful ...
  return UiApp.getActiveApplication().close();
}
uiInstance = uiInstance.close();
SpreadsheetApp.getUi().showModalDialog(uiInstance, 'bar');