jeudi 21 novembre 2019

Trying to send email based on checkbox selection

I am trying to send emails automatically if the checkboxes in Column H are checked. I barely know enough about code to get in trouble. The code I have has been Frankensteined together, so I know I'm missing something but I'm too new at this to understand what. If I run the code as-is, an email is sent if there is an email address listed in Column G, but it is sent regardless of if Column H is checked or not. I don't want an email sent if the checkbox is not checked. I've searched through the forums and have tried to piece together a solution that works for me but I just keep hitting a wall. I know the problem lies with identifying the checkbox and that's where I'm getting lost. Any help would be greatly appreciated.

var EMAIL_SENT = "EMAIL_SENT";

function send1stnotice() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var startRow = 3;  // First row of data to process
  var numRows = sheet.getLastRow()-1   // Number of rows to process
  var dataRange = sheet.getRange(startRow, 1, numRows, 1000)
  // Fetch values for each row in the Range.
  var data = dataRange.getValues();
  for (var i = 0; i < data.length; ++i) {
    var row = data[i];
    var checkbox1 =row[7];
    var emailAddress = row[6];
    if (emailAddress.match('@') == null){
        continue;
    };  // First column
    var subject = "test subject";     
    var message = "test message";    
    var emailSent = row[8];     

    if (emailSent != EMAIL_SENT &&  checkbox1!= 'FALSE') {  // Prevents sending duplicates

      MailApp.sendEmail(emailAddress, subject, message);
      sheet.getRange(startRow + i, 9).setValue(EMAIL_SENT);
      // Make sure the cell is updated right away in case the script is interrupted
      SpreadsheetApp.flush();
    }
  }
  }



Aucun commentaire:

Enregistrer un commentaire