如何在Google脚本中总结从今天起的7天?

我下面的脚本几乎完成了,除了不是从今天开始增加7天,而是从8天之前增加7天,我不知道为什么。而不是得到2020年5月23日(如果是2020年5月16日运行),而是给我2020年5月15日。我已经浏览了很多次,没有看到错误。

有什么想法吗?

function emailAlert() {
  // today's date information
  var today = new Date();
  var todayMonth = today.getMonth() + 1;
  var todayDay = today.getDate();
  var todayYear = today.getFullYear();

  // 1 week from now
  var oneweeksFromToday = today;
  oneweeksFromToday.setDate(oneweeksFromToday.getDate() + 7);
  var oneweeksMonth = oneweeksFromToday.getMonth() + 1;
  var oneweeksDay = oneweeksFromToday.getDate();
  var oneweeksYear = oneweeksFromToday.getYear();

  // 1 month from now
  var oneMonthFromToday = new Date(todayYear, todayMonth, todayDay);
  var oneMonthMonth = oneMonthFromToday.getMonth() + 1;
  var oneMonthDay = oneMonthFromToday.getDate();
  var oneMonthYear = oneMonthFromToday.getYear();

  // getting data from spreadsheet
  var sheet = SpreadsheetApp.getActiveSheet();
  var startRow = 2; // First row of data to process
  var numRows = 100; // Number of rows to process

  var dataRange = sheet.getRange(startRow, 1, numRows, 999);
  var data = dataRange.getValues();

  //looping through all of the rows
  for (var i = 0; i < data.length; ++i) {
    var row = data[i];

    var expireDateFormat = Utilities.formatDate(
      new Date(row[6]),
      'ET',
      'MM/dd/yyyy'
    );

    var dueDateFormat = Utilities.formatDate(
      new Date(row[5]),
      'ET',
      'MM/dd/yyyy'
    );


    // email information
    var subject = '';
    var message =
      ' Good morning' + ', this is a friendly reminder that project ' + row[0] + ' is scheduled to begin on '+ dueDateFormat + '.' + ' Please review the sheet below and update your progress accordingly; note this project is due on ' +
      expireDateFormat + '.';

    //expiration date information
    var expireDateMonth = new Date(row[6]).getMonth() + 1;
    var expireDateDay = new Date(row[6]).getDate();
    var expireDateYear = new Date(row[6]).getYear();

   //checking for 1 week from now

    Logger.log('1 weeks month, expire month' + oneweeksMonth + expireDateMonth);
    if (
      expireDateMonth === oneweeksMonth &&
      expireDateDay === oneweeksDay &&
      expireDateYear === oneweeksYear
    ) {
      var subject =
        'Project reminder: ' +
        row[0] +
          ' is scheduled to begin on '+ dueDateFormat;
      MailApp.sendEmail('jgom839@gmail.com', subject, message);
      Logger.log('2 weeks from now');
    }

    //checking for 1 month from now
    if (
      expireDateMonth === oneMonthMonth &&
      expireDateDay === oneMonthDay &&
      expireDateYear === oneMonthYear
    ) {
      var subject =
        'A license is expiring in 1 month: ' +
        row[7] +
        ' - ' +
        expireDateFormat;
      MailApp.sendEmail('jgom839@gmail.com', subject, message);
      Logger.log('1 month from now');
    }
  }
}


评论