为循环错误发送相同的多封电子邮件而不是一封电子邮件

Sending the same multiple emails instead of 1 email for loop bug

我在 运行 发送电子邮件功能时遇到错误。它发送多封电子邮件而不是一封电子邮件通知这里是我的代码我做错了什么??!?!我收到了 31 封相同的电子邮件。我相信 for 循环的问题是每次 if 语句为真时都会发送一封电子邮件,而不是只有一次如果它真的有帮助。

这是我的代码:

function sendEmail(){

var ss = SpreadsheetApp.getActiveSpreadsheet(); //get active spreadsheet only! to get the url for the filter view
var SpreadsheetID = ss.getSheetId(); // get the sheet Id 
var spreadsheetURL = ss.getUrl(); // get the current active sheet url 
var SpreadsheetID = spreadsheetURL.split("/")[5]; // using the last / for getting the last parts of the email
var filterViewName = 'PO_Log Precentage'; // Name of the filter view you want to get the url from & MAKE SURE Title matches view name account for "spaces" too
var filterViewID = filterId(SpreadsheetID, filterViewName); // Getting filter view id 
var url = createURL(spreadsheetURL, filterViewID); // creating the url to send the filter view id
Logger.log(url);// Testing to see the correct url is created 
var po_numID = ss.getSheetByName("Purchase Orders List").getRange("A2").getDisplayValue().substr(0,3);// Gets the Purchase Order List Sheet and the PO# the first 3 Characters of the PO in A2
Logger.log(po_numID);
var email_va = ss.getSheetByName("Purchase Orders List");

//gonna build statuses to look for into array
var statusesToEmail = ['On-going', '']

//"Status" is in Column T (Col 2)
//"Precent" is in Column Q  (Col 3)

var data = email_va.getDataRange().getValues()
//  //var headerRowNumber = 1; // When checking for emails in the sheet you want to exclude the header/title row 

var emailDataSheet = SpreadsheetApp.openByUrl("https://docs.google.com/spreadsheets/d/17G0QohHxjuAcZzwRtQ6AUW3aMTEvLnmTPs_USGcwvDA/edit#gid=1242890521").getSheetByName("TestA"); // Get The URL from another spreadsheet based on URL

Logger.log(emailDataSheet.getSheetName());


 var emailData = emailDataSheet.getRange("A2:A").getDisplayValues().flat().map(po => po.substr(0,3));
    Logger.log(emailData)///Working to get the first 3 charcters in column A

    var subject = po_numID + " Po Log Daily Notification "; // Unique PoTitle of the email 
    
    
    var options = {} // Using the html body for the email 
  
    options.htmlBody = "Hi All, " + "The following" + '<a href=\"' +url+ '" > Purchase Orders </a>' + "are over 90% spent" + "";

   for(var i = 0; i < data.length; i++){
      let row = data[i];
      if( statusesToEmail.includes(row[1]) & (row[2] >= .80)){
     emailData.every((po, index) => {
    if (po == po_numID){
      const email = emailDataSheet.getRange(index + 2,7).getValue();//Getting the last colmun on the same row when the Po# are the same.
      console.log(email);
      MailApp.sendEmail(email, subject, '', options); // Sending the email which includes the url in options and sending it to the email address after making sure the first 3 Charcters Of the PO_log are the same as 
      return false;
    } else {
      return true;
    }
  });
  }
}

}

这是电子表格 https://docs.google.com/spreadsheets/d/1QW5PIGzy_NSh4MT3j_7PggxXq4XcW4dCKr4wKqIAp0E/edit#gid=611584429

如果你想在循环完成后停止循环,你必须使用 break 函数,因为如果我没记错的话,如果满足 IF 条件,就会发送电子邮件,因此在具有 mailapp.sendemail ,你必须添加一个中断,否则循环将继续发生。这是 javascript 的基础,你应该阅读更多关于 FOR 循环 here

break 只需在代码末尾键入“break”,这样脚本就不会在满足条件后继续循环。