I am a beginner in coding and I tried many things (solutions), but no script is running. I am using an existing sendEmail script (see down below) to send emails to new spreadsheet entries.
https://docs.google.com/spreadsheet
I am not using Google Forms. I am copying the needed Information out of another spreadsheet table. With an onEdit trigger the script will send an email to the new entries.
The problem I am having is that the script sends an email to every row of the spreadsheet even though that information was already sent.
I tried some workarounds, although which are named in the forum, but nothing helped in my case. I don`t want to send an email only to the last single entry. I want to send Emails to a different amount of new entries and this should be triggert by an onEdit Event, if it is possible. I hope, someone can help me soon…
var EMAIL_SENT = "EMAIL_SENT";
function sendEmails(onlyLast) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var dataSheet = ss.getSheets()[0];
var startRow = 2;
var lastRow = datasheet.getlastRow()-1;
if (onlyLast)
startRow = endRow;
var dataRange = dataSheet.getRange(startRow, 1, lastRow, 4);
var templateSheet = ss.getSheets()[1];
var emailTemplate = templateSheet.getRange("A1").getValue();
// Create one JavaScript object per row of data.
var objects = getRowsData(dataSheet, dataRange);
// For every row object, create a personalized email from a template and send
// it to the appropriate person.
for (var i = 0; i < objects.length; ++i) {
// Get a row object
var rowData = objects[i];
var file = DriveApp.getFileById('');
// Generate a personalized email.
// Given a template string, replace markers (for instance ${"First Name"}) with
// the corresponding value in a row object (for instance rowData.firstName).
var emailText = fillInTemplateFromObject(emailTemplate, rowData);
var emailSubject = "Tutorial: Simple Mail Merge";
var emailSent;
if (emailSent != EMAIL_SENT) {
var subject = "Tutorial: Simple Mail Merge";
MailApp.sendEmail(rowData.emailAddress, emailSubject, emailText, {attachments:[file.getAs(MimeType.PDF)]});
dataSheet.getRange(startRow + i, 5).setValue(EMAIL_SENT);
SpreadsheetApp.flush();
}
}
}