# How to send an HTML form to Google Sheets

A Google Sheet is the place a lot of people want form submissions to end up. Everyone on the team can open it, filter it and add a "called back?" column, and nobody has to log in to another tool.

Sheets can't take a form post by itself, though. Something has to receive the submission and write the row. This guide does it two ways: a free Google Apps Script web app you set up yourself, and a form backend that creates the sheet and fills it for you.

## 1\. Option A: Google Apps Script

### Set up the sheet

Create a new spreadsheet and type these headers in row 1, one per column:

| timestamp | name | email | message |
| --- | --- | --- | --- |

Each header has to match a field `name` in your form exactly, including case. That's how the script knows which value goes in which column. The script writes to the tab named in `SHEET_NAME` (`Sheet1` by default).

### Add the script

In the sheet, open **Extensions → Apps Script**, delete what's there and paste this. Replace `YOUR_SPREADSHEET_ID` with the ID from your sheet's URL (`docs.google.com/spreadsheets/d/YOUR_SPREADSHEET_ID/edit`), and update `SHEET_NAME` if you renamed the tab:

```js
const SPREADSHEET_ID = "YOUR_SPREADSHEET_ID";
const SHEET_NAME = "Sheet1";

function doPost(e) {
  const data = JSON.parse(e.postData.contents || "{}");

  // Honeypot filled in: answer OK so the bot learns nothing.
  if (data._gotcha) return json({ ok: true });

  const sheet = SpreadsheetApp.openById(SPREADSHEET_ID).getSheetByName(SHEET_NAME);
  if (!sheet) return json({ ok: false, error: "Sheet not found" });
  const headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues()[0];

  const row = headers.map((header) => {
    if (header === "timestamp") return new Date();
    const value = String(data[header] || "").slice(0, 5000);
    // A value starting with = + - or @ would run as a formula. The ' keeps it as text.
    return /^[=+\-@]/.test(value) ? "'" + value : value;
  });

  sheet.appendRow(row);
  return json({ ok: true });
}

function json(data) {
  return ContentService.createTextOutput(JSON.stringify(data))
    .setMimeType(ContentService.MimeType.JSON);
}
```

### Deploy it as a web app

1. Click **Deploy → New deployment**, then the gear icon and **Web app**.
2. Set **Execute as** to **Me** and **Who has access** to **Anyone**.
3. Click **Deploy** and allow access. Google shows "Google hasn't verified this app" because the script is yours and unreviewed. Click **Advanced**, then **Go to (your project)**.
4. Copy the **Web app URL**. It looks like `https://script.google.com/macros/s/AKfy…/exec`.

### The form

The web app answers on a Google domain, so a plain `<form action>` would leave your visitor on a page of JSON. Send it with `fetch()` instead and show the thank-you message on your own page:

```html
<form id="contact">
  <input name="name" placeholder="Name" required>
  <input name="email" type="email" placeholder="Email" required>
  <textarea name="message" placeholder="Message" required></textarea>
  <!-- Honeypot: hidden from people, bots fill it in -->
  <div aria-hidden="true" style="position:absolute; left:-9999px">
    <input type="text" name="_gotcha" tabindex="-1" autocomplete="off">
  </div>
  <button type="submit">Send</button>
  <p id="status" role="status"></p>
</form>

<script>
  const form = document.getElementById("contact");
  const status = document.getElementById("status");
  form.addEventListener("submit", async (event) => {
    event.preventDefault();
    status.textContent = "";
    try {
      // No Content-Type header, so the browser sends it as text/plain and skips the CORS preflight.
      const res = await fetch("https://script.google.com/macros/s/YOUR_DEPLOYMENT_ID/exec", {
        method: "POST",
        body: JSON.stringify(Object.fromEntries(new FormData(form))),
      });
      if (res.ok && (await res.json()).ok === true) {
        form.replaceWith("Thanks, we got your message.");
        return;
      }
    } catch {}
    status.textContent = "Something went wrong. Please try again.";
  });
</script>
```

Submit it once and a row should show up in the sheet.

### The traps

Most "it stopped working" questions about this setup come down to one of these:

- **Edits don't go live on their own.** The `/exec` URL keeps running the version you deployed. After every change, open **Deploy → Manage deployments**, edit the deployment and pick **New version**, or you'll be debugging code that isn't running.
- **A missing tab.** If the form shows an error, check that `SHEET_NAME` matches the tab. The script can return `{ "ok": false }` with HTTP `200`, so the form checks the response body too.
- **Don't set a JSON content type.** A `Content-Type: application/json` header makes the browser send a CORS preflight first, and Apps Script can't answer it, so the request fails. Leave the header off like the code above. The script parses the body as JSON either way.
- **Formulas run.** `appendRow` treats a value starting with `=` as a formula, so a visitor can put `=IMPORTXML(...)` in your sheet. The `'` prefix in the script stops that. Leave it in.
- **New fields need a new column.** Add a `phone` field to the form and nothing saves it until you add a `phone` header by hand.

### What this version doesn't do

For a personal site it's free and it works. What it leaves out:

- **Spam.** The URL is public and in your page source. Anyone can post to it as often as they like, and the honeypot only stops simple bots. LLM-written sales pitches get through and become rows.
- **Notifications.** Nobody hears about a new row unless they open the sheet. Emailing yourself means more script (`MailApp`) and more quota.
- **A second copy.** If the script errors or the sheet is renamed, the submission is lost and nothing tells you.
- **Sorting.** Every submission lands in the same sheet. Sending only sales leads there means writing the rules, and deciding which messages are leads.

## 2\. Option B: a form backend that creates the sheet

With [Formkove](https://formkove.com/?ref=hashnode), the form posts to a Formkove URL. Each submission is saved, checked for spam, emailed to you, and added to the sheet as a row.

**Point the form at Formkove.** Create a form at [app.formkove.com](https://app.formkove.com/signup?ref=hashnode) and use its endpoint as the action. No script is needed, and the `_gotcha` honeypot is on by default.

```html
<form action="https://app.formkove.com/api/forms/YOUR_FORM_ID/submissions" method="POST">
```

**Connect Google Sheets.** In the form's **Integrations**, click **Connect Google Sheets**, then **Connect Google**, and pick your account. Formkove creates a new spreadsheet in your Drive. Click **Test** to add a sample row and **Open sheet** to see it.

Formkove only asks for access to the files it creates, so it can't open your other spreadsheets. You can remove that access in your Google account at any time, and deleting the connection keeps the sheet and its rows.

**How the rows look.** Each row starts with **Submitted at** and **Submission ID**. Every form field gets its own column the first time it shows up, so a new `phone` field just adds a column. Columns are matched by name, so you can reorder them or add your own (status, notes) and new rows still land in the right place. Values are written as plain text and numbers, so a submitted `=IMPORTXML(...)` stays text.

**Send only some submissions.** Describe your tags in plain words on the form's **AI & Spam** page (for example, `sales-lead`: wants a quote, a price or to hire us), then add a routing rule to the Sheets connection:

- **Tag** has any of `sales-lead`

Now the sheet only gets leads, with **Sentiment** and **Tags** columns next to the fields. Rules can also test any form field, like `budget` is greater than `5000`. Email isn't routed: you still get every real submission by email.

What comes with it:

- **Spam held back** before anything reaches the sheet, kept in your dashboard for 30 days in case one was a real message.
- **Every submission saved**, even when Google is down or the sheet was deleted. Failed deliveries are retried and listed in **Delivery history**.
- **Rate limits** on every form: 10 a minute and 30 an hour per IP address.

Google Sheets, tags and routing are on the Pro and Agency plans, and Free accounts get them on their [first 50 submissions](https://formkove.com/docs/limits/?ref=hashnode#your-first-50-submissions-on-free) ([pricing](https://formkove.com/pricing/?ref=hashnode)). Setup details are in the [Google Sheets docs](https://formkove.com/docs/google-sheets-integration/?ref=hashnode).

---

*Originally published on* [*formkove.com*](https://formkove.com/blog/html-form-to-google-sheets/?ref=hashnode)*.*
