I am using a Google Form for a classroom 'store' for students to purchase items from. A student cashier will fill out the form and then the sheet will send an email receipt to the student that purchased the items. The email sends fine with my current code and trigger, however, it sends an email to all submissions on each new submission. I only want it to send to the last submission. I know there's probably an easy fix to this and I'm just overlooking something. Here is my current code:
function sendAutomatedEmails() {
var spreadSheet = SpreadsheetApp.getActiveSheet();
var dataRange = spreadSheet.getDataRange();
var data = dataRange.getValues();
for (var i = 1; i< data.length; i++) {
(function(val) {
var row = data[i];
var emailAddress = row[3];
var message = 'At '+ row[0] + 'You purchased the following:' + '\n\n' + row[4] + '\n' + row[5] + '\n' + row[6] + '\n' + row[7] + '\n' + row[8] + '\n' + row[9] + '\n' + 'Your total cost is $' + row[10];
var subject = 'Personal Purchase Reciept';
MailApp.sendEmail(emailAddress, subject, message);
})(i);
}
}
I can't delete submissions every time as another classroom job is to check that purchases were paid for in our online bank system each week which is when the submissions will be deleted. Any help is greatly appreciated.