简体   繁体   English

基于 email 列在工作表中对行数据进行分组的 Google App 脚本

[英]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>

Use ... new Set() to retrieve unique emails from the sheet and concatenate the email contents for the rows that correspond to each unique email使用... new Set()从工作表中检索唯一的电子邮件并连接对应于每个唯一 email 的行的 email 内容

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
              }); 
    }
  }
}

UPDATE更新

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>
  • Note that you need to declare tableRangeValues as a global variable if you want to use in the html template with scriptlets请注意,如果要在带有 scriptlet 的 html 模板中使用,则需要将tableRangeValues声明为全局变量
  • Note that if you want to send an email with an htmlBody you have to specify it as such within MailApp.sendEmail()请注意,如果您想发送带有 htmlBody 的htmlBody ,您必须在MailApp.sendEmail()中指定它

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Firebase | 谷歌表 | 应用脚本 | 如何将列中的数据而不是行中的数据从 Firebase 保存到 Google 表格? - Firebase | Google Sheet | App Script | How do I save the data in column instead of row from Firebase to Google Sheet? Google 工作表应用程序脚本 - 根据循环条件将数据从一张工作表复制到另一张工作表 - Google sheet app script - Copy data from one sheet to another based on condition with loop Google Sheet Script - 删除包含特定数据的行 - Google Sheet Script - remove row with specific data 如何使用Google App Script设置范围直到工作表中列的最后一行 - How to set the range until last row of a column in sheet using Google App Script 使用谷歌应用脚本将谷歌工作表中的数据拆分到另一张工作表 - Split data in google sheet to another sheet using google app script 谷歌脚本将数据从工作表 1 复制到第一个空行工作表 2 - google script copy data from sheet 1 to first empty row sheet 2 尝试将工作表行数据发送到GmailApp时缺少变量名; Javascript / Google App脚本 - Missing variable name while trying to send sheet row data to GmailApp; Javascript/Google App script Google App Script:根据列中的值将行(仅行中选定的列)移动到另一个选项卡 - Google App Script: move row (only selected columns within the row) to another tab based on value in column 根据单元格值滚动一行到屏幕顶部:Google Sheet Script - Scroll a row to the top of the screen based on cell value: Google Sheet Script 谷歌应用脚​​本基于“问题文本”移动谷歌表格列 - Google App Scrip to move Google Sheet Column based on "question text"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM