从HTML网页获取数据到Google电子表格

我在html中有四个用户输入文本字段,用于创建用户输入数据 . 我想将这四个值传递给Google电子表格 . 此HTML是使用Google脚本创建的 . 我不熟悉谷歌的脚本,但看起来很糟糕,开发一个工具 . 任何人都可以帮我解决这个问题

回答(1)

3 years ago

这是一个简单的html文件,与电子表格中包含的Google Apps脚本进行通信 . html文件和Google Apps脚本相互通信,我将一个数组从html文件传递到Google Script . 希望这可以帮助 .

Code.gs文件:

function doGet()
{
  var html = HtmlService.createHtmlOutputFromFile('index');
  return html.setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL)

}

function getData(a)
{
  var ts = Utilities.formatDate(new Date(), "GMT-6", "M/d/yyyy' 'HH:mm:ss");
  a.splice(0,0,ts);
  var ss=SpreadsheetApp.openById('SPREADSHEETID')
  ss.getSheetByName('Form Responses 1').appendRow(a);
  return true;
}

function getURL()
{
  var ss=SpreadsheetApp.openById('SPREADSHEETID');
  var sht=ss.getSheetByName('imgURLs');
  var rng=sht.getDataRange();
  var rngA=rng.getValues();
  var urlA=[];
  for(var i=1;i<rngA.length;i++)
  {
    urlA.push(rngA[i][0]);
  }
  return urlA;
}

index.html文件:

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
  <div id="data">
    
Text 1<input name="t1" type="text" size="15" id="txt1" placeholder="Text 1" />
Text 2<input name="t2" type="text" size="15" id="txt2" placeholder="Text 2" />
Text 3<input name="t3" type="text" size="15" id="txt3" placeholder="Text 3" />
Text 4<input name="t4" type="text" size="15" id="txt4" placeholder="Text 4" />
<input type="radio" name="Type" value="Member" checked />Member
<input type="radio" name="Type" value="Guest" />Guest
<input type="radio" name="Type" value="Intruder" />Intruder
<input type="button" value="submit" id="btn1" />
<img id="img1" src="" alt="img1" width="300" /> </div> <div id="resp" style="display:none;"> <h1>Response</h1> <p>Your data has been received.</p> </div> <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <script> $(function() { $('#btn1').click(validate); $('#txt4').val(''); $('#txt3').val(''); $('#txt2').val(''); $('#txt1').val('') google.script.run .withSuccessHandler(setURL) .getURL(); }); function setURL(url) { $('#img1').attr('src',url[0]); } function setResponse(a) { if(a) { $('#data').css('display','none'); $('#resp').css('display','block'); } } function validate() { var txt1 = document.getElementById('txt1').value || ''; var txt2 = document.getElementById('txt2').value || ''; var txt3 = document.getElementById('txt3').value || ''; var txt4 = document.getElementById('txt4').value || ''; var type = $('input[name="Type"]:checked').val(); var a = [txt1,txt2,txt3,txt4,type]; if(txt1 && txt2 && txt3 && txt4) { google.script.run .withSuccessHandler(setResponse) .getData(a); return true; } else { alert('All fields must be completed.'); } } function loadTxt(from,to) { document.getElementById(to).value = document.getElementById(from).value; } function radioValue() { var radios = document.getElementsByName('genderS'); for (var i = 0, length = radios.length; i < length; i++) { if(radios[i].checked) { return radios[i].value; } } } console.log('My Code'); </script> </body> </html>