证书到期时自动生成电子邮件通知
Automatically Generating an Email Notification When Certifications are Expiring
我有一个 excel 电子表格,其中包含我团队每个成员的所有认证的到期日期。随着到期时间的临近,单元格会根据证书的剩余有效期自动改变颜色。
我想要做的是在证书将在 90 天或更短时间内到期时发送电子邮件通知。
function sendEmail() {
// EMAIL Check & Date
// Fetch Cert Dates & Employees
var CertExpiration = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1").getRange(4,3,11,9); //All Cert Dates on Sheet
var CertDate = CertExpiration.getValue(); // Get Certificate Date as a value
var Employee = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1").getRange(4,2,9,1); //Retrieve list of employee Names
var EmployeeName = Employee.getValue(); // Set variable for email notification
var Today = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1").getRange(2,2,1,1); // Sets Today to Today() on sheet
// Check Cert Expiration
if (CertDate - Today < 90);
{
// Fetch the email address
var emailAddress = 'Example@Example.com';
// Send Alert Email.
var message = 'Certificate for ' + EmployeeName + ' expiring soon'; //
var subject = 'CERT EXPIRING SOON | CV CERT TRACKING';
MailApp.sendEmail(emailAddress, subject, message);
}
}
如果可能,我希望这封电子邮件包含符合条件 (CertDate - Today <90) 的所有 EmployeeName 的列表。此外,还有一种防止电子邮件被多次发送的方法。目前,电子邮件发送成功,但仅包含第一个引用的 EmployeeName。
我猜你需要一个循环函数来遍历所有数据。
function sendEmail() {
const data = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1").getRange(4,2,11,9).getValues();
for (let i = 0; i< data.length ;i++){
//change the [i][0] the second number in [] to the actual index of your number column. If your column number is 5, then the index will be 4
//index start from 0.
const EmployeeName = data[i][0];
const CertDates = data[i].slice(1);
const Today = new Date()
//this arr will hold each cert date less than 90 days.
const arr = [];
for(let j=0; j<CertDates.length ;j++ ){
const CertDate = CertDates[j];
if(typeof(CertDate)== 'object' && Math.floor((CertDate - Today ) / (1000*60*60*24)) < 90){
console.log(CertDate)
arr.push(CertDate);
}
}
//if any emlpoyees has expire date less than 90 days. 1 or many. It will send email to employee name.
if(arr.length>0){
// Fetch the email address
var emailAddress = 'Example@Example.com';
// Send Alert Email.
var message = 'Certificate for ' + EmployeeName + ' expiring soon'; //
var subject = 'CERT EXPIRING SOON | CV CERT TRACKING';
MailApp.sendEmail(emailAddress, subject, message);
}
}
我有一个 excel 电子表格,其中包含我团队每个成员的所有认证的到期日期。随着到期时间的临近,单元格会根据证书的剩余有效期自动改变颜色。
我想要做的是在证书将在 90 天或更短时间内到期时发送电子邮件通知。
function sendEmail() {
// EMAIL Check & Date
// Fetch Cert Dates & Employees
var CertExpiration = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1").getRange(4,3,11,9); //All Cert Dates on Sheet
var CertDate = CertExpiration.getValue(); // Get Certificate Date as a value
var Employee = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1").getRange(4,2,9,1); //Retrieve list of employee Names
var EmployeeName = Employee.getValue(); // Set variable for email notification
var Today = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1").getRange(2,2,1,1); // Sets Today to Today() on sheet
// Check Cert Expiration
if (CertDate - Today < 90);
{
// Fetch the email address
var emailAddress = 'Example@Example.com';
// Send Alert Email.
var message = 'Certificate for ' + EmployeeName + ' expiring soon'; //
var subject = 'CERT EXPIRING SOON | CV CERT TRACKING';
MailApp.sendEmail(emailAddress, subject, message);
}
}
如果可能,我希望这封电子邮件包含符合条件 (CertDate - Today <90) 的所有 EmployeeName 的列表。此外,还有一种防止电子邮件被多次发送的方法。目前,电子邮件发送成功,但仅包含第一个引用的 EmployeeName。
我猜你需要一个循环函数来遍历所有数据。
function sendEmail() {
const data = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1").getRange(4,2,11,9).getValues();
for (let i = 0; i< data.length ;i++){
//change the [i][0] the second number in [] to the actual index of your number column. If your column number is 5, then the index will be 4
//index start from 0.
const EmployeeName = data[i][0];
const CertDates = data[i].slice(1);
const Today = new Date()
//this arr will hold each cert date less than 90 days.
const arr = [];
for(let j=0; j<CertDates.length ;j++ ){
const CertDate = CertDates[j];
if(typeof(CertDate)== 'object' && Math.floor((CertDate - Today ) / (1000*60*60*24)) < 90){
console.log(CertDate)
arr.push(CertDate);
}
}
//if any emlpoyees has expire date less than 90 days. 1 or many. It will send email to employee name.
if(arr.length>0){
// Fetch the email address
var emailAddress = 'Example@Example.com';
// Send Alert Email.
var message = 'Certificate for ' + EmployeeName + ' expiring soon'; //
var subject = 'CERT EXPIRING SOON | CV CERT TRACKING';
MailApp.sendEmail(emailAddress, subject, message);
}
}