使用工作表資料傳送電子郵件

鑑於 - A 有一張要求報銷的員工。

要求 - 我們應該在處理完報銷時向員工傳送電子郵件

因此,表格如下:

StackOverflow 文件

傳送電子郵件的功能如下:

function getDataSheet() {

  sheet = SpreadsheetApp.getActiveSheet();

  startRow = 2;  // First row of data to process
  numRows = 100;   // Number of rows to process
  startCol = 1;  //First column of data to process
  numCols = 15;    // Number of columns to process 
  
  var dataRange = sheet.getRange(startRow, startCol, numRows, numCols);

  // Fetch values for each row in the Range.
  var data = dataRange.getValues();  

  return data;  
}    

function getMessage(name, amount) {
  return "Hello " + name + ", Your reimbursement for amount " + amount + " is processed successfully";
}

function sendEmail() {
  
  var emailSent = "Yes";
  var reimbursed = "Yes";
  var emailCol = 5;
      
  var data = getDataSheet();
  
  for (var i = 0; i < data.length; i++) {
    
    var row = data[i];
    
    var isReimbursed = row[3];
    var isEmailSent = row[4];
    var name = row[0];
    var amount = row[2];
    
    if(isReimbursed == reimbursed && isEmailSent != emailSent) {
      
      var subject = "Reimbursement details";
      var message = getMessage(name, amount);
      
      var recipientEmail = row[1];
      
      MailApp.sendEmail(recipientEmail, subject, message);
      
      //Sheet range starts from index 1 and data range starts from index 0
      sheet.getRange(1 + i, emailCol).setValue(emailSent);
    }
  }
}

StackOverflow 文件