Created
July 23, 2018 20:36
-
-
Save geshem/fc7611c635cdb17101fe8b14f8bc2660 to your computer and use it in GitHub Desktop.
Script for Google Sheets to push Gravity form entries to new row
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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