Last active
May 13, 2020 14:57
-
-
Save tiagocordeiro/9fa0bb8d75f838b09539064e1ca45fe8 to your computer and use it in GitHub Desktop.
Script para alimentar uma planilha google através dos forms do Gravity Forms
This file contains hidden or 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 sheetID = "GOOGLE_SPREADSHEET_ID"; // Replace this with the Google Spreadsheet ID | |
var sheetName = "Leads"; // Replace this with the sheet name inside the Spreadsheet | |
var status = {}; | |
// Code by @ferraBRas | |
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(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment