Google-app-script:在电子表格中使用单元格行[]的javascript电子邮件正文中的日期格式问题

Google-app-script: Date formating issue in a javascript email body using a cell row[] in Spreadsheet

本文关键字:javascript 问题 电子邮件 正文 日期 格式 电子表格 Google-app-script 单元格      更新时间:2023-09-26

我试图修改一个脚本,以我自己的需要,但我得到一个错误"TypeError:函数getDate未在对象中找到。(第13行,文件"Code")".

事实上,我想将今天的日期与[7]行中包含的日期进行比较,并针对包含Projects…的电子表格的每一行向一些人发送提醒。

下面是我的实际代码:
function sendEmails(step) {
  var sheet = SpreadsheetApp.getActiveSheet();
  var startRow = 3;  // First row of data to process, Start at second row because the first row contains the data labels
  var numRows = sheet.getLastRow();   // Number of rows to process -> all rows which are not empty
  var totalRows = numRows - startRow +1; // total rows to process is numRows on which we remove start row +1
  // Fetch the range of cells
  var dataRange = sheet.getRange(startRow, 1, totalRows, 20) // range of columns to process
  // Fetch values for each row in the Range
  var data = dataRange.getValues();
  for (i in data) {
    var row = data[i];
    var emailAddress = row[2] + ", " + row [3] + ", " + row [4];  // email addresses for email distribution
    var messagePVEnd = "The PV of project " + "'"+ row[0] +"'" + " is ending the " + row[7].getDate() + "/" + (row[7].getMonth+1) + "/" + row[7].getFullYear() + " , please push " + row[1] + " to get the reports.";       // Email content for PV End
    var messagePVMidStatus = "The PV of project " + "'" + row[0] + "'" + " will be at 500h the " + row[6].getDate() + "/" + (row[6].getMonth()+1) + "/" + row[6].getFullYear() + " , please push " + row[1] + " to get the intermediate status.";       // Email content for PV after 500h
    var messagePVOut = "The PV of project " + "'"+ row[0] +"'" + " is supposed to be finished since " + row[7].getDate() + "/" + (row[7].getMonth()+1) + "/" + row[7].getFullYear() + " , please push " + row[1] + " to get the reports and confirm that all reports are received in PV follow up google sheet.";       // Email content for PV Out
    var subjectPVEnd = row [0] + " -- Reminder for PV ending to get report from " + row [1]; // Email subject for PV end
    var subjectPVMidStatus = row [0] + " -- Reminder to get PV intermediate status after 500h from " + row [1]; // Email subject for PV after 500h
    var subjectPVOut = row [0] + " -- Reminder, PV should be finished with all reports received from " + row [1]; // Email subject for PV Out
    if (row[8]==5) { // if date of PV status after 500h is equal to 5 days
      MailApp.sendEmail(emailAddress, subjectPVMidStatus, messagePVMidStatus)
    }
    else if (row[8]==1) { // if date of PV status after 500h is equal to 1 day
      MailApp.sendEmail(emailAddress, subjectPVMidStatus, messagePVMidStatus)
    }
    else if (row[9]==5) { // if PV end date is equal to 5 days
      MailApp.sendEmail(emailAddress, subjectPVEnd, messagePVEnd)
    }
    else if (row[9]==1) { // if PV end date is equal to 1 day
      MailApp.sendEmail(emailAddress, subjectPVEnd, messagePVEnd)
    }
    else if (row[10]!="yes") {
      for (var j=1; j<=9; j++) {
          if (row[9]==-(j*10)) { // if PV end date is out of date by multiple of 10 days up to 100 days (except if report are received)
          MailApp.sendEmail(emailAddress, subjectPVOut, messagePVOut)
        }
      }
    }
  }
}

电子邮件发送正确,但我得到的问题与消息中的日期格式,我不知道我做错了什么。欢迎任何支持!

提前感谢,

编辑17/08:下面是相应电子表格的图片。此处输入图像描述

请注意,row[7]这是单元格值,而不是对象。因此,要与当前日期进行比较,需要var now = new Date();然后将其与row[7]进行比较

似乎从电子表格[7]行返回的数据不是日期类型。为了手动检查,双击电子表格单元格,看看是否弹出日历。如果不是,则从工作表返回的数据不是日期对象类型。在代码中,首先检查行[7]是否为instanceof Date。如果是,则按原样处理,否则,将从该单元格返回的日期字符串转换为日期对象,然后进一步处理。修改后的部分代码将如下所示

    var emailAddress = row[2] + ", " + row [3] + ", " + row [4];  // email addresses for email distribution
    var dateStr = '';
    if(row[7] instanceof Date){
      dateStr = row[6].getDate() + "/" + (row[6].getMonth()+1) + "/" + row[6].getFullYear();
    }
    else {
      dateStr = row[7];
    }
//    var messagePVEnd = "The PV of project " + "'"+ row[0] +"'" + " is ending the " + dateStr + " , please push " + row[1] + " to get the reports.";       // Email content for PV End
    var messagePVMidStatus = "The PV of project " + "'" + row[0] + "'" + " will be at 500h the " + dateStr + " , please push " + row[1] + " to get the intermediate status.";       // Email content for PV after 500h
    var messagePVOut = "The PV of project " + "'"+ row[0] +"'" + " is supposed to be finished since " + dateStr + " , please push " + row[1] + " to get the reports and confirm that all reports are received in PV follow up google sheet.";       // Email content for PV Out
    var subjectPVEnd = row [0] + " -- Reminder for PV ending to get report from " + row [1]; // Email subject for PV end