简体   繁体   English

如何在Google Apps脚本中从服务器调用函数?

[英]How to call function from server in Google Apps Script?

I'm using a GAS to embed a form in a sidebar in a google spreadsheet. 我正在使用GAS将表单嵌入Google电子表格的边栏中。

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. 如果他单击否:侧栏保持打开状态,并且什么也没有发生。

In code.gs: 在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" ); 
  }
}

In html: 在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. 使用此代码,无论用户单击“是”还是“否”,边栏始终处于关闭状态。

Updated answer 更新的答案

Thanks for the rest of the code, @NunoNogueira. 感谢其余的代码@NunoNogueira。 I've spent considerable time experimenting with it, and I'm confident that the problem is with the use of the alert . 我已经花了很多时间进行试验,并且确信问题出在使用alert This might be related to Issue 4428 , but I can't be sure. 这可能与Issue 4428有关 ,但我不确定。 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. 清楚的是,服务器正在将undefined的返回给successHandler,因此它在客户端无用。 My recommendation is to abandon use of alert for this work flow. 我的建议是在此工作流程中放弃使用alert

If you insist on having a confirmation before validating the form, you can use a confirm() on the client: 如果您在确认表单之前坚持要求确认,则可以在客户端上使用confirm()

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

Original answer - correct information, but not what the problem was. 原始答案-正确的信息,但不是问题所在。

The call to return false will trigger the successHandler , because it is a return . return false的调用将触发successHandler ,因为它是一个return

To invoke a failureHandler instead, the server function must FAIL . 要改为调用failureHandler ,服务器功能必须FAIL Instead of a return : 而不是return

throw new Error( "Optional message" );

... then add a failureHandler in your HTML code. ...然后在您的HTML代码中添加一个failureHandler

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM