Get form input text value to insert in a Google Sp

2019-01-20 15:27发布

问题:

I have some code that works but I need to bring in a form value instead of my hard coded text. I am using Google Apps Script's HTML Service and a Google Spreadsheet to try to accomplish the goal. I am hoping an expert would be kind enough to share a solution.

My 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>

My code.gs looks like

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 
  }

If I rem "//"

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

And try something like

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

I do not get the form value

I am hoping someone can show me the way to grab a value from an input form so that it can be inserted into the spreadsheet.

回答1:

I just came across this from +Anees Hameed at 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]]);  
}