简体   繁体   中英

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

I tried to modify a script to my own needs but I get an error "TypeError: Function getDate not found in the object . (line 13, file "Code")".

In fact, I want to compare today's date to the date contained in row[7] and send a reminder to some people for each line of the spreadsheet containing Projects...

Here is my actual code:

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)
        }
      }
    }
  }
}

The emails are sent properly but I get problems with the date format in the message and I cannot figure out what I did wrong. Any support would be welcome!

Thanks in advance,

Edit 17/08: Here is a picture of the corresponding spreadsheet. enter image description here

Please note that row[7] this is cell value but not the object. So to compare with the current date you need var now = new Date(); and then compare it with the row[7]

It seems like the data returned from spreadsheet in row[7] is not a date type. In order to check manually, double click on the spreadsheet cell and see if a calendar pops up. If not, then the data returned from the sheet is not of date object type. To do in code, first check if the row[7] is instanceof Date . If yes, then process as is, otherwise, convert the date string returned from that cell to a date object and then process further. Part of modified code will look like this

    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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM