使用工作表数据发送电子邮件
鉴于 - A 有一张要求报销的员工。
要求 - 我们应该在处理完报销时向员工发送电子邮件
因此,表格如下:
发送电子邮件的功能如下:
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);
}
}
}