0

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:

var EMAIL_SENT = 'EMAIL_SENT';

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:

 sheet

Any help appreciated!

0

Solution

replace

 for (i in data)

by

 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.

Reference

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in

| improve this answer | |

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.