简体   繁体   English

通过电子邮件应用程序脚本在一封电子邮件中发送多个值

[英]Send multiple values in one email through the Email app script

I wanna set up a google script that will, with a press of a button, send one email to each user on the list.我想设置一个谷歌脚本,只需按一下按钮,就可以向列表中的每个用户发送一封电子邮件。 I have already set up the button, but I need help with merging all the values for one user in one mail instead of sending multiple emails with one value to the same person.我已经设置了按钮,但我需要帮助将一个用户的所有值合并到一封邮件中,而不是向同一个人发送多封具有一个值的电子邮件。

For example:例如:

表格示例

I basically need Rose to get one email which will have these items listed one below another:我基本上需要 Rose 收到一封电子邮件,其中将这些项目一个接一个地列出:

pens
printer
sketchpad
ink and toners

and so on.等等。

This is the script I have so far and there's a HTML body of the email as well:这是我到目前为止的脚本,还有电子邮件的 HTML 正文:

function myFunction() {
  var name = 0;
  var email = 1;
  var objects = 2
  var emailTemp = HtmlService.createTemplateFromFile("body")
  var ws = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet2");
  var data = ws.getRange("A2:D" + ws.getLastRow()).getValues();
  data.forEach(function(row){
    emailTemp.fn = row[name];
    emailTemp.site = row[objects];
    var htmlMessage = emailTemp.evaluate().getContent();
    GmailApp.sendEmail(row[email], "No objects ordered", "Blabla",{htmlBody: htmlMessage})

  });
  
}

A simple way to do it is through obtaining a reduced array of unique names / emails一种简单的方法是通过获取减少的唯一名称/电子邮件数组

This can be done eg with this function:这可以通过这个函数来完成:

function onlyUnique(value, index, self) {
  return self.indexOf(value) === index;
}
  • Then you can loop through all unique values然后您可以遍历所有唯一值
  • Find all the rows where they are contained查找包含它们的所有行
  • Push the corresponding objects into an array将对应的对象推入数组
  • Proceed the array in "body.html" as desired.根据需要处理"body.html"的数组。

Sample:样本:

var unique = ws.getRange("A2:A" + ws.getLastRow()).getValues().flat().filter(onlyUnique);
  unique.forEach(function(person){
    var row_objects =[];  
    var mail = "";
    data.forEach(function(row){
      if(row[name] == person){
        row_objects.push(row[objects]);
        mail = row[email];
      }
    });
    Logger.log(row_objects);
      emailTemp.fn = person;
    emailTemp.site = row_objects;
    var htmlMessage = emailTemp.evaluate().getContent();
    GmailApp.sendEmail(mail, "No objects ordered", "Blabla",{htmlBody: htmlMessage})
  })  

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

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