简体   繁体   中英

I don't know why this script ommits sending emails to some of the recipients

I want to run this script on 2 separate target numbers on a certain sheet within the spreadsheet. The idea is to check individual job budgets and send out an email if it is too low then mark column C with EMAIL sent so I know it was successful. I only seems to be sending email to the first out of 4 address on the first checksales but sending to all 4 emails on the 2nd checksales.

  var monthSalesRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("PROJECT PIPELINE").getRange("I4"); 
  var monthSales = monthSalesRange.getValue();
  var ui = SpreadsheetApp.getUi(); 
  // Check totals sales
  if (monthSales < 7000){
    ui.alert('Epic SG Budget Running Low!');
var EMAIL_SENT = 'EMAIL_SENT';
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Emails");
  var startRow = 2; // First row of data to process
  var numRows = 4; // Number of rows to process
  var dataRange = sheet.getRange(2,1,4,3);
  // 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 emailAddress = row[0]; // First column
    var message = row[1]; // Second column
    var emailSent = row[2]; // Third column
    if (emailSent !== EMAIL_SENT) { // Prevents sending duplicates
      var subject = 'Low Labor Budget Alert';
      MailApp.sendEmail(emailAddress, subject, message);
      sheet.getRange(startRow + i, 3).setValue("EMAIL_SENT");
      // Make sure the cell is updated right away in case the script is interrupted
      SpreadsheetApp.flush()
  var monthSalesRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("PROJECT PIPELINE").getRange("I10"); 
  var monthSales = monthSalesRange.getValue();
  var ui = SpreadsheetApp.getUi(); 
  // Check totals sales
  if (monthSales < 8000){
    ui.alert('Seward Budget Running Low!');
var EMAIL_SENT = 'EMAIL_SENT';
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Emails");
  var startRow = 10; // First row of data to process
  var numRows = 4; // Number of rows to process
  // Fetch the range of cells A10:B11
  var dataRange = sheet.getRange(10,1,4,3);
  // 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 emailAddress = row[0]; // First column
    var message = row[1]; // Second column
    var emailSent = row[2]; // Third column
    if (emailSent !== EMAIL_SENT) { // Prevents sending duplicates
      var subject = 'Low Labor Budget Alert';
      MailApp.sendEmail(emailAddress, subject, message);
      sheet.getRange(startRow + i, 3).setValue("EMAIL_SENT");
      // Make sure the cell is updated right away in case the script is interrupted
      SpreadsheetApp.flush()
    }
  }
}
    }
  }
  }
}


You haven't closed off your first for loop and if statement.

You need to add }} after your first SpreadsheetApp.flush() ie

  SpreadsheetApp.flush() } }

And you'll have to remove a couple of braces from the end of the script to get it to run.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM