繁体   English   中英

如何在Google App脚本函数中使用多个参数,以及如何在HTMLService中选中哪个单选框?

[英]How to use multiple arguments in Google App Script function and check what radio box is checked across HTMLService?

HTML页面:

  <html>
      <body>
    <div id="addGamePrompt">
    <form>
    First Pick Team: 
    <div id="containerT1"><input type="text" id="team1" name="team1"> My Team <input type="radio" name="myTeam" value="team1" id="checkTeam1" checked></div>
    <br><br>
    Second Pick Team: 
    <div id="containerT2"><input type="text"id="team2" name="team2"> My Team <input type="radio" name="myTeam" value="team2" id="checkTeam2">
    <br></div>
</form>
</div>

    <input id="submitBut" type="button" onClick="formSubmit()" value="Ok"/>
    <script type="text/javascript">

        function formSubmit() {
        google.script.run.getTeams(document.forms[0], 1);
        }

    </script>

  </body>
</html>

Google App脚本

function myFunction() {
     // Display a modal dialog box with custom HtmlService content.
     var htmlOutput = HtmlService
         .createHtmlOutputFromFile('addGamePrompt.html')
         .setWidth(300)
         .setHeight(130);
     SpreadsheetApp.getUi().showModalDialog(htmlOutput, 'Enter Teams');

    }
    function getTeams(form,flag1){
      var arg1 = arguments[0]; //form argument value
      var arg2 = arguments[1]; //flag1 argument value
      var ss = SpreadsheetApp.getActiveSpreadsheet();
      var team1 = arg1.team1; //gets value of form in HTML page
      var team2 = arg1.team2; //see above
      var flagCheck = arg2; //sets flagCheck as whatever argument flag1 is
      ss.toast(team1); //prints out in box the value of team1
    }

我希望能够传递文本框的值,并检查是否通过google app脚本选中了哪个单选框(如果第一个是flag1 = 1,如果第二个是flag1 = 2),则什么都没有发生。

注意:在Google表格中使用此功能。

我将单选按钮的名称更改为:

name="myTeam1"
name="myTeam2"

您无需发送第二个参数,只需使用:

google.script.run.getTeams(document.forms[0]);

您的服务器端代码应更改为:

function getTeams(form){
  //To see what is printed to the Log, use the View menu, and Logs
  Logger.log('team1: ' + form.team1);
  Logger.log('myTeam1: ' + form.myTeam1);
  Logger.log('team2: ' + form.team2);
  Logger.log('myTeam2: ' + form.myTeam2);

  var team1 = form.team1; //gets value of form in HTML page
  var team2 = form.team2; //see above

  var myTeam1 = form.myTeam1;//Get value of radio button
  var myTeam2 = form.myTeam2;

  //Whichever radio button is NOT checked will have a value of undefined
  var flagCheck = myTeam1?team1:team2; //sets flagCheck as whatever radio button is checked

  Logger.log('flagCheck: ' + flagCheck);

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  ss.toast(team1); //prints out in box the value of team1
}

如果表单元素是通过google.script.run.getTeams(formElement);传递的google.script.run.getTeams(formElement); 那么它必须是唯一的参数。 如果要传递表单元素,则不能有两个或更多参数。 因此,我将创建一个新对象,将表单答案放入对象中,将任何其他数据或答案放入新对象中,然后将该对象传递给GS服务器函数。

var dataObject = {};
dataObject.value1 = answerOne;
dataObject.value2 = answerTwo;
dataObject.value3 = answerThree;

google.script.run.getTeams(dataObject);

暂无
暂无

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

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