Sending Data From Gravity Forms to Google Sheets


You can use the free Gravity Forms Post to 3rd Party API plugin to send your Gravity Forms submission data to just about any third-party API. One of those APIs that’s dead simple to post to is Google Sheets. Here’s how I sent my data from Gravity Forms to Google Sheets in 5 minutes:

  1. Create your Google spreadsheet and add your header row to the spreadsheet Create Google spreadsheet to post from Gravity Forms to Google Sheets

  2. Go to Tools->Script editor to add a new Google script Gravity Forms to Google Sheets script editor menu Gravity Forms to Google Sheets script editor

  3. Remove anything that’s currently in the editor, and copy and paste the following code, being sure to replace the sheet ID and the sheet name
    The spreadsheet ID is the long sequence of characters between the slashes in the URL of the desired spreadsheet:

    docs.google.com/spreadsheets/d/1WcdRWIKAslZELefW27HDFQ0JW585Q9jJCvckS7H24ab/edit?usp=sharing

    function doPost(e) {
     
      if (!e) return;
     
      var sheetID = "GOOGLE_SPREADSHEET_ID";  // Replace this with the Google Spreadsheet ID
      var sheetName = "Sheet1";       // Replace this with the sheet name inside the Spreadsheet
     
      var status = {};
     
      // Code based on Martin Hawksey (@mhawksey)'s snippet
     
      var lock = LockService.getScriptLock();
      lock.waitLock(30000);
     
      try {
     
        var sheet = SpreadsheetApp.openById(sheetID).getSheetByName(sheetName);
        var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues()[0];
     
        // Add the data and time when the Gravity Form was submitted
        var column, row = [],
          input = {
            "timestamp": new Date()
          };
     
        for (var keys in e.parameter) {
          input[normalize_(keys)] = e.parameter[keys];
        }
     
        for (i in headers) {
          column = normalize_(headers[i])
          row.push(input[column] || "");
        }
     
        if (row.length) {
     
          sheet.appendRow(row);
          status = {
            result: "success",
            message: "Row added at position " + sheet.getLastRow()
          };
     
        } else {
          status = {
            result: "error",
            message: "No data was entered"
          };
        }
     
      } catch (e) {
     
        status = {
          result: "error",
          message: e.toString()
        };
     
      } finally {
     
        lock.releaseLock();
     
      }
     
      return ContentService
        .createTextOutput(JSON.stringify(status))
        .setMimeType(ContentService.MimeType.JSON);
     
    }
     
    function normalize_(str) {
      return str.replace(/[^\w]/g, "").toLowerCase();
    }
    

  4. Save your script Gravity Forms to Google Sheets save script Gravity Forms to Google Sheets new script name

  5. Run your script to authorize it Gravity Forms to Google Sheets run google script function Gravity Forms to Google Sheets script authorization Gravity Forms to Google Sheets allow authorization

  6. Deploy the script so that we can get the script URL to post to Gravity Forms to Google Sheets deploy as web app Gravity Forms to Google Sheets deploy info

  7. Copy the current web app URL from the confirmation screen Gravity Forms to Google Sheets project deployed

  8. Create a new Send to Third Party feed for your form, select the POST method, paste the web app URL into the API URL field, set the Authorization to None, and map your form fields to your spreadsheet header columns 11-gravity-forms-post-to-google-spreadsheet

  9. Submit the form and you’ll see the new entry and the information added to your spreadsheet Gravity Forms to Google Sheets entry Gravity Forms to Google Sheets information added

So if there isn’t an add-on available off-the-shelf, and you don’t want to use Zapier or it isn’t quite working right for your API, use the Gravity Forms Post to 3rd Party API plugin 🙂


Questions? Email me