获取表单输入文本值以插入 Google 电子表格

Get form input text value to insert in a Google Spreadsheet

我有一些有效的代码,但我需要引入 value 形式,而不是我的硬编码文本。我正在使用 Google Apps Script 的 HTML 服务和 Google 电子表格来尝试实现目标。希望高手能给个解决办法。

我的index.html

<div id="inputDiv">
  <form id="inputForm" onsubmit="fncWriteInputData()">
    <label>Name:  </label> <br />
     <input type="text" tabindex="1" id="idFirstInput" class="inptFld" placeholder="Add a name" required><br />
     <label>Number:</label> <br />
     <input type="text" tabindex="2" id="idSecondInput" class="inptFld" placeholder="Employee number" required><br />
    <br />
    <input type="submit" tabindex="3" id="btnSubmitInput" value="Click To Submit">
  </form>
</div>
<div>
<hr>
<? var data = getData(); ?>
<table>
  <? for (var i = 0; i < data.length; i++) { ?>
    <tr>
      <? for (var j = 0; j < data[i].length; j++) { ?>
        <td><?= data[i][j] ?></td>
      <? } ?>
    </tr>
  <? } ?>
</table>
</div>
<script>
 fncWriteInputData=function(){
    var value = (inputForm.idFirstInput)
    console.log("fncWriteInputData ran");
    google.script.run
    .fncRunAppScript(); //Runs server side .gs function    
}

</script>

我的code.gs长得像

var submissioSSKey = '1N6uShSxHwhFYrfTDhJyp8ohCHpvElVAOtuGdQv3kt8k';
function doGet() {
  return HtmlService
      .createTemplateFromFile('index')
      .evaluate()
      .setSandboxMode(HtmlService.SandboxMode.IFRAME);
}

function getData() {
  return SpreadsheetApp
      .openById(submissioSSKey)
      .getActiveSheet()
      .getDataRange()
      .getValues(); 
}

function fncRunAppScript(inputForm){
 var ss = SpreadsheetApp.openById(submissioSSKey);
 var sheet = ss.getActiveSheet();
 var lastRow = ss.getLastRow();
 var cell = sheet.getRange(lastRow+1, 1, 1,2);
 var value= "Frank"; //this works but I need the value in the text box
//  var value = value.idFirstInput;//nogo
// var value= index.html.inputForm.idFirstInput.getValue();//nogo 
 var value2= "123"; 
// cell.setValues([[value1a,value2a]]);//this one works 
 cell.setValues([[value,value2]]);//this one works 
  }

如果我 rem "//"

var value= "Frank"; //this works but I need the value in the text box

然后试试

 var value= index.html.inputForm.idFirstInput.getValue();

我没有得到表单值

我希望有人能告诉我如何从输入表单中获取值,以便将其插入到电子表格中。

我刚从 +Anees Hameed 那里看到这个 在 https://plus.google.com/106533582137684473193/posts/dNxkPDrFZB8

Index.html

<form id="myForm">
<input id="firstName" name="firstName" type="text" />
<input type="button" value="Submit" 
 onClick="google.script.run.withSuccessHandler(DataSaved).processForm(this.form)"
        />
</form>
<div id="Message"></div>
 <script>
    function DataSaved(){    
    document.getElementById('Message').innerHTML = "Data Saved";
};
 </script>

Code.gs

function doGet() {
  return HtmlService.createHtmlOutputFromFile('Index')
    .setSandboxMode(HtmlService.SandboxMode.IFRAME);
}

function processForm(myForm) {
  var firstName = myForm.firstName;
  var ss = SpreadsheetApp.openById('ID'); 
  var sheet = ss.getSheetByName('Sheet1');
  var email = Session.getActiveUser().getEmail();
  sheet.getRange(sheet.getLastRow()+1, 1, 1, 3).setValues([[Date(), email, firstName]]);  
}