Add basic data to a Google Spreadsheet via a simple HTML Form

前端 未结 1 724
不思量自难忘°
不思量自难忘° 2020-12-09 07:15

I have a form with a Name and an Email address, that the user can fill out.

\"enter

相关标签:
1条回答
  • 2020-12-09 07:31

    Yes this is possible. Using some server side magic or not.
    But what's important is to know witch server we are talking about, your server or google server, because in the end it will go server side to be stored on the spreadsheet apps :)
    I think this is possible to do it in full JS (but it will be really painfull).
    The solution that I propose you here is to relay on a google apps script that will handle the server side magic in a really easy way: the demo:

    the HTML code on you site:

    <form  id="form" method="get" action="https://script.google.com/macros/s/AKfycbxeKTVlTkqSGLIRphBrzACjuWlfmejbPIG7NqBxx-7Us7lnqLw/exec" accept-charset="UTF-8">
        <input type="text" name="newletter_name" placeholder="Name">
        <input type="text" name="newletter_email" placeholder="Email">
        <input type="submit" value="Subscribe"/>
    </form>
    

    and here the google apps script:

    function doGet(e){
      var vals=[];
      vals.push(new Date());
      for(var i in e.parameter){
        vals.push(e.parameter[i]);
      }
      SpreadsheetApp.openById("0Ao02g19G1-G-dElQQW92ekZWa0lGRGREYUpHRWQwTVE").appendRow(vals);
      return ContentService.createTextOutput("added");
    }
    

    If you want to see the result you can have a look to the spreadsheet here

    what you need to know:
    The google apps script is here located in the spreadsheet (but that's not mandatory as in the script I call it with his id).
    In order to run the script you must publish it and allow anyone to run it. When you'll do this action you will get the published URL that you need to put in your HTML code. Here its: https://script.google.com/macros/s/AKfycbxeKTVlTkqSGLIRphBrzACjuWlfmejbPIG7NqBxx-7Us7lnqLw/exec

    Hope this is a satisfying answer for you.

    0 讨论(0)
提交回复
热议问题