获取要在Google电子表格中插入的表单输入文本值 [英] Get form input text value to insert in a Google Spreadsheet

查看:104
本文介绍了获取要在Google电子表格中插入的表单输入文本值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些可行的代码,但我需要引入而不是硬编码文本。我正在使用Google Apps脚本的HTML服务和Google Spreadsheet来尝试完成此目标。我希望专家能够分享一个解决方案。



我的index.html

 < div id =inputDiv> 
< form id =inputFormonsubmit =fncWriteInputData()>
< label>名称:< / label> < br />
< input type =texttabindex =1id =idFirstInputclass =inptFldplaceholder =Add a namerequired>< br />
< label>编号:< / label> < br />
< input type =texttabindex =2id =idSecondInputclass =inptFldplaceholder =Employee numberrequired>< br />>
< br />
< input type =submittabindex =3id =btnSubmitInputvalue =点击提交>
< / 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 run);
google.script.run
.fncRunAppScript(); //运行服务器端.gs函数
}

< / script>

我的code.gs看起来像

  var submissioSSKey ='1N6uShSxHwhFYrfTDhJyp8ohCHpvElVAOtuGdQv3kt8k'; 
函数doGet(){
返回HtmlService
.createTemplateFromFile('index')
.evaluate()
.setSandboxMode(HtmlService.SandboxMode.IFRAME);


函数getData(){
返回SpreadsheetApp
.openById(submissioSSKey)
.getActiveSheet()
.getDataRange()
.getValues();
}

函数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; //这个工程,但我需要在文本框中的值
// var value = value.idFirstInput; // nogo
// var value = index.html.inputForm.idFirstInput.getValue(); // nogo
var value2 =123;
// cell.setValues([[value1a,value2a]]); //这个工程
cell.setValues([[value,value2]]); //这个工程
}

如果我rem//

  var value =Frank; //这个工程,但我需要在文本框中的值

然后尝试类似

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

我没有得到表单值



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

我刚刚从+ Anees Hameed
https://plus.google.com/106533582137684473193 / posts / dNxkPDrFZB8


$ b Index.html

 < form id =myForm> 
< input id =firstNamename =firstNametype =text/>
onClick =google.script.run.withSuccessHandler(DataSaved).processForm(this.form)
/>
< input type =buttonvalue =Submit
< / form>
< div id =Message>< / div>
< script>
函数DataSaved(){
document.getElementById('Message')。innerHTML =保存的数据;
};
< / script>

Code.gs

 函数doGet(){
return HtmlService.createHtmlOutputFromFile('Index')
.setSandboxMode(HtmlService。 SandboxMode.IFRAME);
}

函数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]]);
}


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.

解决方案

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]]);  
}

这篇关于获取要在Google电子表格中插入的表单输入文本值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