Updated for Google Script Editor 2022 Version.
The following will teach you how to send data from an HTML form to a Google Sheet using only HTML and JavaScript.
This example shows how to set up a mailing list/newsletter form that sends data from an HTML form to Google Sheets, but you can use it for any sort of data.
Example:
A | B | C | ... | |
---|---|---|---|---|
1 | Date | firstANDlast |
Click on Extensions -> Apps Script
. This will open new Google Script. Rename it to something like "Newsletter".
Replace function myFunction() { ...
with the following code:
// Original code from https://github.com/jamiewilson/form-to-google-sheets
// Updated for 2021 and ES6 standards
const sheetName = 'Sheet1'
const scriptProp = PropertiesService.getScriptProperties()
function initialSetup () {
const activeSpreadsheet = SpreadsheetApp.getActiveSpreadsheet()
scriptProp.setProperty('key', activeSpreadsheet.getId())
}
function doPost (e) {
const lock = LockService.getScriptLock()
lock.tryLock(10000)
try {
const doc = SpreadsheetApp.openById(scriptProp.getProperty('key'))
const sheet = doc.getSheetByName(sheetName)
const headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues()[0]
const nextRow = sheet.getLastRow() + 1
const newRow = headers.map(function(header) {
return header === 'Date' ? 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()
}
}
Save the project ctrl+s
before moving on to the next step.
You should see a modal/popup asking for permissions. Click Review permissions
and continue to the next screen.
Because this script has not been reviewed by Google, it will generate a warning before you can continue. You must click the "Go to Newsletter (Unsafe)" for the script to have the correct permissions to update your form.
Once you click the "Go to Newsletter (Unsafe)" click "Allow".
After giving the script the correct permissions, run the script again, you should see the following output in the script editor console:
Now your script has the correct permissions to continue to the next step.
Select the project "Triggers" from the sidebar and then click the Add Trigger
button.
In the window that appears, select the following options:
doPost
Head
From spreadsheet
On form submit
Then select "Save". It will ask you to review permissions again, click "Allow".
Now your project is ready to publish. Select the Deploy
button and New Deployment
from the drop-down.
Click the "Select type" icon and select Web App
.
In the form that appears, select the following options:
Newsletter blah blah blah
(This can be anything that you want. Just make it descriptive.)Me
Anyone
Then click Deploy
.
Important: Copy and save the web app URL before moving on to the next step.
Configure your HTML form like the following, replacing WEBAPP_URL
with the web app URL you saved from the previous step.
Make sure you update the the code with the proper information.
<form id="FORM_ID" method="POST" action="WEBAPP_URL">
<input name="Email" type="email" placeholder="Email" required>
<input name="Name" type="text" placeholder="Name" required>
<button type="submit">Send</button>
</form>
OPTIONAL: To create a custom thank you page copy the javascript below. Replace FORM_ID
in the HTML and JavaScript with the proper ID and https://www.YOUR_WEBSITE.com/thanks
with the proper url for your thank you or home page.
<script type = "text/javascript" >
window.addEventListener("DOMContentLoaded", function() {
const yourForm = document.getElementById('FORM_ID');
yourForm.addEventListener("submit", function(e) {
e.preventDefault();
const data = new FormData(yourForm);
const action = e.target.action;
fetch(action, {
method: 'POST',
body: data,
}).then(() => {
window.location.replace('https://www.YOUR_WEBSITE.com/thanks')
})
})
});
</script>
Without this javascript code you will be redirected to a page that looks similar to the following.
Note: You will need to create a thank you page on your website. However a thank you page is not required, you could always redirect someone to your website home page, or previous page.
Now when you submit this form from any location, the data will be saved in your specified Google Sheet.
The input names on the google sheet are case sensitive. They MUST match the same casing as the form script.
For example:
If your input name is "firstANDlast",
<input name="firstANDlast">
A1
on your form would look like the following:
A | B | C | ... | |
---|---|---|---|---|
1 | firstANDlast | ... | ... | |
2 | John Smith | ... | ... |
THIS WILL NOT WORK:
A | B | C | ... | |
---|---|---|---|---|
1 | FiRstaNdLast | ... | ... | |
2 | ... | ... |
Additionally, to have the date included on your form you must use "Date", this is also case sensitive. A form with the date included would look similar to the following:
A | B | C | ... | |
---|---|---|---|---|
1 | firstANDlast | Date | ... | |
2 | John Smith | 1/23/2023 | ... |
Thanks to the following articles, projects, and people that inspired this guide;