简体   繁体   English

如何使用javascript对来自Google工作表的电子邮件进行分组

[英]How to group emails from google sheets with javascript

Situation: This is a Leave (or time off) approval/monitoring system for a small office of around 40 people. 情况:这是一个大约40人的小型办公室的休假(或休假)批准/监控系统。 They put in for leave via google forms which is fed (eventually) into a google sheet. 他们通过谷歌表格提出请假,这些表格最终被送入谷歌表格。 Once a month, we need to send out a single email to each person who took leave containing every entry they made for the month. 每月一次,我们需要向每个请假的人发送一封电子邮件,其中包含他们为该月制作的每个条目。 I have altered the standard javascript from google that will send an email from a spreadsheet but it sends one email per row. 我更改了谷歌的标准JavaScript,它会从电子表格发送电子邮件,但每行发送一封电子邮件。 I need to send a consolidated email to each person. 我需要向每个人发送一份综合电子邮件。 Prior to running the script, the spreadsheet will only contain records with applicable dates, and will be sorted by [Email] and [Start Date] columns so there is no need to do any of that work in this script. 在运行脚本之前,电子表格将只包含具有适用日期的记录,并将按[电子邮件]和[开始日期]列进行排序,因此无需在此脚本中执行任何此类工作。

Here are the all columns in order: Timestamp, Email, Type of Leave Requested, Starting Date, Last Date, Number of Hours, Optional Note, Approval Decision, Optional Explanation. 以下是按顺序排列的所有列:时间戳,电子邮件,请求的请假类型,开始日期,上次日期,小时数,可选注释,批准决定,可选说明。

I've tried creating a 2D array but my previous attempts at such an array have only resulted in using my 100/day google transactions in a matter of seconds. 我已经尝试过创建一个2D阵列,但我以前在这样一个阵列上的尝试只能在几秒钟内使用我的100 /天谷歌交易。 :/ Help is appreciated. :/帮助表示赞赏。

function onOpen() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();

  var menu = [{
    name: "Send Email",
    functionName: "uiSendEmail"
  }];

  ss.addMenu("Send Email Test", menu);
}

function uiSendEmail() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet();
  var range = sheet.getDataRange();

  range= range.offset(1, 0, range.getNumRows()-1);
  range.getValues().forEach( function( cell, key, data ){


    var body = "Type of Leave - " + cell[2] + "\n\nStarting - " + cell[3] + "\n\nEnding - " + cell[4] + "\n\nHours - " + cell[5] + "\n\nNote Provided - " + cell[6] + "\n\n\n";
    //   "Type of Leave " + cell[2] \n\n;+ "Starting " + cell[3] \n\n + "Ending " + cell[4] \n\n + "Hours " + cell[5] \n\n + "Note provided " + cell[6] 
    GmailApp.sendEmail(cell[1], "Recent Requests for Time Off", body);
    });
  }

You could create an object, loop through the values and add them to the corresponding email. 您可以创建一个对象,遍历这些值并将它们添加到相应的电子邮件中。

function uiSendEmail() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var range = sheet.getDataRange();
var emails = {};

range= range.offset(1, 0, range.getNumRows()-1);
range.getValues().forEach( function( cell, key, data ){

    var body = "Type of Leave - " + cell[2] + "\n\nStarting - " + cell[3] + "\n\nEnding - " + cell[4] + "\n\nHours - " + cell[5] + "\n\nNote Provided - " + cell[6] + "\n\n\n";

    if(typeof emails[cell[1]] === 'undefined'){ 
      emails[cell[1]] = body;
    } else {
      emails[cell[1]] += body;
    }
   });

//Now loop through the email object and send each completed one
for(var address in emails) {
   GmailApp.sendEmail(address, "Recent Requests for Time Off", emails[address]);
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM