应用程序脚本 - 根据剩余天数发送电子邮件 - 无法让脚本将其发送至剩余 X 天
App Script - Send Email based on days left to date - Can't make script send it X days left to date
我正在使用这个电子表格Here
我正在尝试在特定日期还剩 3 天时向特定收件人“email@myemail.com”发送电子邮件。
每行代表一个人,我希望发送的电子邮件为每个离职日期 - 今天正好是 3 天的人发送一封电子邮件给我。
这是我的输出:Email received
这是我正在使用的脚本:
function Offboarding_Reminder() {
// get the spreadsheet object
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
// set the first sheet as active
SpreadsheetApp.setActiveSheet(spreadsheet.getSheets()[0]);
// fetch this sheet
var sheet = spreadsheet.getActiveSheet();
// figure out what the last row is
var lastRow = sheet.getLastRow();
// the rows are indexed starting at 1, and the first row
// is the headers, so start with row
var startRow = 2;
// grab column 11 (the 'days left' column)
var range = sheet.getRange(2,12,lastRow-startRow+1,1 );
var numRows = range.getNumRows();
var days_left_values = range.getValues();
// Now, grab the user name column
range = sheet.getRange(2, 1, lastRow-startRow+1, 1);
var reminder_name = range.getValues();
var warning_count = 0;
var msg = "";
// Loop over the days left values
for (var i = 0; i < numRows - 1; i++) {
var days_left = days_left_values[i][0];
if(days_left == 3) {
// if it's exactly 3, do something with the data.
var user_name = reminder_name[0][i];
msg ="Reminder:"+reminder_name+" offboarding is due in "+days_left+" days.\n";
warning_count++;
}
}
if(warning_count) {
MailApp.sendEmail("email@myemail.com","Reminder Offboarding",msg)
}
};
两件事我不明白:
- 为什么我发送的电子邮件给我所有的用户电子邮件,而不是只有 days_left == 3 的电子邮件?
- 要使此脚本自动化,一旦它起作用,我应该向应用脚本添加一个触发器吗?
感谢您的帮助!
实际上我修改了你的代码,因为它没有优化。它有很多多余和不必要的行。
更改如下:
- 一次获取了从
user
列到 days_left
列的范围,而不是单独获取
- 创建了数组来存储符合条件的用户
- 格式化电子邮件以使其看起来更好。
代码:
function offboardingReminder() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
// set active sheet to first sheet
spreadsheet.setActiveSheet(spreadsheet.getSheets()[0]);
var sheet = spreadsheet.getActiveSheet();
// figure out what the last row is
var lastRow = sheet.getLastRow();
var startRow = 2;
// grab all data from user to days left
var range = sheet.getRange(startRow, 1, lastRow - startRow + 1, 12);
var values = range.getValues();
var users = [];
// loop all data per row
values.forEach(function(row) {
// if days left is 3
if(row[11] == 3) {
// add user if 3 days left
users.push(row[0]);
}
});
// if users has elements
if(users) {
// Formatted the message as html to look nicer
var message = "<html><body><h1>Reminder!!!</h1><p>The following user/s offboarding is due in 3 days:</p>";
// created bulleted list for list of users
var emails = "<ul>";
users.forEach(function(user){
emails = emails + "<li>" + user + "</li>";
});
emails += "</ul>";
message = message + emails + "</body></html>";
MailApp.sendEmail("email@myemail.com", "Reminder Offboarding", "", {htmlBody: message, noReply: true});
}
}
示例数据:
示例输出:
注:
- 您可以改为在脚本中计算剩余天数,但如果工作表中已经需要它,那么重新使用它就可以了。
- 我通过公式
=DAYS(C2,TODAY())
填充了第 2 行的剩余天数(拖到其他行)
我正在使用这个电子表格Here
我正在尝试在特定日期还剩 3 天时向特定收件人“email@myemail.com”发送电子邮件。 每行代表一个人,我希望发送的电子邮件为每个离职日期 - 今天正好是 3 天的人发送一封电子邮件给我。
这是我的输出:Email received
这是我正在使用的脚本:
function Offboarding_Reminder() {
// get the spreadsheet object
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
// set the first sheet as active
SpreadsheetApp.setActiveSheet(spreadsheet.getSheets()[0]);
// fetch this sheet
var sheet = spreadsheet.getActiveSheet();
// figure out what the last row is
var lastRow = sheet.getLastRow();
// the rows are indexed starting at 1, and the first row
// is the headers, so start with row
var startRow = 2;
// grab column 11 (the 'days left' column)
var range = sheet.getRange(2,12,lastRow-startRow+1,1 );
var numRows = range.getNumRows();
var days_left_values = range.getValues();
// Now, grab the user name column
range = sheet.getRange(2, 1, lastRow-startRow+1, 1);
var reminder_name = range.getValues();
var warning_count = 0;
var msg = "";
// Loop over the days left values
for (var i = 0; i < numRows - 1; i++) {
var days_left = days_left_values[i][0];
if(days_left == 3) {
// if it's exactly 3, do something with the data.
var user_name = reminder_name[0][i];
msg ="Reminder:"+reminder_name+" offboarding is due in "+days_left+" days.\n";
warning_count++;
}
}
if(warning_count) {
MailApp.sendEmail("email@myemail.com","Reminder Offboarding",msg)
}
};
两件事我不明白:
- 为什么我发送的电子邮件给我所有的用户电子邮件,而不是只有 days_left == 3 的电子邮件?
- 要使此脚本自动化,一旦它起作用,我应该向应用脚本添加一个触发器吗?
感谢您的帮助!
实际上我修改了你的代码,因为它没有优化。它有很多多余和不必要的行。
更改如下:
- 一次获取了从
user
列到days_left
列的范围,而不是单独获取 - 创建了数组来存储符合条件的用户
- 格式化电子邮件以使其看起来更好。
代码:
function offboardingReminder() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
// set active sheet to first sheet
spreadsheet.setActiveSheet(spreadsheet.getSheets()[0]);
var sheet = spreadsheet.getActiveSheet();
// figure out what the last row is
var lastRow = sheet.getLastRow();
var startRow = 2;
// grab all data from user to days left
var range = sheet.getRange(startRow, 1, lastRow - startRow + 1, 12);
var values = range.getValues();
var users = [];
// loop all data per row
values.forEach(function(row) {
// if days left is 3
if(row[11] == 3) {
// add user if 3 days left
users.push(row[0]);
}
});
// if users has elements
if(users) {
// Formatted the message as html to look nicer
var message = "<html><body><h1>Reminder!!!</h1><p>The following user/s offboarding is due in 3 days:</p>";
// created bulleted list for list of users
var emails = "<ul>";
users.forEach(function(user){
emails = emails + "<li>" + user + "</li>";
});
emails += "</ul>";
message = message + emails + "</body></html>";
MailApp.sendEmail("email@myemail.com", "Reminder Offboarding", "", {htmlBody: message, noReply: true});
}
}
示例数据:
示例输出:
注:
- 您可以改为在脚本中计算剩余天数,但如果工作表中已经需要它,那么重新使用它就可以了。
- 我通过公式
=DAYS(C2,TODAY())
填充了第 2 行的剩余天数(拖到其他行)