Skip to content

Instantly share code, notes, and snippets.

@baymac
Created January 1, 2019 21:04
Show Gist options
  • Save baymac/45bcf98e70b04938154f7ef485aecd25 to your computer and use it in GitHub Desktop.
Save baymac/45bcf98e70b04938154f7ef485aecd25 to your computer and use it in GitHub Desktop.
App Script to send the response from Google Form to Firebase
function onFormSubmit(e) {
var form = FormApp.openById('1Z65umQSZ2drz3MvTWCWlwP4-ySeoB5D7RvX1LtusnEU');
var formResponses = form.getResponses();
var latestResponse = formResponses.length - 1;
var formResponse = formResponses[latestResponse];
var email = formResponse.getRespondentEmail();
var items = formResponse.getItemResponses();
var name = getMethod(items[3]);
var phone = getMethod(items[4]);
var flat = getMethod(items[5]);
var area = getMethod(items[6]);
var item = getMethod(items[0]).toString();
var quantity = getMethod(items[1]);
var suggestion = getMethod(items[7]);
var dataToExport = {};
dataToExport =
{
"name": name,
"email":email,
"phone":phone,
"address":
{
"flat":flat,
"area":area
},
"item": item,
"quantity": quantity,
"suggestion": suggestion
};
Logger.log(getMethod(items[0]));
var dateStr = getMethod(items[2]);
var date = Utilities.formatDate(new Date(dateStr), "GMT+1", "yyyyMMdd");
var base = FirebaseApp.getDatabaseByUrl('https://sendingresponse-64495.firebaseio.com/');
base.setData("orders/"+date+"/"+phone, dataToExport);
}
function getMethod(item) {
var value = item || "_unknown_"; // if null then "_unknown_" is assigned
function check(x) {
if(x == "")
return "_unknown_";
else
return x;
}
if(value != "_unknown_") {
return check(value.getResponse());
} else {
return value;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment