我在Google电子表格的脚本中有以下代码:
var uiInstance;
function displayDialog() {
uiInstance = UiApp.createApplication()
.setWidth(130)
.setHeight(130);
uiInstance.add(uiInstance.createLabel("foo"));
SpreadsheetApp.getUi().showModalDialog(uiInstance, 'bar');
}
此对话框旨在通知用户脚本正在计算某些内容,并且我希望在脚本完成其工作后再次关闭对话框。如果我使用
uiInstance.close();
功能内部或外部似乎没有发生;对话框保持打开状态,直到用户关闭它。我的问题有解决办法吗?
答案 0 :(得分:1)
您必须“返回”以有效关闭uiInstance,需要return
来反映您对Ui所做的任何更改,包括关闭它。
试
return uiInstance.close();
按照您的评论编辑:
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();
}
答案 1 :(得分:0)
使用:
uiInstance = uiInstance.close();
SpreadsheetApp.getUi().showModalDialog(uiInstance, 'bar');