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

时间:2015-01-11 14:50:51

标签: google-apps-script google-sheets

我有一些可行的代码,但我需要引入一个表单,而不是我的硬编码文本。我正在使用Google Apps脚本的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 
  }

如果我记得&#34; //&#34;

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

尝试类似

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

我没有获得表单值

我希望有人可以告诉我从输入表单中获取值的方法,以便将其插入到电子表格中。

1 个答案:

答案 0 :(得分:7)

我刚从+ 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]]);  
}