[英]Google App Script to Group Row data in sheet based on email column
I have google sheet with several projects.我有几个项目的谷歌表。 The sheet also has names of project leaders that manage these projects.该表还包含管理这些项目的项目负责人的姓名。 One project leaders can manage up to 5 projects at a go.一位项目负责人可以在 go 上管理多达 5 个项目。 The sheet looks like this.单子看起来像这样。
I would like to group each row data by column F (email) and send one email to each project lead on the projects they are managing, say for instance Tim Curry will receive a table listing only the projects he leads.我想按 F 列(电子邮件)对每一行数据进行分组,并向他们正在管理的项目的每个项目负责人发送一个 email,例如Tim Curry将收到一个仅列出他领导的项目的表格。 My code below is not adequate to accomplish this task and I will appreciate help in modifying it to group the data and send it as HTML.我下面的代码不足以完成这项任务,我将不胜感激修改它以对数据进行分组并将其作为 HTML 发送。
function sendEmails() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 3; // First row of data to process
var numRows = 12; // Number of rows to process
var dataRange = sheet.getRange(startRow, 1, numRows, 12)
// Fetch values for each row in the Range.
var data = dataRange.getValues();
var emails = data.map(function (e) {
return e[5];
})
var uniqueEmails = [...new Set(emails)];
for (var j = 0; j < uniqueEmails.length; j++) {
var subject = "";
var body = "Please see if your project is on course, if not foward this email to PMS admin"
for (var i = 0; i < data.length; i++) {
if (data[i][5] == uniqueEmails[j]){
var row = data[i];
var projectnumber = row[0];
var startdate = row[1];
var enddate = row[2];
var projectname = row[3];
var projectlead= row[4];
var email= row[5];
var lr = sheet.getLastRow();
var tableRangeValues=sheet.getRange(2,1, lr-1,6).getDisplayValues();
var htmlTemplate = HtmlService.createTemplateFromFile("Notify")
htmlTemplate.projectnumber = projectnumber;
htmlTemplate.startdate= startdate;
htmlTemplate.enddate= enddate;
htmlTemplate.projectname= projectname;
htmlTemplate.projectlead=projectlead;
htmlTemplate.email=email;
var htmlForEmail = htmlTemplate.evaluate().getContent();
if (enddate instanceof Date && enddate.getTime()>= new Date(07/14/20).getTime()) {
var emailSent = row[6];
if (emailSent != "Y") {
subject += "Action Required - Project End";
}
}
}
}
if(subject.length > 0){
MailApp.sendEmail({
to: uniqueEmails[j],
cc: "example@gmail.com",
subject: subject,
body: body
});
}
}
}
Notify HTML table Code:通知HTML 表代码:
<thead>
<tr>
<th><?=projectnumber?></th>
<th><?=startdate?></th>
<th><?=enddate?></th>
<th><?=projectname?></th>
<th><?=projectlead?></th>
<th><?=email?></th>
</tr>
</thead>
<tbody>
<?tableRangeValues.forEach(r=>{?>
<tr>
<td><?=r[0]?></td>
<td><?=r[1]?></td>
<td><?=r[2]?></td>
<td><?=r[3]?></td>
<td><?=r[4]?></td>
<td><?=r[5]?></td>
</tr>
<?})?>
</tbody>
</table>
Sample:样本:
function sendEmails() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 3; // First row of data to process
var numRows = 12; // Number of rows to process
// var numItems = SpreadsheetApp.getActiveSheet().getRange(startRow,1 numRows, sheet.getLastColumn()).getValues();
// var numRows = numItems[0]
var dataRange = sheet.getRange(startRow, 1, numRows, 11)
// Fetch values for each row in the Range.
var data = dataRange.getValues();
var emails = data.map(function (e) {
return e[5];
})
var uniqueEmails = [...new Set(emails)];
for (var j = 0; j < uniqueEmails.length; j++) {
var subject = "";
var body = "Please see if your project is on course, if not foward this email to PMS admin"
for (var i = 0; i < data.length; i++) {
if (data[i][5] == uniqueEmails[j]){
var row = data[i];
var projectnumber = row[0];
var startdate = row[1];
var enddate = row[2];
var projectname = row[3];
var projectlead= row[4];
var email= row[5];
if (enddate instanceof Date && enddate.getTime()>= new Date(07/14/20).getTime()) {
var emailSent = row[6];
if (emailSent != "Y") {
subject += "Action Required - Project: " + projectname + " " + projectnumber + " " ;
}
}
}
}
if(subject.length > 0){
MailApp.sendEmail({
to: uniqueEmails[j],
cc: "admin@example.com",
subject: subject,
body: body
});
}
}
}
If you want to group the rows eg to pass them together to a loop in the html template, you can use the Javascript function filter() .如果要将行分组,例如将它们一起传递到 html 模板中的循环,则可以使用 Javascript function filter() 。
Sample样本
Code.gs代码.gs
var tableRangeValues;
function sendEmails() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 3; // First row of data to process
var numRows = 12; // Number of rows to process
var dataRange = sheet.getRange(startRow, 1, numRows, 12)
// Fetch values for each row in the Range.
var data = dataRange.getValues();
var emails = data.map(function (e) {
return e[5];
})
var uniqueEmails = [...new Set(emails)];
for (var j = 0; j < uniqueEmails.length; j++) {
var subject = "Action Required - Project End";
var body = "Please see if your project is on course, if not foward this email to PMS admin";
tableRangeValues = data.filter(function(row){return (row[5] == uniqueEmails[j]);});
var htmlTemplate = HtmlService.createTemplateFromFile("Notify") ;
var htmlForEmail = htmlTemplate.evaluate().getContent();
body += htmlForEmail;
if(filteredData.length > 0){
Logger.log("body: " + body);
MailApp.sendEmail({
to: uniqueEmails[j],
cc: "example@gmail.com",
subject: subject,
htmlBody: body
});
}
}
}
Notify.html通知.html
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<thead>
</thead>
<tbody>
<?tableRangeValues.forEach(r=>{?>
<tr>
<td><?=r[0]?></td>
<td><?=r[1]?></td>
<td><?=r[2]?></td>
<td><?=r[3]?></td>
<td><?=r[4]?></td>
<td><?=r[5]?></td>
</tr>
<?})?>
</tbody>
</table>
</body>
</html>
tableRangeValues
as a global variable if you want to use in the html template with scriptlets请注意,如果要在带有 scriptlet 的 html 模板中使用,则需要将tableRangeValues
声明为全局变量htmlBody
you have to specify it as such within MailApp.sendEmail()
请注意,如果您想发送带有 htmlBody 的htmlBody
,您必须在MailApp.sendEmail()
中指定它
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.