簡體   English   中英

Google-app-script:使用電子表格中的單元格行[]的JavaScript電子郵件正文中的日期格式問題

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

我嘗試根據自己的需要修改腳本,但收到錯誤“ TypeError:在對象中找不到函數getDate。(第13行,文件“ Code”))。

實際上,我想將今天的日期與row [7]中包含的日期進行比較,並向包含項目的電子表格的每一行向某些人發送提醒...

這是我的實際代碼:

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]比較row[7]

從第[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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM