Google 脚本:MailApp.sendEmail 到多个地址?

Google Script: MailApp.sendEmail to multiple addresses?

我有一个使用以下脚本的脚本:

MailApp.sendEmail(row.shiftManager, "Holiday Approval Request", "", {htmlBody: message});
  row.state = STATE_PENDING;

但是我也想将同样的邮件发送到 row.shiftSupervisor,这可能是我忽略的非常简单的事情,但我想这里的人会立即知道它是什么。

为你的帮助干杯:)

编辑 - 我尝试使用:

MailApp.sendEmail(row.shiftManager, row.shiftSupervisor, "Holiday Approval Request", "", {htmlBody: message});
      row.state = STATE_PENDING;

但没有快乐。

编辑 2 - 我使用了它:

  MailApp.sendEmail(row.shiftManager, "Holiday Approval Request", "", {htmlBody: message});
  MailApp.sendEmail(row.shiftSupervisor, "Holiday Approval Request", "", {htmlBody: message});
  row.state = STATE_PENDING;

不是最优雅的一段代码,但它完成了工作...

可以使用加号和每个电子邮件地址之间的逗号来连接(连接在一起)电子邮件地址。在 JavaScript 中,加号可用于添加或连接字符串。加号在JavaScript中既是加法运算符又是字符串运算符。字符串是文本,如果用加号连接文本字符串就是字符串公式。

一个解决方案是:

var recipient = row.shiftManager + "," + row.shiftSupervisor;
MailApp.sendEmail(recipient, "Holiday Approval Request", "", {htmlBody: message});

在上面的例子中,有4个参数。但是 MailApp.sendEmail() 有多种可能的参数结构。以下示例显示了放入对象的所有设置,其中对象中的“to”键用于收件人。

MailApp.sendEmail({
  to: recipient,
  cc: recipientsCC,
  subject: Subject,
  htmlBody: html
});

一个完整的例子:

function sendToMultiple() {
  var message = "This is a test of HTML <br><br> Line two";
  
  var recipientsTO = "example@gmail.com" + "," + "example@yahoo.com";
  var recipientsCC = "example@gmail.com";
  var Subject = "Vacation Approval Request";
  var html = message;
  
  MailApp.sendEmail({
    to: recipientsTO,
    cc: recipientsCC,
    subject: Subject,
    htmlBody: html
  });

}

带有示例的文档位于以下 link:

Google Documentation - MailApp.sendEmail

这是我的做法:

//check quota and log
const emailsLeft = MailApp.getRemainingDailyQuota();
console.log( emailsLeft + " emails left in quota");

//get list of emails from spreadsheet itself
//filter out empty rows
const emails = getTab("Config").getRange("D2:D").getValues().map(function(el){ return el[0]; }).filter(function(el){ return el != '' });  

//send emails from NO_REPLY email, subject and HTML body
MailApp.sendEmail({
  to: emails.join(","),
  subject: subject,
  htmlBody: html,
  noReply: true
});

function getTab(name) {
  var sheet = SpreadsheetApp.getActiveSpreadsheet();
  return sheet.getSheetByName(name);
}

getTab() 和其他辅助函数可以在这里找到 https://github.com/tim-kozak/google-spreadsheet-helpers