Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Script for Google Sheets to push Gravity form entries to new row
function doPost(e) {
if (!e) return;
var status = {};
var lock = LockService.getScriptLock();
lock.waitLock(30000);
try {
//var sheet = SpreadsheetApp.openById(sheetID).getSheetByName(sheetName);
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1');
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) {
Logger.log(keys)
input[normalize_(keys)] = e.parameter[keys];
}
// sheet.getRange('A558').setValue(input)
for (i in headers) {
column = normalize_(headers[i])
row.push(input[column] || "");
}
// sheet.getRange('A559').setValue(row)
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();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment