簡體   English   中英

使用 html 對話框中輸入字段的值使用 Google Apps 腳本在 code.gs 中進行 var

[英]use value from input field in html dialog box to var in code.gs using Google Apps Script

我有一個對話框,用戶可以在其中一年 select。 然后我希望服務器處理 function doSomethingWithCompetitionYear(theYear) 中的選定值。

看了幾個討論,但無法正常工作。 看起來我需要用.withSuccesHandler() 做點什么。

代碼.gs

function fncOpenMyDialog() {
  //Open a dialog
  var htmlDlg = HtmlService.createTemplateFromFile('DropDown_NewCompetitionFile');
  thisYear = new Date();
  htmlDlg.thisYear = thisYear.getFullYear();
  htmlDlg.nextYear = htmlDlg.thisYear + 1;

  htmlDlg = htmlDlg.evaluate()
      .setSandboxMode(HtmlService.SandboxMode.IFRAME)
      .setWidth(200)
      .setHeight(150);
  SpreadsheetApp.getUi()
      .showModalDialog(htmlDlg, 'Make selection');
};

function doSomethingWithCompetitionYear(theYear) {
  var ui = SpreadsheetApp.getUi();
  ui.alert(theYear);  
}

HTML 文檔

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>

Year
<select name="Competition_year" id="Competition_year" type="integer">
  <option value=<?= thisYear?>><?= thisYear?></option>
  <option value="nextYear"><?= nextYear?></option>
</select>

<hr/>

<button onmouseup="closeDia()">Submit</button>

<script>
  var theYear = document.getElementById("Competition_year").value;   
  google.script.run.doSomethingWithCompetitionYear();
  window.closeDia = function() {
    google.script.host.close();
  };
</script>


  </body>
</html>

這是一個簡單的樣板,用於使用.withSuccessHandler 做一些事情

我添加了一些 JQuery、一個 msgdiv、withSuccessHandler() 和一個 doSomethingWithCompetitionYear() 服務器 function。

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
      <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
      <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

  </head>
  <body>

Year
<select name="Competition_year" id="Competition_year" type="integer">
  <option value=<?= thisYear?>><?= thisYear?></option>
  <option value="nextYear"><?= nextYear?></option>
</select>

<hr/>

<button onmouseup="closeDia()">Submit</button>
<div id="msgdiv"></div>
<script>
  var theYear = document.getElementById("Competition_year").value;   
  google.script.run
  .withSuccessHandler(function(msg){
     document.getElementById("msgdiv").innerHTML=msg;
  }))
  .doSomethingWithCompetitionYear();
  window.closeDia = function() {
    google.script.host.close({year:$("#competition_year").val()});
  };
</script>
  </body>
</html>

代碼.gs:

function doSomethingWithCompetitionYear(obj) {
  return Utilities.formatString('I did something with this year: %s',obj.year);
}

情況:

如果我理解正確,您有一個包含兩個選項的對話框,並且您希望將有關所選選項(在本例中為20202021 )的信息傳遞給服務器端 function doSomethingWithCompetitionYear(); 單擊Submit按鈕時。

問題:

如果是這種情況,您實際上並不需要成功處理程序。 您只需將選定的值作為doSomethingWithCompetitionYear(theYear); .

此外,如果您希望在單擊Submit按鈕時發生這種情況。 您應該將此添加到closeDia function。 否則, doSomethingWithCompetitionYear(); 將在提交之前運行。

最后,如果您想通過下一年( 2021 ),而不是字符串“nextYear”,則必須對元素的值使用scriplets

修改 1. 明年值:

替換這個:

<option value="nextYear"><?= nextYear?></option>

為了這:

<option value=<?= nextYear?>><?= nextYear?></option>

修改2.點擊按鈕時調用服務器端function:

替換這個:

<script>
  var theYear = document.getElementById("Competition_year").value;   
  google.script.run.doSomethingWithCompetitionYear();
  window.closeDia = function() {
    google.script.host.close();
  };
</script>

為了這:

<script>
  window.closeDia = function() {
    var theYear = document.getElementById("Competition_year").value;   
    google.script.run.doSomethingWithCompetitionYear(theYear); // theYear parameter has to be passed to server-side function
    google.script.host.close();
  };
</script>

筆記:

  • 如果您想使用來自doSomethingWithCompetitionYear(theYear);的信息在客戶端,您應該使用成功處理程序,它可以調用客戶端 function,它將獲取此數據作為參數(由 google.script.run 調用的服務器端google.script.run本身不會返回任何內容,它需要一個回調函數)。 它會是這樣的:
google.script.run.withSuccessHandler(yourClientSideFunction).doSomethingWithCompetitionYear(theYear);

參考:

暫無
暫無

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

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