Here is my code. I have a spreadsheet with 8 column headers, linked here https://docs.google.com/spreadsheets/d/1ZQp9GIzLrkEsOF6k_TwAZeUi7v5u94GBrDEKoB3Wk-8/edit?usp=sharing I am trying to get column I to have "email_sent" added to it to prevent sending duplicates every time someone submits details to the sheet. The script is running and sending an email formatted as per the Var = message section but I am having issues with it placing an "email_sent" message in column I and not sending the email on the next trigger of the script. I think it is the "sheet.getRange(rowData + i, 'I2:I1000').setValue('EMAIL_SENT');" section that is not working but unsure what is wrong.
function sendEmails() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
ss.setActiveSheet(ss.getSheetByName("Email"));
var sheet = SpreadsheetApp.getActiveSheet();
var dataRange = sheet.getRange("A2:I1000");
var data = dataRange.getValues();
for (var i = 0; i < data.length; ++i) {
var rowData = data[i];
var emailAddress = rowData[1];
var recipient = rowData[0];
var message1 = rowData[3];
var message2 = rowData[4];
var message3 = rowData[5];
var message4 = rowData[6];
var message5 = rowData[7];
var emailSent = rowData[9];
if (emailSent != 'EMAIL_SENT') { // Prevents sending duplicates
var message = 'Hi ' + recipient + ',\n\n' + message1 + ' ' + message2 +
',\n\n' + message3 + ',\n' + message4 + ',\n' + message5;
var subject = 'Medical Questionairre Check';
MailApp.sendEmail(emailAddress, subject, message);
sheet.getRange(rowData + i, 'I2:I1000').setValue('EMAIL_SENT');
// Make sure the cell is updated right away in case the script is
interrupted
SpreadsheetApp.flush();
}
}
}