I took this script from the Google developer site and modified it to suit my needs. The issue i am having is where the "EMAIL-SENT" lands on my sheet.

I need it to be placed on the 4th column right next to the email that was just sent. Instead it falls on the 4th column on row 20 and row 21. Here is the modified script:


function sendEmails() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var startRow = 2; 
  var numRows = 2;
  var dataRange = sheet.getRange(startRow, 1, numRows, 3) 
  var data = dataRange.getValues();
  for (i in data) {
   var row = data[i];
   var emailAddress = row[0]; 
   var message = "REJECTED LOAD NUMBER:  " + row[1] + row[2] + ",\n"; 
   var subject = "REJECTED LOAD";
   var emailSent = row[2];
   if (emailSent != EMAIL_SENT) {
   MailApp.sendEmail(emailAddress, subject, message);
   sheet.getRange(startRow + i, 4).setValue(EMAIL_SENT);

I'm not the best script writer but I can't figure this out.

Here is an image of how it is appearing in my sheet:


Any help appreciated!

1 Answer 1




 for (i in data)


 for(var i = 0; i < data.length; i++)

Maybe you think that i on for (i in data) is the the same as for(var i = 0; i < data.length; i++) but it isn't. On each iteration it has assigned a property name rather than integer.



Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.