A simple example of sending data from an ordinary web form straight to a Google Spreadsheet without a server.
This repo is just a quick example I made. If you want a full step-by-step tutorial, see:
https://github.com/dwyl/html-form-send-email-via-google-script-without-server
A friend called and asked for a favor:
"Can you set up a web form that posts data to a Google Spreadsheet
without a (server) backend...? (We need it done by tomorrow)"
Never one to turn down a chance to help a friend and learn something new, I agreed to the task!
The plan is to simulate the POST
request an actual Google Form sends when it submits a survey response to its google ("results") spreadsheet. Sounds simple. it is. kinda ...
Open the Sample Google Spreasheet (Ensure you are signed into your Google Account)
https://docs.google.com/spreadsheets/d/10tt64TiALYhPMqR2fh9JzkuhxW7oC0rXXPb_pmJ7HAY
Once you have made the copy, open the Script Editor so you can edit the App Script
Without modifying the sample script, Publish it!
Deploy the script as a "Web App"
Authorize the script to access your Google Drive
You will need the url for step {X} below...
I my case the url is:
https://script.google.com/macros/s/AKfycby2i5t13ccSQ9e-atuknlnPDbqKplF2QUVFiWIX_wnEPD34GM0/exec
But your will be different.
The Setup Script gets the Name of your associated Google Spreadsheet so it knows where to put the data...
Once you have run the Setup Script, test that it worked.
Clicking that link will open a new page with a JSON
result:
Back in your spreadsheet, confirm that the row was inserted:
Now the fun part...
Copy the code from this index.html and paste it into your own index.html
Find the line in that defines the "ajax" request:
request = $.ajax({
url: "https://script.google.com/macros/s/AKfycby2i5t13ccSQ9e-atuknlnPDbqKplF2QUVFiWIX_wnEPD34GM0/exec",
type: "post",
data: serializedData
});
And update it to be the url for your "App" which you copied in step 12.6 above!
Save and open the form in your web browser.
Enter some data and submit the form!
Back in the spreadsheet, confirm that the row was inserted:
<form id="gform">
<p><label for="checkin">Checkin Date</label>
<input id="checkin" name="checkin" type="date" value=""/></p>
<p><label for="checkout">Checkout Date</label>
<input id="checkout" name="checkout" type="date" value=""/></p>
<p><label for="email">Email:</label>
<input id="email" name="email" type="text" value=""/></p>
<p><label for="guests">Number of Guests:</label>
<input id="guests" name="guests" type="text" value=""/></p>
<p id="result"></p>
<input type="submit" value="Send"/>
</form>
Note: that the field names in the Google Spreadsheet (column headers) need to match the field names in the HTML exactly so no spaces in names, please... best to use single words and all lowercase or use underscores to separate words.
Save index.html
and open it in your browser:
Submit the form with some sample data!
Back in your Google Sheet, confirm that the row was inserted:
Grab the HTML from the existing page and customize the form to your heart's content. You won't need to update the JavaScript because its generic, however you will need to match the field names in your HTML form to the column headings in the Spreadsheet for the data to get inserted correctly.
An additional requirement was to send an email (IKR!)
when ever the HTML Form is submitted.
Thankfully, Google has a good Method for doing this:
MaillApp
In your Google Spreadsheet Script call:
MailApp.sendEmail("[email protected]", // to
"[email protected]", // from
"Your Subject Goes Here", // email subject
"What ever you want to say"); // email body
For this new functionality to work in your script you will need to "Save a New Version" and (Re-)Deploy your App! (simply clicking the 💾 "Save" is Not Enough!)
It's not immediately obvious but you have to click on "Manage Versions..."
Then create your new version:
Select the latest project version to deploy:
Click "OK". No need to update the script url in your HTML form (it does not change - there are pros & cons to this...)
Hypothesis: for high value items some people will prefer to speak to a person on the phone instead of emails. Therefore having a form input for Phone Number will "convert"" better than email.
I've added two new files: _lon-phone.html
and _par-phone.html
to test this Hypothesis.
If you want more, please ask!
- AJAX post to google spreadsheet: http://stackoverflow.com/questions/10000020/ajax-post-to-google-spreadsheet which points to:
- MailApp Service: https://developers.google.com/apps-script/reference/mail/mail-app
- I Posted this OverFlow Question because I could not get
MailApp.sendEmail
to work: http://stackoverflow.com/questions/33180172/why-does-mailapp-sendemail-from-google-spreadsheet-script-not-work