如何在Google Apps Script中从服务器调用函数
How to call function from server in Google Apps Script?
我正在使用 GAS 在谷歌电子表格的侧边栏中嵌入表单。
提交表单并经用户确认后,如何关闭此侧边栏?
验证是在将数据发送到服务器之前完成的。之后,用户必须确认他想要提交带有"是/否"警报框的表单。
如果他单击"是":侧边栏关闭并提交数据
如果他点击"否":侧边栏保持打开状态,没有任何反应。
在 code.gs:
function onOpen() {
SpreadsheetApp.getUi()
.createMenu('Test')
.addItem('New Test', 'showSidebar')
.addToUi()
}
function include (file) {
return HtmlService.createTemplateFromFile(file).evaluate().getContent();
}
function showSidebar() {
var html = HtmlService.createTemplateFromFile('Page')
.evaluate()
.setTitle('Sidebar')
.setWidth(200);
SpreadsheetApp.getUi()
.showSidebar(html);
}
function processF(form){
var ui = SpreadsheetApp.getUi();
var result = ui.alert(
'Please confirm',
ui.ButtonSet.YES_NO);
if (result == ui.Button.YES) {
Browser.msgBox('Yes');
return true;
} else{
return false;
//throw new Error( "Optional message" );
}
}
在 html 中:
<link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/jqueryui/1.11.1/themes/smoothness/jquery-ui.css" />
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/jquery-ui.min.js"></script>
<script>
function validar(){
try{
var form=document.getElementById('configs');
if(!document.getElementById('nome').value || !document.getElementById('datepicker').value){
return false;
} else {
this.disabled=true;
google.script.run
//.withFailureHandler(google.script.host.close)
.withSuccessHandler(function(closeTF){
if(closeTF==true){
google.script.host.close();
}
}).processF(form);
//google.script.host.close();
}
} catch(e){
alert('Erro: '+e.message);
}
};
</script>
<div class="painel">
<form id="configs" action="#">
<fieldset>
<label>Name</label>
<p><input type="text" name="nome" id="nome" minlength="2" required/></p>
<label>1-Choose date:</label>
<p><input type="text" name="datepicker" id="datepicker" required/></p>
<label>2-Choose:</label>
<p>
<select name="horizonte" id="horizonte">
<option value=1>1 Month</option>
<option value=2>2 Months</option>
<option value=3 selected="selected">3 Months</option>
<option value=6>6 Months</option>
</select>
</p>
<label>3-Choose:</label>
<p>
<select name="relatorio" id="relatorio">
<option value=1>Week</option>
<option value=2>Month</option>
</select>
</p>
<p>
<input type="submit" id="enviar" value="Submit" onclick="validar()" />
</p>
</fieldset>
</form>
</div>
<?!= include('Javascript'); ?>
使用此代码,无论用户单击"是"还是"否",侧边栏始终处于关闭状态。
更新了答案
感谢您的其余代码,@NunoNogueira。我花了相当多的时间对其进行实验,我相信问题出在alert
的使用上。这可能与问题 4428 有关,但我不确定。警报可能正在覆盖某些窗口上下文信息。很明显,服务器将undefined
返回到 successHandler,因此它在客户端毫无用处。我的建议是放弃在此工作流程中使用alert
。
如果您坚持在验证表单之前进行确认,则可以在客户端上使用confirm()
:
function validar(){
if (!confirm("Por favor, confirme")) return;
...
原始答案 - 正确的信息,但不是问题所在。
对return false
的调用将触发successHandler
,因为它是一个return
。
若要改为调用failureHandler
,服务器函数必须失败。而不是return
:
throw new Error( "Optional message" );
。然后在 HTML 代码中添加failureHandler
。
相关文章:
- HTML客户端可移植文件生成-无外部资源或服务器调用
- 我如何通过从服务器调用gif来检查是否有互联网连接
- 通过服务器调用Graph API来构建我自己的Facebook帖子数据库
- SignalR 不从服务器调用客户端方法
- AngularJS - 选择 - 服务器调用的默认值
- 从服务器调用确认对话框 ASP.NET.并返回服务器上的选择
- Meteor:从服务器调用相互依赖的外部 API 并将数据转储到 db
- 在键盘上的select2中按下向下箭头键,然后我必须避免服务器调用
- Angular Promise 在服务器调用完成之前返回
- 如何在Google Apps Script中从服务器调用函数
- 注释从 JSON 服务器调用接收的类
- 从服务器调用 JQuery 函数 (asp.net)
- Meteor.js:基于时间的服务器调用
- 发送请求以从其他服务器调用函数
- javascript从远程服务器调用webapi
- 如何使用socket.io从服务器调用客户端方法
- 您应该将HTML代码放入JavaScript中还是从服务器调用它
- 使用定义的阈值限制服务器调用次数
- 它能在JavaScript选项runat=“”中从另一台服务器调用js文件吗;服务器”;
- 通过 APACHE 服务器调用多个 perl 脚本