How to

Submit HTML form’s data to Google Sheet | Sending HTML Form’s data to google sheet

Ravindra Kumar

Sometimes, you need to record Html form’s data into Google sheet so that you can use this data later. I am going to explain how can we do this by using HTML form and google sheet.
HTML Form Code


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, minimum-scale=1.0">
    <title>Contact Us</title>
    <link rel="shortcut icon" href="images/index.ico" type="image/x-icon">

    <!-- CSS: Fontawesome -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
    <!-- CSS: Bootstrap v-5.3 -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.4.1/dist/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
   
    
<body> 
             <!-- space -->
                <div class="py-lg-5 py-md-4 py-sm-3 py-2"></div>
                <div class="row justify-content-center pt-5">
                    <div class="col-lg-7 col-md-10">
                        <h3 class="fw-normal text-center mb-5 border-bottom pb-2">Contact with Our Help Team</h3>

                        <form class="form_overlay" method="POST" accept-charset="UTF-8" autocomplete="off" name="contact"> 
                            <div class="row g-4">
                                <div class="col-lg-6">
                                    <div class="input_overlay">
                                        <label for="fname" class="lable">Enter your first name</label>
                                        <input type="text" name="first_name" class="form-control" required autocomplete="off">
                                    </div>
                                </div>
                                <div class="col-lg-6">
                                    <div class="input_overlay">
                                        <label for="lname" class="lable">Enter your last name</label>
                                        <input type="text" name="last_name" class="form-control" required autocomplete="off">
                                    </div>
                                </div>
                                <div class="col-lg-6">
                                    <div class="input_overlay">
                                        <label for="email" class="lable">Enter email address</label>
                                        <input type="text" name="email" class="form-control" required autocomplete="off">
                                    </div>
                                </div>
                                <div class="col-lg-6">
                                    <div class="input_overlay">
                                        <label for="phone" class="lable">Phone number</label>
                                        <input type="text" name="phone" class="form-control" required autocomplete="off">
                                    </div>
                                </div>
                                <div class="col-12">
                                    <div class="input_overlay">
                                        <label for="subject" class="lable">Subject name</label>
                                        <input type="text" name="subject" class="form-control" required autocomplete="off">
                                    </div>
                                </div>
                                <div class="col-12">
                                    <div class="input_overlay textarea">
                                        <label for="desc" class="lable">Description</label>
                                        <textarea name="description" cols="30" rows="4" class="form-control" required autocomplete="off"></textarea>
                                    </div>
                                </div>
                                
                                <div class="col-12 text-end">
                                <button type="submit" class="btn btn-primary mt-3">Submit</button>
                                </div>
                                <p id="demo"></p>
                            </div>
                            
                        </form>
                    </div>
                </div>


    </main>


    

   <script>
            const scriptURL = 'https://script.google.com/macros/s/AKfycbyMVQlyqjWagy6MS0C4ICRW6Be5T647IAR1iClnTLaih6qZce3Hclv4_NvN8uhW2EJZzw/exec'
            const form = document.forms['contact']
           
            form.addEventListener('submit', e => {
              e.preventDefault()
              fetch(scriptURL, { method: 'POST', body: new FormData(form)})
                .then(response => document.getElementById("demo").innerHTML = "<div class='alert alert-primary' role='alert'><b>Thank You for providing the details, We shall get back to you shortly !</b></div>",contact.reset())
                .catch(error => console.error('Error!', error.message))
            })
    </script>

             
 </body>
 </html>

Script for HTML Form

<script>
            const scriptURL = 'https://script.google.com/macros/s/AKfycbyMVQlyqjWagy6MS0C4ICRW6Be5T647IAR1iClnTLaih6qZce3Hclv4_NvN8uhW2EJZzw/exec'
            const form = document.forms['contact']
            
            form.addEventListener('submit', e => {
              e.preventDefault()
              fetch(scriptURL, { method: 'POST', body: new FormData(form)})
                .then(response => document.getElementById("demo").innerHTML = "<div class='alert alert-primary' role='alert'><b>Thank You for providing the details, We shall get back to you shortly !</b></div>",contact.reset())
                .catch(error => console.error('Error!', error.message))
            })
    </script>

