2017-04-24 79 views
1

我正嘗試每週向具有出色報告的經理髮送電子郵件。在發送提醒時,代碼應該考慮列「E」中的單元格的值。我使用的代碼粘貼在互聯網上的各種來源,它執行沒有任何錯誤,但它不會發送任何電子郵件,即使條件已滿足。我將不勝感激任何幫助。根據單元格值發送電子郵件

正在使用的數據出現在「區域管理器」表單上。

,這是我的代碼嘗試:

function sendEmails() { 

var sSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Area Managers"); 
var range = sSheet.getDataRange(); 
var column = 4; 
var cValues = Number(sSheet.getRange(2, column, sSheet.getLastRow()).getValue()); 

    var startRow = 2; 
    var numRows = sSheet.getLastRow()-1; 
    var dataRange = sSheet.getRange(startRow, 1, numRows, 2) 
    var data = dataRange.getValues(); 

    for (i in data) { 
    var row = data[i]; 

    if(row[5] >= 0) { 
    range = range.offset(1, 0, range.getNumRows()-1); 
    range.getValues().forEach(function(recipient, key, data) { 
    var body = "Good day "+ recipient[0] + " you have " + recipient[4] + " Internal Audit reports outstanding."; 
    GmailApp.sendEmail(recipient[0] +"<"+recipient[1]+">","Outstanding Internal Audit Reports", body); 
    })}}; 
} 

回答

0

你的代碼是有點亂。您聲明一些變量,你不use.Here是你的問題的根本解決辦法:

function sendEmails() { 
    var sSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Area Managers"); //get the sheet "Area Managers" 
    var data = sSheet.getRange(2, 1, sSheet.getLastRow(), sSheet.getLastColumn()).getValues(); //get the values of your table, without the header 

    for (var i = 0; i < data.length; i++) { // For each element of your tab 

    var row = data[i]; 

    if(row[5] > 0) { // If the person have got at least 1 report outstanding (column E) 

     var body = "Good day "+ row[0] + " you have " + row[5] + " Internal Audit reports outstanding."; 
     GmailApp.sendEmail(row[1],"Outstanding Internal Audit Reports", body); // Sending a mail to alert 

    } 
    } 
} 

的代碼註釋這樣你就可以瞭解整個過程

PS:你應該小心配額使用GmailApp郵件發送(see here),如果您發送的郵件過多,則可能會被阻止。

相關問題