簡體   English   中英

如何在Google Apps腳本中從服務器調用函數?

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

我正在使用GAS將表單嵌入Google電子表格的邊欄中。

表單提交並由用戶確認后,如何關閉此側邊欄?

驗證是在將數據發送到服務器之前完成的。 之后,用戶必須使用“是/否”警報框確認他要提交表單。

如果他單擊“是”:側邊欄關閉,數據已提交

如果他單擊否:側欄保持打開狀態,並且什么也沒有發生。

在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 這可能與Issue 4428有關 ,但我不確定。 警報很可能覆蓋了一些窗口上下文信息。 清楚的是,服務器正在將undefined的返回給successHandler,因此它在客戶端無用。 我的建議是在此工作流程中放棄使用alert

如果您在確認表單之前堅持要求確認,則可以在客戶端上使用confirm()

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

原始答案-正確的信息,但不是問題所在。

return false的調用將觸發successHandler ,因為它是一個return

要改為調用failureHandler ,服務器功能必須FAIL 而不是return

throw new Error( "Optional message" );

...然后在您的HTML代碼中添加一個failureHandler

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM