我有一个包含以下列的操作项Google表单:
我是GAS的新手,所以我拼凑了片段,让脚本向每个操作项的所有者发送1封电子邮件,但我想按E列(所有者)对操作项进行分组并发送一个电子邮件。我知道我需要一个嵌套循环,但我不知道如何继续。 生成的电子邮件的格式需要是A,B,D,F,G列(至少)。目前的脚本如下:
function sendEmails() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 3; // First row of data to process
//var numRows = 12; // Number of rows to process *CHANGE AS NEEDED*
//var numItems = getRowsData(sheet, OpenItems);
var numItems = SpreadsheetApp.getActiveSheet().getRange("OpenItems").getValues();
var numRows = numItems[0]
// Fetch the range of cells A2:B3
var dataRange = sheet.getRange(startRow, 1, numRows, 11) // must encompass Column K (11)
// 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 actionitemid = row[0];
var assignedon = Utilities.formatDate(new Date(row[1]), "GMT-05:00", "MM/dd/yy");
var assignedto = row[4];
var project = row[2];
var actionitem = row[3];
var duedate = Utilities.formatDate(new Date(row[5]), "GMT-05:00", "MM/dd/yy");
if (duedate == "12/31/69") { // no due date
var duedate = "TBD - please update!!" ;}
var status = row[6];
var emailAddress = row[9]; // Column J = 9 (starts at A=0
var subject = "Action Item Reminder - Project: " + project;
var sheetURL = SpreadsheetApp.getActiveSpreadsheet().getUrl();
var body = "A reminder to do the following:\n\n Project: "+project + "\n Due: "+duedate+"\nAssigned: "+assignedon+"\n Owner: "+assignedto+"\n Status: " + status + "\n\nACTION ITEM: "+actionitemid+"\n=============================\n"+actionitem+"\n=============================\n\nPlease advise if you will not meet the due date ASAP.\n\nAll action items can be found in the Action Item sheet:\n"+sheetURL+".";
var emailSent = row[11]; // Column D = 3
// if (oktosend == "Y") { // Send only for marked rows
var ok = emailAddress.length
if ( emailAddress.length > 0) {
// Send email
// MailApp.sendEmail(emailAddress, subject, body);
// MailApp.sendEmail(emailAddress, "TEST", body);
MailApp.sendEmail({
to: emailAddress,
cc: "rdeloach@rentpath.com",
subject: subject,
body: body
});
// Update EmailSent
var time = new Date();
time = Utilities.formatDate(time, "GMT-05:00", "MM/dd/yy, hh:mm");
sheet.getRange(startRow + i, 11).setValue(time); // Use column for EmailSent + 1 here getRange(startRow + i, X)
// Update oktosend to N
// sheet.getRange(startRow + i, 5).setValue('N'); // Use column for oktosend + 1 here getRange(startRow + i, X)
// Make sure the cell is updated right away in case the script is interrupted
SpreadsheetApp.flush();
} //oktosend<>N
}
}
主题:行动项目提醒 - 项目:项目X
提醒您执行以下操作:
项目:项目X
截止日期:3/1/14
已分配:02/01/14
所有者:Rick D
状态:待定行动项目:1
~~~~~~~~~~~~~~~~~~~~~~~
提供项目X上新数据库表的数据要求。 ~~~~~~~~~~~~~~~~~~~~~~~
如果您未能尽快到达截止日期,请告知。
可以在“操作项目”表中找到所有操作项: 结束输出
寻求有关在Gmail中分组和制作表格的帮助。
答案 0 :(得分:0)
您可以将表格放在邮件正文中,然后通过GmailApp服务而不是MailApp发送。
var body = '<table><tr><td>col</td><td>val</td></tr></table>';
GmailApp.sendMail('to@example.com', 'subject', '',
{ cc: 'cc@example.com',
htmlBody: body
});