Google Sheet Script - 未成功的电子邮件自动化

时间:2018-02-22 13:30:12

标签: google-apps-script google-sheets

我正在尝试整理一个脚本,该脚本循环显示电子表格中的二维范围;当满足标准时(在这种情况下,在需要续订之前,培训分别是一个月和一周),应发送一封电子邮件。

当脚本只需循环一行包含多行时,我就能实现这一点;但是现在我正在尝试循环遍历多个列和多行,脚本正在运行而没有错误,但数据不会触发要发送的电子邮件。

我正在尝试使用的较小版本的脚本显示在下面,尽管原始版本更广泛,试图覆盖大范围的数据。此处不存在的脚本是需要考虑的每个列的“列”部分的重复。

function trainingReminder() {
//For testing purposes
//You can view the log by typing CMD+ENTER (CTRL + E) after you run the script.
Logger.log(msg);

// grab the Google Sheet object
var googleSheets = SpreadsheetApp.getActiveSpreadsheet();
// activates the first sheet
SpreadsheetApp.setActiveSheet(googleSheets.getSheets()[0]);
// grabs the sheet
var currentSheet = googleSheets.getActiveSheet();

// find the last row
var lastRow = currentSheet.getLastRow();

// The first row starts at row 2 to account for headers
var startRow = 2;

//**************************************************************//
//                        Column AU                             //
//**************************************************************//

// get AU column data (Training Name)
var range = currentSheet.getRange(2,48,lastRow-startRow+1,1 );
var numRows = range.getNumRows();
var remainingDays = range.getValues();

// get AR column data (Employee Names)
//range = currentSheet.getRange(2, 45, lastRow-startRow+1, 1);

var columnAR = range.getValues();

var warning_count = 0;
var msg = "";

// Loop column AU (Training Name) (1 Month)
for (var i = 0; i <= numRows - 1; i++) {
var daysLeft = remainingDays[i][0];
if(daysLeft == 30) {
// Checks to see if a cell value in column AU equals 30
var employeeName = columnAR[i][0];
msg = msg + "Don't Forget: "+employeeName+"'s "+employeeTraining+" is due in
"+daysLeft+" days.\n";
warning_count++;
}
}

// Loop column AU (Training Name) (1 Week)
for (var i = 0; i <= numRows - 1; i++) {
var daysLeft = remainingDays[i][0];
if(daysLeft == 7) {
// Checks to see if a cell value in column AU equals 7
var employeeName = columnAR[i][0];
var employeeTraining = range.getValue(1,48);

msg = msg + "Don't Forget: "+employeeName+"'s "+employeeTraining+" is due a week.\n";
warning_count++;
}
}

//**************************************************************//
//                       Column AV                              //
//**************************************************************//

// get AV column data (Training Name)
var range = currentSheet.getRange(2,49,lastRow-startRow+1,1 );
var numRows = range.getNumRows();
var remainingDays = range.getValues();

// get AR column data (Employee Names)
//range = currentSheet.getRange(2, 45, lastRow-startRow+1, 1);

var columnAR = range.getValues();

var warning_count = 0;
var msg = "";

// Loop column AV (Training Name) (1 Month)
for (var i = 0; i <= numRows - 1; i++) {
var daysLeft = remainingDays[i][0];
if(daysLeft == 30) {
// Checks to see if a cell value in column AV equals 30
var employeeName = columnAR[i][0];
var employeeTraining = range.getValue(1,49)
msg = msg + "Don't Forget: "+employeeName+"'s "+employeeTraining+" is due in "+daysLeft+" days.\n";
warning_count++;
}
}

// Loop column AV (Training Name) (1 Week)
for (var i = 0; i <= numRows - 1; i++) {
var daysLeft = remainingDays[i][0];
if(daysLeft == 7) {
// Checks to see if a cell value in column AV equals 7
var employeeName = columnAR[i][0];
var employeeTraining = range.getValue(1,49);

msg = msg + "Don't Forget: "+employeeName+"'s "+employeeTraining+" is due a week.\n";
warning_count++;
}
}

//**************************************************************//
//                       Column AW                              //
//**************************************************************//

// get AW column data (Training Name)
var range = currentSheet.getRange(2,50,lastRow-startRow+1,1 );
var numRows = range.getNumRows();
var remainingDays = range.getValues();

// get AR column data (Employee Names)
//range = currentSheet.getRange(2, 45, lastRow-startRow+1, 1);

var columnAR = range.getValues();

var warning_count = 0;
var msg = "";

// Loop column AW (Training Name) (1 Month)
for (var i = 0; i <= numRows - 1; i++) {
var daysLeft = remainingDays[i][0];
if(daysLeft == 30) {
// Checks to see if a cell value in column AW equals 30
var employeeName = columnAR[i][0];
var employeeTraining = range.getValue(1,50)

msg = msg + "Don't Forget: "+employeeName+"'s "+employeeTraining+" is due in "+daysLeft+" days.\n";
warning_count++;
}
}

// Loop column AW (Training Name) (1 Week)
for (var i = 0; i <= numRows - 1; i++) {
var daysLeft = remainingDays[i][0];
if(daysLeft == 7) {
// Checks to see if a cell value in column AW equals 7
var employeeName = columnAR[i][0];
var employeeTraining = range.getValue(1,50);

msg = msg + "Don't Forget: "+employeeName+"'s "+employeeTraining+" is due a week.\n";
warning_count++;
}
}

// Loop column CG (Training Name) (1 Week)
for (var i = 0; i <= numRows - 1; i++) {
var daysLeft = remainingDays[i][0];
if(daysLeft == 7) {
// Checks to see if a cell value in column CG equals 7
var employeeName = columnAR[i][0];
var employeeTraining = range.getValue(1,86);

msg = msg + "Don't Forget: "+employeeName+"'s "+employeeTraining+" is due a week.\n";
warning_count++;
}
}

if(warning_count) {
MailApp.sendEmail("example@email.com",
"Training Reminder", msg);
}
};

我意识到任何有能力编写脚本的人都可能会有一些非常明显的东西,但不幸的是我还没有它。任何帮助将不胜感激!

0 个答案:

没有答案