Script for Google Sheet

var sheetName = 'contact'
        var scriptProp = PropertiesService.getScriptProperties()
 
        function intialSetup () {
          var activeSpreadsheet = SpreadsheetApp.getActiveSpreadsheet()
          scriptProp.setProperty('key', activeSpreadsheet.getId())
        }
 
        function doPost (e) {
          var lock = LockService.getScriptLock()
          lock.tryLock(10000)
 
          try {
            var doc = SpreadsheetApp.openById(scriptProp.getProperty('key'))
            var sheet = doc.getSheetByName(sheetName)
 
            var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues()[0]
            var nextRow = sheet.getLastRow() + 1
 
            var newRow = headers.map(function(header) {
              return header === 'timestamp' ? new Date() : e.parameter[header]
            })
 
            sheet.getRange(nextRow, 1, 1, newRow.length).setValues([newRow])
 
            return ContentService
              .createTextOutput(JSON.stringify({ 'result': 'success', 'row': nextRow }))
              .setMimeType(ContentService.MimeType.JSON)
          }
 
          catch (e) {
            return ContentService
              .createTextOutput(JSON.stringify({ 'result': 'error', 'error': e }))
              .setMimeType(ContentService.MimeType.JSON)
          }
 
          finally {
            lock.releaseLock()
          }
        }

Ravindra Kumar

Ravindra is a passionate full stack developer and dedicated blogger with a flair for crafting user-friendly web applications and insightful articles. With expertise spanning front-end and back-end technologies, Ravindra brings ideas to life through innovative coding solutions.

Suggested Reading

Call to undefined method Intervention\Image\ImageManager::make() in Laravel

The error “Call to undefined method Intervention\Image\ImageManager::make()” typically happens when you’re trying to use the make() method from the Intervention Image package but haven’t correctly set up the manager or are using the wrong instance. Solution Make sure you’re using Intervention\Image\Facades\Image facade, not ImageManager directly. 1. Correct Usage in Laravel In your controller or wherever […]

How to redirect http to https in IIS

o redirect HTTP to HTTPS in IIS (Internet Information Services), you have a few options depending on your setup. Here’s how to do it step-by-step: Method 1: Using IIS HTTP Redirect Module This works best for simple websites and requires the HTTP Redirect feature to be installed. Steps: Open IIS Manager. Select your website from […]

How to redirect non-www to www in htaccess file

To redirect non-www to www using your .htaccess file, add the following code: RewriteEngine On RewriteCond %{HTTP_HOST} !^www\. RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301] Combined with HTTP to HTTPS redirect If you also want to force HTTPS and redirect non-www to www, here’s a combined version: </p> RewriteEngine On # Force HTTPS RewriteCond %{HTTPS} off RewriteRule ^(.*)$ […]

How to redirect http to https in htacccess file

To redirect all HTTP traffic to HTTPS using .htaccess, you can use the following code snippet: RewriteEngine On RewriteCond %{HTTPS} off RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] Steps: Locate the .htaccess file in the root directory of your website (often public_html/). If there is no .htaccess file, create one. Paste the above code at the top of […]

Whatsapp Contact Form Source Code Html CSS Javascript

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Contact Form</title> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet"> <style> body { background-color: #f8f9fa; } .contact-form { max-width: 600px; margin: 50px auto; padding: 20px; background: #fff; border-radius: 10px; box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1); } </style> </head> <body> <div class="container"> <div class="contact-form"> <h2 class="text-center mb-4">Contact Us</h2> […]

Making WhatsApp Order Form using HTML, CSS, Javascript

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Order Form</title> <!-- Bootstrap CSS --> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous"> <script> function sendOrderToWhatsApp() { // Retrieve form values var name = document.getElementById('name').value; var email = document.getElementById('email').value; var contact = document.getElementById('contact').value; var address = document.getElementById('address').value; var pincode = document.getElementById('pincode').value; var landmark = document.getElementById('landmark').value; // Retrieve product selections var […]