如何在Google Apps脚本中从服务器调用函数? [英] How to call function from server in Google Apps Script?

查看:45
本文介绍了如何在Google Apps脚本中从服务器调用函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用GAS将表单嵌入到Google电子表格的边栏中.

I'm using a GAS to embed a form in a sidebar in a google spreadsheet.

在用户提交并确认表单后,如何关闭此侧边栏?

How do I close this sidebar after the form has been submitted and confirmed by the user?

在将数据发送到服务器之前完成验证.之后,用户必须使用是/否"警报框确认他要提交表单.

The validation is done before sending the data to the server. After that, the user has to confirm he wants to submit the form with a Yes/No alert box.

如果他单击是":边栏关闭,数据已提交

If he clicks yes: The sidebar closes and the data is submitted

如果他单击否:侧边栏保持打开状态,什么也没发生.

If he clicks no: The sidebar remains open and nothing happens.

在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'); ?>

使用此代码,无论用户单击是"还是否",侧边栏始终处于关闭状态.

With this code, the sidebar is always closed, no matter if the user clicks Yes or No.

推荐答案

更新后的答案

感谢其余的代码@NunoNogueira.我已经花了很多时间进行试验,并且确信问题出在 alert 的使用上.这可能与问题4428 有关,但我不确定.警报很可能覆盖了一些窗口上下文信息.清楚的是,服务器正在将 undefined 返回给successHandler,因此它在客户端无用.我的建议是在此工作流程中放弃使用 alert .

Thanks for the rest of the code, @NunoNogueira. I've spent considerable time experimenting with it, and I'm confident that the problem is with the use of the alert. This might be related to Issue 4428, but I can't be sure. It's likely that the alert is overriding some window context information. What is clear is that the server is returning undefined to the successHandler, so it's useless on the client side. My recommendation is to abandon use of alert for this work flow.

如果在验证表单之前坚持要求确认,则可以在客户端上使用 confirm():

If you insist on having a confirmation before validating the form, you can use a confirm() on the client:

function validar(){
  if (!confirm("Por favor, confirme")) return;
  ...


原始答案-正确的信息,但不是问题所在.


Original answer - correct information, but not what the problem was.

return false 的调用将触发 successHandler ,因为它是 return .

The call to return false will trigger the successHandler, because it is a return.

要改为调用 failureHandler ,服务器功能必须 FAIL .代替 return :

To invoke a failureHandler instead, the server function must FAIL. Instead of a return:

throw new Error( "Optional message" );

...,然后在您的HTML代码中添加 failureHandler .

... then add a failureHandler in your HTML code.

这篇关于如何在Google Apps脚本中从服务器调用函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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