The script above will configure your Google Sheet to handle a GET request (function doGet()), tells the spreadsheet where to place the received data (lines 21–38), and sets the allowed MIME type (setMimeType(ContentService.MimeType.JSON)). Give it a read—Will Patera did a great job explaining the script with his comments so I left them in there. (The original gist can be found here.)
Save the script and give it an appropriate name. Then go to the “Run” menu and select “setup.”
You might be asked to give Google Scripts to use your Google account.
Once you’ve given your authorization, go to the “Publish” menu and select “Deploy as web app.”
You will then be presented with a few options with which to customize your script.
The last two of these three options are extremely important to set correctly or you won’t be able to access your script with an AJAX request. You must execute the app as yourself and you must give “Anyone, even anonymous” access to the app. Without these settings your script will reject any request from a different server, like your form’s AJAX request, because it won’t be configured to allow for cross-origin resource sharing (CORS).
Once you’ve configured these options, go ahead and click “Deploy.”
You will the be presented with the URL for your web app. This is where we’ll be sending our AJAX request so copy that URL and save it for later.
Finally, we’ll connect our HTML form with our Google Script with a little JavaScript/jQuery. Nothing too crazy here, either. We’re preventing the form from submitting normally and instead constructing an AJAX request to the URL for our web app/script that we obtained earlier.
The only interesting part is the data that we’re sending. Our script is expecting a JSON data-type so we’ll have to convert our form data into a JSON object. I chose to do this with the jQuery Serialize Object script, which provides the serializeObject() method, and will convert our form into a JavaScript object. You can solve this problem however you’d like—as long as your data is structured like JSON!
Now, when someone submits your HTML form, their responses should be recorded in your Google Sheet. Sweet!