function sendEmails() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2; // First row of data to process
var numRows = 5; // Number of rows to process
// Fetch the range of cells A2:I6
var dataRange = sheet.getRange(startRow, 1, numRows, 10);
// Fetch values for each row in the Range.
var data = dataRange.getValues();
var counter = 0;
var qua_num = [[]];
for (var i in data) {
var row = data[i];
var days = row[8];
if (days > -14) {
var emailAddress = row[9]; // First column
qua_num += [row[0]];
counter += 1;
}
}
var message = qua_num + ' equipment are approaching due date or out of cal. Please check with QA for replacement or sending the equipment out for re-calibration'; // Second column
var subject = 'Check Equipment Calibration Alert';
MailApp.sendEmail(emailAddress, subject, message);
}
当前代码仅允许我从Google电子表格发送到一个电子邮件地址。如果每行中都有一个不同的电子邮件地址(多行也可能具有相同的电子邮件地址),如何更改功能?请参见下面的Google电子表格示例。
Screenshot of Google Spreadsheet
根据电子表格中的信息,QUA-005和QUA-007满足if语句的条件。预期的输出将是:
通过电子邮件1发送到电子邮件地址1 QUA-005设备即将到期或过期。请与质量检查部门联系以进行更换或将设备寄出进行重新校准。
通过电子邮件2发送到电子邮件地址2 QUA-007设备即将到期或过期。请与质量检查部门联系以进行更换或将设备寄出进行重新校准。
如果电子邮件地址相同,则输出为:
QUA-005,QUA-007设备即将到期或过期。请与质量检查部门联系以进行更换或将设备寄出进行重新校准。
答案 0 :(得分:2)
-14
时,您想将电子邮件发送到“ J”列的电子邮件地址。如果我的理解正确,那么这个答案如何?请认为这只是几个可能的答案之一。
修改脚本后,请进行以下修改。
var counter = 0;
var qua_num = [[]];
for (var i in data) {
var row = data[i];
var days = row[8];
if (days > -14) {
var emailAddress = row[9]; // First column
qua_num += [row[0]];
counter += 1;
}
}
var message = qua_num + ' equipment are approaching due date or out of cal. Please check with QA for replacement or sending the equipment out for re-calibration'; // Second column
var subject = 'Check Equipment Calibration Alert';
MailApp.sendEmail(emailAddress, subject, message);
data.reduce((m, [qua_num,,,,,,,,daysPastCal,emailAddress]) => {
if (daysPastCal > -14) m.set(emailAddress, m.has(emailAddress) ? [...m.get(emailAddress), qua_num] : [qua_num]);
return m;
}, new Map())
.forEach((qua_num, emailAddress) => {
var message = qua_num.join(",") + ' equipment are approaching due date or out of cal. Please check with QA for replacement or sending the equipment out for re-calibration';
var subject = 'Check Equipment Calibration Alert';
MailApp.sendEmail(emailAddress, subject, message);
});