【问题标题】:Google-app-script: Date formating issue in a javascript email body using a cell row[] in SpreadsheetGoogle-app-script:在电子表格中使用单元格行 [] 的 javascript 电子邮件正文中的日期格式问题
【发布时间】:2016-08-16 13:53:39
【问题描述】:

我尝试根据自己的需要修改脚本,但出现错误“TypeError: Function getDate not found in the object . (line 13, file "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: 这是相应电子表格的图片。 enter image description here

【问题讨论】:

    标签: javascript date google-apps-script google-sheets date-formatting


    【解决方案1】:

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

    【讨论】:

    • 谢谢尤金,我的意思是,如果我只在正文邮件消息中使用«row[7]»表达式,我会在邮件中得到一个«完整日期+小时»,而我想要它就像 dd/mm/yyyy 格式一样。就像 row[7] 的包含充当字符串而不是日期包含。我弄错了吗?
    【解决方案2】:

    似乎从第 [7] 行中的电子表格返回的数据不是日期类型。为了手动检查,双击电子表格单元格并查看是否弹出日历。如果不是,则从工作表返回的数据不是日期对象类型。 要在代码中执行,首先检查 row[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
    

    【讨论】:

    • 嗨 Waqar,感谢您的回复,这是我一开始的想法,但实际上我通过双击行 [7] 得到了一个日历,它是一个日期对象类型。 .
    • 我试图重现这个但不能。每当单元格中的数据(双击显示日历)总是将数据返回为 Date 对象和 getDate(),getMonth() 等方法在该对象上运行良好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-19
    • 1970-01-01
    相关资源
    最近更新 更多