使用html对话框中的输入字段中的值使用Google Apps脚本在code.gs中使用var

我有一个对话框,用户可以在其中选择年份。然后,我希望服务器在函数doSomethingWithCompetitionYear(theYear)中处理选定的值。

查看了几次讨论,但无法正常进行。看起来我需要对.withSuccesHandler()做些事情。

Code.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 doc

<!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()服务器函数。

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

code.gs:

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

使用html对话框中的输入字段中的值使用Google Apps脚本在code.gs中使用var

我有一个对话框,用户可以在其中选择年份。然后,我希望服务器在函数doSomethingWithCompetitionYear(theYear)中处理选定的值。

查看了几次讨论,但无法正常进行。看起来我需要对.withSuccesHandler()做些事情。

Code.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 doc

<!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()服务器函数。

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

code.gs:

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