电子邮件提醒多列

时间:2017-12-14 07:16:08

标签: google-apps-script google-sheets

如果日期列距离今天7天或1天,我想发送电子邮件提醒。我已经制作了脚本,我想要添加的是它应该考虑其他列,不仅仅是一个脚本,用于发送相应列的提醒。

例如: 它应该提醒计划日期,计划日期1,计划日期2和计划日期3.

请参阅Sample Attached

脚本:

function checkReminder() {
// get the spreadsheet object
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
// fetch this sheet
var sheet = spreadsheet.getSheets()[0];

// figure out what the last row is
var lastRow = sheet.getLastRow();

// figure out what the last column is
var lastCol = sheet.getLastColumn();

// the rows are indexed starting at 1, and the first row
// is the headers, so start with row 2
var startRow = 2;

// the columns are indexed starting at 2, and the first column
// is the headers, so start with column 2
var startCol = 2;

// grab column 3 (the 'days left' column) 
var range = sheet.getRange(2,3,lastRow-startRow+1,1 );
var numRows = range.getNumRows();
var days_left_values = range.getValues();

// Now, grab the reminder name column
range = sheet.getRange(2, 1, lastRow-startRow+1, 1);
var reminder_info_values = range.getValues();

// Now, grab the first row
range = sheet.getRange(1, 2, lastCol-startCol+1, 1);
var column_info_values = 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 == 1) {
  // if it's exactly 1, do something with the data.
  var reminder_name = reminder_info_values[i][0];
  var column_name = column_info_values[0][0];

  msg = msg + "Reminder: "+reminder_name+" - "+column_name+" is due in "+days_left+" day.\n";
  warning_count++;
  }
}
for (var i = 0; i <= numRows - 1; i++) {
var days_left = days_left_values[i][0];
if(days_left == 7) {
  // if it's exactly 7, do something with the data.
  var reminder_name = reminder_info_values[i][0];
  var column_name = column_info_values[0][0];

  msg = msg + "Reminder: "+reminder_name+" - "+column_name+" is due in "+days_left+" days.\n";
  warning_count++;
}
}

 if(warning_count) {
MailApp.sendEmail("myidsample@gmail.com", 
    "Reminder Spreadsheet Message", msg);
}

};

1 个答案:

答案 0 :(得分:0)

您目前只提取单列并仅处理该列。 为了使这个脚本也在其他列上运行,你需要循环遍历它们,获取数据然后逐个处理它们。

您可以在此处从第3列获取数据:

// grab column 3 (the 'days left' column) 
var range = sheet.getRange(2,3,lastRow-startRow+1,1 );
var numRows = range.getNumRows();
var days_left_values = range.getValues();

同样地,你可以运行一个循环直到最后一列并从第3,第5,第7列获取数据并继续处理它们。

所以代码就像这样

function checkReminder() {
    // get the spreadsheet object
    var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
    // fetch this sheet
    var sheet = spreadsheet.getSheets()[0];

    // figure out what the last row is
    var lastRow = sheet.getLastRow();

    // figure out what the last column is
    var lastCol = sheet.getLastColumn();

    // the rows are indexed starting at 1, and the first row
    // is the headers, so start with row 2
    var startRow = 2;

    // the columns are indexed starting at 2, and the first column
    // is the headers, so start with column 2
    var startCol = 2;

    // Now, grab the reminder name column
    range = sheet.getRange(2, 1, lastRow - startRow + 1, 1);
    var reminder_info_values = range.getValues();

    // Now, grab the first row
    range = sheet.getRange(1, 2, lastCol - startCol + 1, 1);
    var column_info_values = range.getValues();

    var warning_count = 0;
    var msg = "";

    for (var u = 3; u <= lastCol; u += 2) {
        // grab the day's left column
        var range = sheet.getRange(2, u, lastRow - startRow + 1, 1);
        var numRows = range.getNumRows();
        var days_left_values = range.getValues();

        // 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 == 1) {
                // if it's exactly 1, do something with the data.
                var reminder_name = reminder_info_values[i][0];
                var column_name = column_info_values[0][0];

                msg = msg + "Reminder: " + reminder_name + " - " + column_name + " is due in " + days_left + " day.\n";
                warning_count++;
            }
        }
        for (var i = 0; i <= numRows - 1; i++) {
            var days_left = days_left_values[i][0];
            if (days_left == 7) {
                // if it's exactly 7, do something with the data.
                var reminder_name = reminder_info_values[i][0];
                var column_name = column_info_values[0][0];

                msg = msg + "Reminder: " + reminder_name + " - " + column_name + " is due in " + days_left + " days.\n";
                warning_count++;
            }
        }

        if (warning_count) {
            MailApp.sendEmail("myidsample@gmail.com",
                "Reminder Spreadsheet Message", msg);
        }
    }
}

这是未经测试的代码,如果出现任何问题,请与我们联系。我很乐意帮助你。

谢谢!