How to Submit HTML Form Data to Google Sheets | Save Using doPost Method | AppScript | JavaScript.
- First open Vs Code or any your favourite code editor and create a index.html file.
-
Paste the below code in the index.html.
<!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, initial-scale=1.0"> <title>HTML form to Google Sheet</title> </head> <body> <h1 id="msg"></h1> <form> <input type="text" name="name" placeholder='Name'><br><br> <input type="email" name="email" placeholder='Email'><br><br> <input type="tel" name="phone" placeholder='Phone'><br><br> <input type="submit" id="sub"> </form> <script> let form = document.querySelector("form"); form.addEventListener('submit', (e) => { e.preventDefault(); document.querySelector("#sub").value = "Submiting.."; let data = new FormData(form); fetch('Paste_Your_Api_EndPoint_Url', { method: "POST", body: data }) .then(res => res.text()) .then(data => { document.querySelector("#msg").innerHTML = data; document.querySelector("#sub").value = "Submit" }); }) </script> </body> </html>
-
Now goto https://google.com/sheets and create a blank spreadsheet.
-
Click on extensions then click on Apps Script.
-
A new AppScript project will be open in new tab. Paste the below code there and go back to spreadsheet tab then copy url of spreadsheet then give the URL in argument of SpreadsheetApp.openByUrl() method in the code.
const sheets = SpreadsheetApp.openByUrl("Paste your spreadsheet url here"); //if you have changed your sheet name then replace the below Sheet1 with your sheet name const sheet = sheets.getSheetByName("Sheet1"); function doPost(e){ let data = e.parameter; sheet.appendRow([data.name,data.email,data.phone]); return ContentService.createTextOutput("Success"); }
- Click Deploy then click on New deployments.
- Select type "Web App".
- In configuration section input your description and select "Anyone" in "who has access" dropdown menu then click on deploy.
- Click the Authorize Access Button.
- Select your Google account then you will see something like below image, click on "advanced" and click on "Go to Untitled project (unsafe)" then allow your Google account to access the project.
- You will get a API end point URL there Copy the URL then goto to your code editor and give the URL to argument of fetch() method. You are Done!.
- Convert HTML CSS to PDF with JavaScript | HTML to PDF
- Create a Working Contact Form Using HTML - CSS - JavaScript And AppScript | HTML Form to Email
- CRUD App Using HTML - CSS - JavaScript and IndexedDB API
- Reading Data From Google Sheet | Google Sheet to JSON and JSON to HTML | JavaScript and Apps Script
- How to Submit HTML Form Data to Google Sheets | Save Using doPost Method | AppScript | JavaScript
- Fetch and Read Excel Sheets Data in HTML Table with JavaScript
- JavaScript Todo List App with Local Storage | Todo App | Javascript Project
- Fetch and Display Multiple CSV Files in HTML Table with JavaScript | CSV to HTML | Read CSV
- Create a Math Quiz App using HTML CSS and JavaScript | JS Quiz App | JavaScript Project
- Speech to Text with JavaScript | Speech to Text App | JS
- Create a Text to Speech App with JavaScript | JS
- Export HTML Table to CSV with JavaScript | HTML 2 CSV | convert html table to csv | JS
- Validate and show Preview of Single or Multiple Image before upload in server with Javascript | Js
- Convert HTML to Canvas and Canvas to downloadable png / jpeg image | html2canvas | javascript | js
- Match Media Queries With JavaScript matchMedia Method
- Read CSV Files Data in HTML Table using JavaScript Fetch Method | CSV to HTML | JS | CSV File Reader
- How to read csv data in html table using Javascript ( JS )