Now it is time to develop!
In the Untitled project you add:
//this is a function that fires when the webapp receives a GET request -
function doGet(e) {
return HtmlService.createHtmlOutput("get request received");
}
//this is a function that fires when the webapp receives a POST request
function doPost(e) {
try {
var params = JSON.stringify(e.postData.contents);
params = JSON.parse(params);
var myData = JSON.parse(e.postData.contents);
var customer_firstname = myData.customer_firstname;
var customer_lastname = myData.customer_lastname;
var customer_email = myData.customer_email;
var customer_phone = myData.customer_phone;
var comment = myData.comment;
var price_total = myData.price_total;
// try statement needed because no invoice data triggers an error
try {
var invoice_address = myData.invoicing_address;
var invoice_company_name = invoice_address.company_name;
var invoice_vat = invoice_address.vat;
var invoice_street = invoice_address.street;
var invoice_number = invoice_address.number;
var invoice_zip = invoice_address.zip;
var invoice_city = invoice_address.city;
}
catch(err) {
var invoice_company_name = "no invoice";
var invoice_vat = "";
var invoice_street = "";
var invoice_number = "";
var invoice_zip = "";
var invoice_city = err;
}
var sheet = SpreadsheetApp.getActiveSheet();
var lastRow = Math.max(sheet.getLastRow(),1);
sheet.insertRowAfter(lastRow);
var timestamp = new Date();
sheet.getRange(lastRow + 1, 1).setValue(timestamp);
// Separate test data from production data
sheet.getRange(lastRow + 1, 2).setValue("Real Order");
// Json data output for debug reasons, you can delete this
sheet.getRange(lastRow + 1, 3).setValue(myData);
sheet.getRange(lastRow + 1, 4).setValue(customer_firstname);
sheet.getRange(lastRow + 1, 5).setValue(customer_lastname);
sheet.getRange(lastRow + 1, 6).setValue(customer_phone);
sheet.getRange(lastRow + 1, 7).setValue(invoice_company_name);
sheet.getRange(lastRow + 1, 8).setValue(invoice_vat);
sheet.getRange(lastRow + 1, 9).setValue(invoice_street);
sheet.getRange(lastRow + 1, 10).setValue(invoice_number);
sheet.getRange(lastRow + 1, 11).setValue(invoice_zip);
sheet.getRange(lastRow + 1, 12).setValue(invoice_city);
sheet.getRange(lastRow + 1, 13).setValue(comment);
sheet.getRange(lastRow + 1, 14).setValue(price_total);
// Json data output for debug reasons, you can delete this
sheet.getRange(lastRow + 1, 15).setValue(invoice_address);
}
catch(err) {
var sheet = SpreadsheetApp.getActiveSheet();
var lastRow = Math.max(sheet.getLastRow(),1);
sheet.insertRowAfter(lastRow);
var timestamp = new Date();
sheet.getRange(lastRow + 1, 1).setValue(timestamp);
sheet.getRange(lastRow + 1, 2).setValue("Error" + err);
}
var emailAddress = "yourname@gmail.com";
var subject = "Sayl Retail Order Result";
var message = params; //"Sending emails from a Spreadsheet";
MailApp.sendEmail(emailAddress, subject, message, {
name: 'Yourname - Sayl Retail'
});
SpreadsheetApp.flush();
return HtmlService.createHtmlOutput("post request received");
}}