Skip to content

Instantly share code, notes, and snippets.

@mathetos
Created March 19, 2022 17:21
Show Gist options
  • Save mathetos/a189a1666573e8c2bb77e52fff316c3d to your computer and use it in GitHub Desktop.
Save mathetos/a189a1666573e8c2bb77e52fff316c3d to your computer and use it in GitHub Desktop.
Google Script to Process Webhook JSON and add Row in Google Sheets
function doPost(e) {
var body = e.postData.contents;
var bodyJSON = JSON.parse(body);
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Submissions");
var timeStamp = new Date();
var time = Utilities.formatDate(timeStamp, "GMT-08:00", "MM/dd/yy, h:mm a");
var lastRow = sheet.getLastRow();
//Insert the data into the sheet
sheet.getRange(lastRow + 1, 1).setValue(bodyJSON["firstName"]);
sheet.getRange(lastRow + 1, 2).setValue(bodyJSON["lastName"]);
sheet.getRange(lastRow + 1, 3).setValue(bodyJSON["email"]);
sheet.getRange(lastRow + 1, 4).setValue(bodyJSON["pluginURL"]);
sheet.getRange(lastRow + 1, 5).setValue(bodyJSON["freeFreemium"]);
sheet.getRange(lastRow + 1, 6).setValue(bodyJSON["addonURL"]);
sheet.getRange(lastRow + 1, 7).setValue(bodyJSON["proURL"]);
sheet.getRange(lastRow + 1, 8).setValue(bodyJSON["marketingInfo"]);
sheet.getRange(lastRow + 1, 9).setValue(bodyJSON["background"]);
return HtmlService.createHtmlOutput(200);
}
@mathetos
Copy link
Author

Step 1: Add Apps Script

image

Step 2: Paste script into Code.gs

image

Step 3: Deploy as Web App

  • Deploy as a webapp
  • Set the "Execute as" as yourself
  • Set the "Who has access" to Anyone -- this ensures the source of your webhook can access the script
    image

Step 4: Copy Webapp URL

image

Step 5: Confirm/Debug with Postman

  • Paste the URL into Postman
  • Set it to "POST"
  • Put in a JSON array in the "Body" in "raw" format
  • Set the response to "Preview" to see results and/or error messages for troubleshooting
    image

Step 6: Confirm the rows are created in your spreadsheet

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment