Created
August 25, 2016 19:03
-
-
Save thoughtpalette/d2cc6b88d32cb4613b037bde32fa8f99 to your computer and use it in GitHub Desktop.
Gist for serverless forms on G Sheets
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
// Usage | |
// 1. Enter sheet name where data is to be written below | |
var SHEET_NAME = "testform"; | |
// 2. Run > setup | |
// | |
// 3. Publish > Deploy as web app | |
// - enter Project Version name and click 'Save New Version' | |
// - set security level and enable service (most likely execute as 'me' and access 'anyone, even anonymously) | |
// | |
// 4. Copy the 'Current web app URL' and post this in your form/script action | |
// | |
// 5. Insert column names on your destination sheet matching the parameter names of the data you are passing in (exactly matching case) | |
var TO_ADDRESS = "EMAIL HERE"; | |
// If you don't want to expose either GET or POST methods you can comment out the appropriate function | |
function doGet(e){ | |
return handleResponse(e); | |
} | |
function doPost(e){ | |
return handleResponse(e); | |
} | |
function handleResponse(e) { | |
try { | |
var SUBJECT = "New Brand Camp Contact"; | |
if ( e.parameters.contactShort[0] === "true" ) { | |
var checkBoxValue = e.parameters.readyNdaVal[0] === "true" ? "yes please" : "not needed"; | |
MailApp.sendEmail({ | |
to: TO_ADDRESS, | |
subject: SUBJECT, | |
htmlBody: "<h2>Peeps want you to contact them</h2><br>" + | |
"<p>Name: <strong>" + e.parameters.readyName[0] + "</strong></p><br>" + | |
"<p>Email: <strong>" + e.parameters.readyEmail[0] + "</strong></p><br>" + | |
"<p>Phone: <strong>" + e.parameters.readyPhone[0] + "</strong></p><br>" + | |
"<p>NDA?: <strong>" + checkBoxValue + "</strong></p><br>" | |
}); | |
return ContentService // return json success results | |
.createTextOutput( | |
JSON.stringify({"result":"success", | |
"data": e.parameters })) | |
.setMimeType(ContentService.MimeType.JSON); | |
Logger.log("short form hit"); | |
} else { | |
Logger.log("long form hit"); | |
var checkBoxValue = e.parameters.readyNdaVal[0] === "true" ? "yes please" : "not needed"; | |
MailApp.sendEmail({ | |
to: TO_ADDRESS, | |
subject: SUBJECT, | |
htmlBody: "<h2>You have a new email from the Brand Camp Website</h2><br>" + | |
"<p>Name: <strong>" + e.parameters.name[0] + "</strong></p><br>" + | |
"<p>Email: <strong>" + e.parameters.email[0] + "</strong></p><br>" + | |
"<p>Phone: <strong>" + e.parameters.phone[0] + "</strong></p><br>" + | |
"<p>My company is currently: <strong>" + e.parameters.companyPhase[0] + "</strong></p><br>" + | |
"<p>I want to start: <strong>" + e.parameters.startTime[0] + "</strong></p><br>" + | |
"<p>I'd like to: <strong>" + e.parameters.clientParticipation[0] + "</strong></p><br>" + | |
"<p>I expect to invest: <strong>" + e.parameters.investRange[0] + "</strong></p><br>" + | |
"<p>NDA?: <strong>" + checkBoxValue + "</strong></p><br>" | |
}); | |
return ContentService // return json success results | |
.createTextOutput( | |
JSON.stringify({"result":"success", | |
"data": e.parameters })) | |
.setMimeType(ContentService.MimeType.JSON); | |
} | |
} catch(error) { // if error return this | |
Logger.log(error); | |
return ContentService | |
.createTextOutput(JSON.stringify({"result":"error", "error": e})) | |
.setMimeType(ContentService.MimeType.JSON); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment