0

我一直在研究如何根據Google表格中單元格的特定日期發送電子郵件。我發現這個article,這幾乎完全是我想做的事,因爲時間對我來說無關緊要。如何根據Google表格中的單元格日期發送提醒日期?

我的問題是,當我使用代碼,我得到錯誤TypeError:無法找到函數toLocaleDateString在對象中。 (第19行,文件「代碼」)。

這裏是片設置的副本,我有:https://docs.google.com/spreadsheets/d/1FlrpvLMRMuq8t6pI4inlKI2acrZJ68pyGHQ7Xtqi5AU/edit?usp=sharing

這裏是我的代碼,格式爲我的專欄等

var EMAIL_SENT = "EMAIL_SENT"; 

function sendEmails3() { 
    var today = new Date().toLocaleDateString(); // Today's date, without time 

    var sheet = SpreadsheetApp.getActiveSheet(); 
    var startRow = 2; // First row of data to process 
    var numRows = 999; // Number of rows to process 
    // Fetch the range of cells A2:B999 
    var dataRange = sheet.getRange(startRow, 1, numRows, 999) 
    // Fetch values for each row in the Range. 
    var data = dataRange.getValues(); 
    for (var i = 0; i < data.length; ++i) { 
    var row = data[i]; 
    var emailAddress = row[3]; // POC Email column D 
    var subject = row[5];  // Subject column F 
    var message = row[6]; // Message column G 
    var emailSent = row[7]; // Output the message is sent in column H 
    var reminderDate = row[2].toLocaleDateString(); // date specified in cell C 

    if (reminderDate != today)  // Skip this reminder if not for today 
     continue; 

    if (emailSent != EMAIL_SENT) { // Prevents sending duplicates 

     MailApp.sendEmail(emailAddress, subject, message); 
     sheet.getRange(startRow + i, 4).setValue(EMAIL_SENT); 
     // Make sure the cell is updated right away in case the script is interrupted 
     SpreadsheetApp.flush(); 
    } 
    } 
} 

如何解決這個問題?除了錯誤之外,我還可以通過電子郵件發送我的工作表中的兩個聯繫點嗎?

謝謝你提供的所有幫助。

+0

你有999行的數據?你可以改變這一行:var numRows = 999;到你有的行數並嘗試一下代碼?我認爲你試圖將一個空字符串轉換爲一個日期字符串,從而導致問題 –

回答

0

你得到錯誤的最可能原因是你試圖訪問一個空單元並試圖將它轉換成日期字符串。

修改這一行:

var numRows = 999; 

以下

var numRows = sheet.getLastRow()-1 // Get last row, -1 because your startrow is 2 

是有它的數據,所以你只能訪問行。 也可以發送電子郵件給多個人只是包括在這樣的逗號分隔字符串的所有電子郵件:

var emailAddress = row[3] + "," + row[4]; // POC Email column D and E (Just create a comma seprated email list to send email to multiple people) 

找到下面的修改後的代碼:

function sendEmails3() { 
    var today = new Date().toLocaleDateString(); // Today's date, without time 

    var sheet = SpreadsheetApp.getActiveSheet(); 
    var startRow = 2; // First row of data to process 
    var numRows = sheet.getLastRow() - 1; // Number of rows to process, -1 since you are starting from row 2 
    // Fetch the range of cells A2:G(LastRow -1) 
    var dataRange = sheet.getRange(startRow, 1, numRows, 8) 
    // Fetch values for each row in the Range. 
    var data = dataRange.getValues(); 
    for (var i = 0; i < data.length; ++i) { 
    var row = data[i]; 
    var emailAddress = row[3] + "," + row[4]; // POC Email column D and E (Just create a comma seprated email list to send email to multiple people) 
    var subject = row[5];  // Subject column F 
    var message = row[6]; // Message column G 
    var emailSent = row[7]; // Output the message is sent in column H 
    var reminderDate = row[2].toLocaleDateString(); // date specified in cell C 

    if (reminderDate != today)  // Skip this reminder if not for today 
     continue; 

    if (emailSent != EMAIL_SENT) { // Prevents sending duplicates 

     MailApp.sendEmail(emailAddress, subject, message); 
     sheet.getRange(startRow + i, 8).setValue(EMAIL_SENT); 
     // Make sure the cell is updated right away in case the script is interrupted 
     SpreadsheetApp.flush(); 
    } 
    } 
} 

希望幫助!

0

這條線:

var reminderDate = row[2].toLocaleDateString(); // date specified in cell C 

你應該分解成多個操作:

var cellValue,reminderDate;//Define variables at top of function 

cellValue = row[2];// date specified in cell C 

if (!cellValue) {continue;}//If there is no cell value continue looping 

if (typeof cellValue === 'object') { 
    reminderDate = cellValue.toLocaleDateString(); 
} else { 
    cellValue = new Date(cellValue);//Convert date as string to object 
    reminderDate = cellValue.toLocaleDateString(); 
} 
相關問題