如何在满足条件后退出 ForEach 循环
How to exit out of ForEach loop once condition is met
我创建了一个小脚本,如果员工有出勤问题,它会生成一封电子邮件。我使用了两种模板,一种是员工有“迟到”违规行为,另一种是存在不包括“迟到”的其他违规行为(例如,未来电未出现、无故取消等)。但是,在我的 forEach 循环中,我不知道如何退出循环,所以我没有收到多封电子邮件。发送一 (1) 封电子邮件后如何退出循环?
function sendEiEmail(){
var ss = SpreadsheetApp.getActive().getSheetByName('Attendance Narrative Generator');
var arr = ss.getRange(7, 1, 10, 1).getValues(); //Get all values from sheet
// Logger.log(arr);
var htmlBodyNoLate = HtmlService.createTemplateFromFile("AttendanceNoLate").evaluate().getContent();
var htmlBodyLate = HtmlService.createTemplateFromFile("AttendanceLate").evaluate().getContent();
arr.forEach(function(res) {
if(res == 'Late'){
MailApp.sendEmail({
to: Session.getActiveUser().getEmail(),
subject: `Attendance Infractions Narrative - ${firstName} ${lastName} #${employeeEID}`,
htmlBody: htmlBodyLate,
});
return;
}else{
MailApp.sendEmail({
to: Session.getActiveUser().getEmail(),
subject: `Attendance Infractions Narrative - ${firstName} ${lastName} #${employeeEID}`,
htmlBody: htmlBodyNoLate,
});
}
});
}
不可能打破 forEach 循环。
(除非你想使用异常,但不推荐)
解决问题有两种方案:
- 将循环类型改为for,而不是forEach,然后使用break语句。
- 使用标记知道邮件已发送:
function sendEiEmail() {
var ss = SpreadsheetApp.getActive().getSheetByName('Attendance Narrative Generator');
var arr = ss.getRange(7, 1, 10, 1).getValues(); //Get all values from sheet
var htmlBodyNoLate = HtmlService.createTemplateFromFile("AttendanceNoLate").evaluate().getContent();
var htmlBodyLate = HtmlService.createTemplateFromFile("AttendanceLate").evaluate().getContent();
var isEmailSent = false;
arr.forEach(function (res) {
if (isEmailSent == false) {
if (res == 'Late') {
MailApp.sendEmail({
to: Session.getActiveUser().getEmail(),
subject: `Attendance Infractions Narrative - ${firstName} ${lastName} #${employeeEID}`,
htmlBody: htmlBodyLate,
});
isEmailSent = true;
} else {
MailApp.sendEmail({
to: Session.getActiveUser().getEmail(),
subject: `Attendance Infractions Narrative - ${firstName} ${lastName} #${employeeEID}`,
htmlBody: htmlBodyNoLate,
});
isEmailSent = true;
}
}
});
}
我创建了一个小脚本,如果员工有出勤问题,它会生成一封电子邮件。我使用了两种模板,一种是员工有“迟到”违规行为,另一种是存在不包括“迟到”的其他违规行为(例如,未来电未出现、无故取消等)。但是,在我的 forEach 循环中,我不知道如何退出循环,所以我没有收到多封电子邮件。发送一 (1) 封电子邮件后如何退出循环?
function sendEiEmail(){
var ss = SpreadsheetApp.getActive().getSheetByName('Attendance Narrative Generator');
var arr = ss.getRange(7, 1, 10, 1).getValues(); //Get all values from sheet
// Logger.log(arr);
var htmlBodyNoLate = HtmlService.createTemplateFromFile("AttendanceNoLate").evaluate().getContent();
var htmlBodyLate = HtmlService.createTemplateFromFile("AttendanceLate").evaluate().getContent();
arr.forEach(function(res) {
if(res == 'Late'){
MailApp.sendEmail({
to: Session.getActiveUser().getEmail(),
subject: `Attendance Infractions Narrative - ${firstName} ${lastName} #${employeeEID}`,
htmlBody: htmlBodyLate,
});
return;
}else{
MailApp.sendEmail({
to: Session.getActiveUser().getEmail(),
subject: `Attendance Infractions Narrative - ${firstName} ${lastName} #${employeeEID}`,
htmlBody: htmlBodyNoLate,
});
}
});
}
不可能打破 forEach 循环。 (除非你想使用异常,但不推荐) 解决问题有两种方案:
- 将循环类型改为for,而不是forEach,然后使用break语句。
- 使用标记知道邮件已发送:
function sendEiEmail() {
var ss = SpreadsheetApp.getActive().getSheetByName('Attendance Narrative Generator');
var arr = ss.getRange(7, 1, 10, 1).getValues(); //Get all values from sheet
var htmlBodyNoLate = HtmlService.createTemplateFromFile("AttendanceNoLate").evaluate().getContent();
var htmlBodyLate = HtmlService.createTemplateFromFile("AttendanceLate").evaluate().getContent();
var isEmailSent = false;
arr.forEach(function (res) {
if (isEmailSent == false) {
if (res == 'Late') {
MailApp.sendEmail({
to: Session.getActiveUser().getEmail(),
subject: `Attendance Infractions Narrative - ${firstName} ${lastName} #${employeeEID}`,
htmlBody: htmlBodyLate,
});
isEmailSent = true;
} else {
MailApp.sendEmail({
to: Session.getActiveUser().getEmail(),
subject: `Attendance Infractions Narrative - ${firstName} ${lastName} #${employeeEID}`,
htmlBody: htmlBodyNoLate,
});
isEmailSent = true;
}
}
});
}