Google apps script 自动关闭模式对话框-完成服务器代码后,关闭谷歌电子表格中的对话框
我有一个谷歌工作表,它运行一些应用程序脚本服务器代码来连接到SQL服务器。我想在刷新数据时在模式对话框中显示消息“正在加载…”。我可以让模态弹出,但我想在代码完成后自动关闭对话框 我举了一个例子:Google apps script 自动关闭模式对话框-完成服务器代码后,关闭谷歌电子表格中的对话框,google-apps-script,google-sheets,showmodaldialog,Google Apps Script,Google Sheets,Showmodaldialog,我有一个谷歌工作表,它运行一些应用程序脚本服务器代码来连接到SQL服务器。我想在刷新数据时在模式对话框中显示消息“正在加载…”。我可以让模态弹出,但我想在代码完成后自动关闭对话框 我举了一个例子: function testpop () { var htmlOutput = HtmlService .createHtmlOutput('<p> This box will close when the data has finished loading.</p>
function testpop () {
var htmlOutput = HtmlService
.createHtmlOutput('<p> This box will close when the data has finished loading.</p>')
.setSandboxMode(HtmlService.SandboxMode.IFRAME)
.setWidth(250)
.setHeight(200);
SpreadsheetApp.getUi().showModalDialog(htmlOutput, 'Loading...');
sleep(1000);
//close the dialog
}
函数testpop(){
var htmlOutput=HtmlService
.createHtmlOutput(“数据加载完成后,此框将关闭。”)
.setSandboxMode(HtmlService.SandboxMode.IFRAME)
.setWidth(250)
.设置高度(200);
SpreadsheetApp.getUi().showModalDialog(htmlOutput,'Loading…');
睡眠(1000);
//关闭对话框
}
我知道这可以在客户端调用,但需要在GS中处理,以便在代码完成时触发。事件流可以是:
- 用户做了什么
- 触发模态对话框
模态对话框事件触发客户端代码onLoad
- 客户端
触发服务器端google.script.run
函数运行.gs
- 运行
脚本文件中的服务器功能.gs
- 数据库已从服务器更新
- 服务器代码将返回值发送回对话框
- 对话框中的“withSuccessHandler()”检测来自服务器的返回
- “withSuccessHandler()”使用
google.script.host.close()运行并关闭对话框代码>
标记
<script>
window.onload = function() {
//console.log('window.onload ran!');
google.script.run
.withSuccessHandler(closeDialog)
.theFunctionNameToUpdateDatabase()
};
window.closeDialog = function() {
google.script.host.close();
};
</script>
您可以改为从文件创建HTML:
HtmlService.createHtmlOutputFromFile(filename)
//显示对话框
var output = HtmlService.createHtmlOutput('<b>Please wait...</b>');
SpreadsheetApp.getUi().showModalDialog(output, 'Saving...');
我也同意@pcw11211给出的答案。我将htmlmodalDialog调用封装在一个可以打开或关闭对话框的函数中,从而稍微修饰了一下它。我也添加了一些html样式。还要注意htmlmodalDialog周围的try/catch,这样当您从脚本编辑器进行测试时,脚本仍然可以运行,否则主脚本有时会(?)由于上下文错误而停止
/**
* HELPER FUNCTION : TO OPEN & CLOSE MODAL DIALOGUE
*/
function htmlmodalDialog(title, text, close){
var htmlText = '<div>' + text + '</div>';
htmlText += '<style type="text/css">';
htmlText += 'body{text-align: center; font-family: Roboto, Arial, sans-serif; font-size: 14px;}';
htmlText += 'div{margin: auto;}';
htmlText += '</style>';
if(close){htmlText += '<script>google.script.host.close();</script>';}
var htmlOutput = HtmlService
.createHtmlOutput(htmlText)
.setHeight(60)
.setWidth(200);
try {
SpreadsheetApp.getUi().showModalDialog(htmlOutput, title);
}catch(e){
Logger.log('function htmlmodalDialog(title, text, close)');
Logger.log(e);
}
希望这对其他人有所帮助。此解决方案提供了一个立即关闭对话框的按钮,并将在5000毫秒(5秒)后通过在body标记中使用“onload=”setTimeout(closer,5000)”自动关闭。它将向HTML传递一个google脚本变量以自定义对话框 这是我想到的 我的GS文件:
function myGSFunction(inputVariable){
const ui = SpreadsheetApp.getUi();
let gsVariable = inputVariable.toUpperCase().trim() || null;
let htmlOutput;
.
.
.
htmlOutput = HtmlService.createTemplateFromFile('errorModelessDialog');
htmlOutput.htmlVariable = gsVariable; //define variable accessible in html
ui.showModelessDialog(htmlOutput.evaluate().setWidth(400).setHeight(150), 'ERROR');
.
.
.
};
My errorModelessDialog.html文件:
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body onload="setTimeout(closer, 5000)">
<p align="center"><?!=htmlVariable?> is not valid.</p>
<p align="center"><button onclick="google.script.host.close()">Close</button></p>
<script>
function closer(){
google.script.host.close();
}
</script>
</body>
</html>
无效
关闭
函数closer(){
google.script.host.close();
}
我喜欢这个。我认为这是一个更干净的解决方案。谢谢!这比(目前)公认的答案简单得多,也更可靠。所有要点都指向你。
/**
* HELPER FUNCTION : TO OPEN & CLOSE MODAL DIALOGUE
*/
function htmlmodalDialog(title, text, close){
var htmlText = '<div>' + text + '</div>';
htmlText += '<style type="text/css">';
htmlText += 'body{text-align: center; font-family: Roboto, Arial, sans-serif; font-size: 14px;}';
htmlText += 'div{margin: auto;}';
htmlText += '</style>';
if(close){htmlText += '<script>google.script.host.close();</script>';}
var htmlOutput = HtmlService
.createHtmlOutput(htmlText)
.setHeight(60)
.setWidth(200);
try {
SpreadsheetApp.getUi().showModalDialog(htmlOutput, title);
}catch(e){
Logger.log('function htmlmodalDialog(title, text, close)');
Logger.log(e);
}
htmlmodalDialog('Starting', 'Working ...', false);
//do something here
htmlmodalDialog('Finished', 'Operation completed.', true);
function myGSFunction(inputVariable){
const ui = SpreadsheetApp.getUi();
let gsVariable = inputVariable.toUpperCase().trim() || null;
let htmlOutput;
.
.
.
htmlOutput = HtmlService.createTemplateFromFile('errorModelessDialog');
htmlOutput.htmlVariable = gsVariable; //define variable accessible in html
ui.showModelessDialog(htmlOutput.evaluate().setWidth(400).setHeight(150), 'ERROR');
.
.
.
};
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body onload="setTimeout(closer, 5000)">
<p align="center"><?!=htmlVariable?> is not valid.</p>
<p align="center"><button onclick="google.script.host.close()">Close</button></p>
<script>
function closer(){
google.script.host.close();
}
</script>
</body>
</html>