{"id":13433874,"url":"https://github.com/jamiewilson/form-to-google-sheets","last_synced_at":"2025-05-14T15:11:34.279Z","repository":{"id":37514073,"uuid":"105404142","full_name":"jamiewilson/form-to-google-sheets","owner":"jamiewilson","description":"Store HTML form submissions in Google Sheets.","archived":false,"fork":false,"pushed_at":"2024-05-11T23:30:09.000Z","size":25,"stargazers_count":4637,"open_issues_count":54,"forks_count":822,"subscribers_count":42,"default_branch":"master","last_synced_at":"2025-04-12T02:57:30.789Z","etag":null,"topics":["fetch-api","google-sheets","html-form","javascript","promise-polyfills"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jamiewilson.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-09-30T22:12:09.000Z","updated_at":"2025-04-12T00:33:53.000Z","dependencies_parsed_at":"2024-12-19T14:02:26.401Z","dependency_job_id":null,"html_url":"https://github.com/jamiewilson/form-to-google-sheets","commit_stats":{"total_commits":14,"total_committers":3,"mean_commits":4.666666666666667,"dds":0.1428571428571429,"last_synced_commit":"e528dc8bac8895ca5290cc1bc64ca82708de7822"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jamiewilson%2Fform-to-google-sheets","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jamiewilson%2Fform-to-google-sheets/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jamiewilson%2Fform-to-google-sheets/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jamiewilson%2Fform-to-google-sheets/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jamiewilson","download_url":"https://codeload.github.com/jamiewilson/form-to-google-sheets/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254170059,"owners_count":22026219,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["fetch-api","google-sheets","html-form","javascript","promise-polyfills"],"created_at":"2024-07-31T02:01:38.745Z","updated_at":"2025-05-14T15:11:34.214Z","avatar_url":"https://github.com/jamiewilson.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"# Submit a Form to Google Sheets | [Demo](https://form-to-google-sheets.surge.sh)\n\n#### How to create an HTML form that stores the submitted form data in Google Sheets using plain 'ol JavaScript (ES6), [Google Apps Script](https://developers.google.com/apps-script/), [Fetch](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) and [FormData](https://developer.mozilla.org/en-US/docs/Web/API/FormData).\n\n## 1. Create a new Google Sheet\n\n- First, go to [Google Sheets](https://docs.google.com/spreadsheets) and `Start a new spreadsheet` with the `Blank` template.\n- Rename it `Email Subscribers`. Or whatever, it doesn't matter.\n- Put the following headers into the first row:\n\n|   |     A     |   B   | C | ... |\n|---|:---------:|:-----:|:-:|:---:|\n| 1 | timestamp | email |   |     |\n\n\u003e To learn how to add additional input fields, [checkout section 7 below](#7-adding-additional-form-data).\n\n## 2. Create a Google Apps Script\n\n- Click on `Tools \u003e Script Editor…` which should open a new tab.\n- Rename it `Submit Form to Google Sheets`. _Make sure to wait for it to actually save and update the title before editing the script._\n- Now, delete the `function myFunction() {}` block within the `Code.gs` tab.\n- Paste the following script in it's place and `File \u003e Save`:\n\n```js\nvar sheetName = 'Sheet1'\nvar scriptProp = PropertiesService.getScriptProperties()\n\nfunction intialSetup () {\n  var activeSpreadsheet = SpreadsheetApp.getActiveSpreadsheet()\n  scriptProp.setProperty('key', activeSpreadsheet.getId())\n}\n\nfunction doPost (e) {\n  var lock = LockService.getScriptLock()\n  lock.tryLock(10000)\n\n  try {\n    var doc = SpreadsheetApp.openById(scriptProp.getProperty('key'))\n    var sheet = doc.getSheetByName(sheetName)\n\n    var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues()[0]\n    var nextRow = sheet.getLastRow() + 1\n\n    var newRow = headers.map(function(header) {\n      return header === 'timestamp' ? new Date() : e.parameter[header]\n    })\n\n    sheet.getRange(nextRow, 1, 1, newRow.length).setValues([newRow])\n\n    return ContentService\n      .createTextOutput(JSON.stringify({ 'result': 'success', 'row': nextRow }))\n      .setMimeType(ContentService.MimeType.JSON)\n  }\n\n  catch (e) {\n    return ContentService\n      .createTextOutput(JSON.stringify({ 'result': 'error', 'error': e }))\n      .setMimeType(ContentService.MimeType.JSON)\n  }\n\n  finally {\n    lock.releaseLock()\n  }\n}\n```\n\n\u003e If you want to better understand what this script is doing, checkout the [`form-script-commented.js`](https://github.com/jamiewilson/form-to-google-sheets/blob/master/form-script-commented.js) file in the repo for a detailed explanation. \n\n## 3. Run the setup function\n\n- Next, go to `Run \u003e Run Function \u003e initialSetup` to run this function.\n- In the `Authorization Required` dialog, click on `Review Permissions`.\n- Sign in or pick the Google account associated with this projects.\n- You should see a dialog that says `Hi {Your Name}`, `Submit Form to Google Sheets wants to`...\n- Click `Allow`\n\n## 4. Add a new project trigger \n- Click on `Edit \u003e Current project’s triggers`. \n- In the dialog click `No triggers set up. Click here to add one now.` \n- In the dropdowns select `doPost`\n- Set the events fields to `From spreadsheet` and `On form submit`\n- Then click `Save`\n\n## 5. Publish the project as a web app\n\n- Click on `Publish \u003e Deploy as web app…`.\n- Set `Project Version` to `New` and put `initial version` in the input field below.\n- Leave `Execute the app as:` set to `Me(your@address.com)`.\n- For `Who has access to the app:` select `Anyone, even anonymous`.\n- Click `Deploy`.\n- In the popup, copy the `Current web app URL` from the dialog.\n- And click `OK`.\n\n\u003e **IMPORTANT!** If you have a custom domain with Gmail, you _might_ need to click `OK`, refresh the page, and then go to `Publish \u003e Deploy as web app…` again to get the proper web app URL. It should look something like `https://script.google.com/a/yourdomain.com/macros/s/XXXX…`.\n\n## 6. Input your web app URL\n\nOpen the file named `index.html`. On line 12 replace `\u003cSCRIPT URL\u003e` with your script url:\n\n```js\n\u003cform name=\"submit-to-google-sheet\"\u003e\n  \u003cinput name=\"email\" type=\"email\" placeholder=\"Email\" required\u003e\n  \u003cbutton type=\"submit\"\u003eSend\u003c/button\u003e\n\u003c/form\u003e\n\n\u003cscript\u003e\n  const scriptURL = '\u003cSCRIPT URL\u003e'\n  const form = document.forms['submit-to-google-sheet']\n\n  form.addEventListener('submit', e =\u003e {\n    e.preventDefault()\n    fetch(scriptURL, { method: 'POST', body: new FormData(form)})\n      .then(response =\u003e console.log('Success!', response))\n      .catch(error =\u003e console.error('Error!', error.message))\n  })\n\u003c/script\u003e\n```\n\nAs you can see, this script uses the the [Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API), a fairly new promise-based mechanism for making web requests. It makes a \"POST\" request to your script URL and uses [FormData](https://developer.mozilla.org/en-US/docs/Web/API/FormData) to pass in our data as URL paramters.\n\nBecause Fetch and FormData aren't fully supported, you'll likely want to include their respective polyfills. [See section #8](#8-related-polyfills). \n\n\u003e **Fun fact!** The `\u003chtml\u003e`, `\u003chead\u003e`, and `body` tags are actually among a handful of optional tags, but since the [rules around how the browser parses a page are kinda complicated](https://www.w3.org/TR/2011/WD-html5-20110525/syntax.html#optional-tags), you'd probably not want to omit them on real websites.\n\n## 7. Adding additional form data\nTo capture additional data, you'll just need to create new columns with titles matching exactly the `name` values from your form inputs. For example, if you want to add first and last name inputs, you'd give them `name` values like so:\n\n```html\n\u003cform name=\"submit-to-google-sheet\"\u003e\n  \u003cinput name=\"email\" type=\"email\" placeholder=\"Email\" required\u003e\n  \u003cinput name=\"firstName\" type=\"text\" placeholder=\"First Name\"\u003e\n  \u003cinput name=\"lastName\" type=\"text\" placeholder=\"Last Name\"\u003e\n  \u003cbutton type=\"submit\"\u003eSend\u003c/button\u003e\n\u003c/form\u003e\n```\n\nThen create new headers with the exact, case-sensitive `name` values:\n\n|   |     A     |   B   |     C     |     D    | ... |\n|---|:---------:|:-----:|:---------:|:--------:|:---:|\n| 1 | timestamp | email | firstName | lastName |     |\n\n## 8. Related Polyfills\nSome of this stuff is not yet fully supported by browsers or doesn't work on older ones. Here are some polyfill options to use for better support.\n\n- [Promise Polyfill](https://github.com/taylorhakes/promise-polyfill)\n- [Fetch Polyfill](https://github.com/github/fetch)\n- [FormData Polyfill](https://github.com/jimmywarting/FormData)\n\nSince the FormData polyfill is published as a Node package and needs to be compiled for browsers to work with, a good option for including these is using [Browserify's CDN called wzrd.in](https://wzrd.in/). This service compiles, minifies and serves the latest version of these scripts for us.\n\nYou'll want to make sure these load before the main script handling the form submission. e.g.:\n\n```html\n\u003cscript src=\"https://wzrd.in/standalone/formdata-polyfill\"\u003e\u003c/script\u003e\n\u003cscript src=\"https://wzrd.in/standalone/promise-polyfill@latest\"\u003e\u003c/script\u003e\n\u003cscript src=\"https://wzrd.in/standalone/whatwg-fetch@latest\"\u003e\u003c/script\u003e\n\n\u003cscript\u003e\n  const scriptURL = '\u003cSCRIPT URL\u003e'\n  const form = document.forms['submit-to-google-sheet']\n  ...\n\u003c/script\u003e\n```\n\n# Have feedback/requests/issues?\nPlease [create a new issue](https://github.com/jamiewilson/form-to-google-sheet/issues). PRs are definitely welcome, but please run your ideas by me before putting in a lot of work. Thanks!\n\n#### Related/Inspirational Articles\n- [Google Spreadsheets as a Database – INSERT with Apps Script form POST/GET submit method](https://mashe.hawksey.info/2011/10/google-spreadsheets-as-a-database-insert-with-apps-script-form-postget-submit-method/)\n- [Step by step setup to send form data to Google Sheets](http://railsrescue.com/blog/2015-05-28-step-by-step-setup-to-send-form-data-to-google-sheets/)\n- [Google Sheet Form Post](https://gist.github.com/willpatera/ee41ae374d3c9839c2d6)\n- [How to Submit an HTML Form to Google Sheets…without Google Forms](https://medium.com/@dmccoy/how-to-submit-an-html-form-to-google-sheets-without-google-forms-b833952cc175)\n- [Send Email from a Static HTML Form using Google Apps Mail!](https://github.com/dwyl/html-form-send-email-via-google-script-without-server)\n\n#### Documentation\n- [Google Apps Script](https://developers.google.com/apps-script/)\n- [Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API)\n- [FormData](https://developer.mozilla.org/en-US/docs/Web/API/FormData)\n- [HTML `\u003cform\u003e` element](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/form)\n- [Document.forms](https://developer.mozilla.org/en-US/docs/Web/API/Document/forms)\n- [Sending forms through JavaScript](https://developer.mozilla.org/en-US/docs/Learn/HTML/Forms/Sending_forms_through_JavaScript)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjamiewilson%2Fform-to-google-sheets","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjamiewilson%2Fform-to-google-sheets","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjamiewilson%2Fform-to-google-sheets/lists"}