Tuesday, August 17, 2021

How to create a form with Google Apps Script and submit the data to Goog...


 Code.gs
function doGet(request) {
  return HtmlService.createTemplateFromFile('Index').evaluate();
  
}

function include(filename){
  return HtmlService.createHtmlOutputFromFile(filename).getContent();
}

function processForm(formObject){
  var url="";
  var ssSpreadsheetApp.openByUrl(url);
  var ws=ss.getSheetByName("Data");

  ws.appendRow([
    formObject.first_name,
    formObject.last_name,
    formObject.gender,
    formObject.dateOfBirth,
    formObject.email,
    formObject.phone

  ]);
}


Index.html
<!DOCTYPE html>
<html>
  <head>
  
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css"
 integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2" 
crossorigin="anonymous">
  <?!= include('JavaScript'); ?>
  </head>
  
  <body>
    <div class="container">
            <div class="row">
                <div class="col-6">
                    <form id="myForm" onsubmit="handleFormSubmit(this)">
                        <p class="h4 mb-4 text-center">Contact Details</p>

                        <div class="form-row">
                            <div class="form-group col-md-6">
                                <label for="first_name">First Name</label>
                                <input type="text" class="form-control" id="first_name" name="first_name" placeholder="First Name">
                            </div>
                            <div class="form-group col-md-6">
                                <label for="last_name">Last Name</label>
                                <input type="text" class="form-control" id="last_name" name="last_name" placeholder="Last Name">
                            </div>
                        </div>

                        <div class="form-row">
                            <div class="form-group col-md-6">
                                <p>Gender</p>
                                <div class="form-check form-check-inline">
                                    <input class="form-check-input" type="radio" name="gender" id="male" value="male">
                                    <label class="form-check-label" for="male">Male</label>
                                </div>
                                <div class="form-check form-check-inline">
                                    <input class="form-check-input" type="radio" name="gender" id="female" value="female">
                                    <label class="form-check-label" for="female">Female</label>
                                </div>
                            </div>
                            <div class="form-group col-md-6">
                                <label for="dateOfBirth">Date of Birth</label>
                                <input type="date" class="form-control" id="dateOfBirth" name="dateOfBirth">
                            </div>
                        </div>

                        <div class="form-group">
                            <label for="email">Email</label>
                            <input type="email" class="form-control" id="email" name="email" placeholder="Email">
                        </div>

                        <div class="form-group">
                            <label for="phone">Phone Number</label>
                            <input type="tel" class="form-control" id="phone" name="phone" placeholder="Phone Number">
                        </div>

                        <button type="submit" class="btn btn-primary btn-block">Submit</button>
                    </form>
                    <div id="output"></div>
                </div>
            </div>      
        </div>
  </body>
</html>



JavaScript

<script>

  function preventFormSubmit(){
    var forms=document.querySelectorAll('form');
    for (var i=0;i<forms.length;i++){
      forms[i].addEventListener('submit',function(event){
        event.preventDefault();
      });
    }
  }
window.addEventListener('load',preventFormSubmit);

function handleFormSubmit(formObject){
  google.script.run.processForm(formObject);
  document.getElementById("myForm").reset();
}


</script>

46 comments:

  1. ReferenceError: window is not defined (line 11, file "Javascript") is error

    ReplyDelete
    Replies
    1. check your "Javascript" spelling. case-sensitive...needs to match spelling/case of the .gs file you created

      Delete
  2. tks bro! you saved my life!

    ReplyDelete
  3. Can you upload another video like this one (https://youtu.be/v2X-fArILPA) using HTML

    ReplyDelete
  4. Sir how to create submit after redirect to new/next page please tell me Sir..

    ReplyDelete
  5. can you tell how to add search and update option in this code?

    ReplyDelete
  6. You brilliant
    In

    ReplyDelete
  7. index file is not found. Can I know why?

    ReplyDelete
    Replies
    1. check your "index" spelling. case-sensitive...needs to match spelling/case of the .html file you created

      Delete
  8. Exception: Malformed HTML content: function preventFormSubmit(){

    ReplyDelete
  9. Dear Author, I follow your instruction, Everything is Fine But My Upload File Not Working, How can i Upload File Also With THis Form Please? Code On Google Drive, Please See.
    https://drive.google.com/drive/folders/1Xey_j-QzHShpCJNqWxS8qrVfGLmXZjl8?usp=sharing

    ReplyDelete
  10. i need Multiple image file upload fields. help me please.

    ReplyDelete
  11. i need Multiple image file upload fields. help me please.

    ReplyDelete
    Replies
    1. Hey Khalid, did you find how can we add images also in spreadsheet? If yes can you please provide me source code?

      Delete
  12. Hello, can anyone help me how to validate the email and phone number entered by the user. (Form validation)

    ReplyDelete
  13. aita? my wife 99 had seks with maj siste r f 54 i want seks? so i shoo t her with the gaj gan an shie diied omg

    ReplyDelete
  14. How to send a google slide when finish the form

    ReplyDelete
  15. Is it possible to clear(reset) only some field not all form

    ReplyDelete
  16. Hay only my url is not working on mobile view any changes required ?

    ReplyDelete
  17. Prevent users from submitting a form by hitting Enter
    and Allow enter key on textareas only
    $(document).on("keydown", ":input:not(textarea)", function(event) {
    if (event.key == "Enter") {
    event.preventDefault();
    }
    });

    ReplyDelete
  18. Want to downloaad thumbnails from youtube click here grab-thumbnail.blogspot.com

    ReplyDelete
  19. ReferenceError: window is not defined (line 9, file "JavaScript")

    ReplyDelete
    Replies
    1. I get the same error:
      ReferenceError: window is not defined (line 9, file "JavaScript")

      Delete
    2. create html file named JavaScript and enclose js code within script tag

      Delete
  20. I get an error on this line:

    return HtmlService.createTemplateFromFile('Index').evaluate();

    Line two

    ReplyDelete
    Replies
    1. Make sure your html file index has a capital I. I had this issue as well.

      Delete
  21. Can you share a code for a message alert that info has been saved after form submission? I would highly appreciate that.

    ReplyDelete
    Replies
    1. I'm the OP above, nvm the code, I found a way to do it. Now I am figuring out how to make a data from a checkbox, appear in one column of my sheet. Thank you for sharing this anyway.

      Delete
  22. I'm glad I find this but I still need a mentor
    Please let talk I needs your help I'm a full bigginner

    ReplyDelete
  23. Great video. But I stuck at end. please guide me where I go wrong. form website is not accessible to other people when I shared. it is specified...
    Execute as
    User accessing the web app

    Who has access
    Anyone with Google account

    Also excel sheet is shared to public

    Is it possible to add a message box below with 5 line display box. I added it but I can't alter size of box?

    ReplyDelete
  24. Most excellent introduction into Web Apps using Google Sheets. Thank you very much.

    ReplyDelete
  25. Thanks for this tutorial. But do you mind helping out? I've followed the steps here but the form still shows blank screen after submit. I've added some dropdowns on the form that you've created but. I'm having issues at the end after submitting it still shows as blank screen.

    ReplyDelete
  26. I followed everything but the data being inputted still didn't reflected to the spreadsheets.

    ReplyDelete
    Replies
    1. Please help me how to make it work.

      Delete