自动化Google电子表格–电子邮件提醒

时间:2018-09-25 12:11:51

标签: google-apps-script google-sheets

我有一个Google Spreadsheet,并且已按照指示修改以下代码,以便在到期日期的7天前发送电子邮件警报。这可以完美地工作,但是只查看第一张工作表,而电子表格总共包含23个工作表。我想我需要包含一个数组并循环执行代码,但无法解决此问题,因此,我们将不胜感激!预先感谢。

function checkReminder() {
  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 2
  var startRow = 7;

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

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

  // Now, grab the deceased name column
  range = sheet.getRange(3, 1);
  var name_of_sheet = range.getValue();

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

      msg = msg + "Reminder: " + name_of_sheet + " inscription work is due in " + days_left + " days.\n";
      warning_count++;
    }
  }

  if(warning_count) {
    MailApp.sendEmail("my@email.com", 
        "Reminder Inscription Schedule Message", msg);
  }
};

0 个答案:

没有答案