Dados e lista de e-mail em duas planilhas diferentes
Tenho os dados na planilha Back - end e tenho a lista de e-mail na planilha Assinantes , mas o e-mail não é enviado para a lista, onde errei?
function sendEmail(e) {
var thisSheet = e.source.getActiveSheet();
if (thisSheet.getName() !== 'Backend' || e.range.columnStart !== 17 || e.range.rowStart == 1 || e.value !== 'LOCAL') return;
var body, headers = thisSheet.getRange(1, 1, 1, 6)
.getValues()[0],
thisRow = thisSheet.getRange(e.range.rowStart, 1, 1, 6)
.getValues()[0],
vehicle = thisRow[3],
vehicle2 = thisRow[4],
subject = "⚫ Vehicle Ready "+vehicle +" "+vehicle2
var emailList = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Subscribers").getRange("A2:A").getValues();
body = "<i>The following vehicle is ready after wash</i><br><br>",
i = 0;
while (i < 6) {
body += '<font style="font-size:14px;color:grey;font-family: Arial">'+headers[i] +'</font>'+' - <b><font style="font-size:14px;font-family: Arial">' + thisRow[i] +'</font></b><br><hr width="30%" align="left" Color="#bfbfbf" size="0.75">';
i++;
}
MailApp.sendEmail (emailList, assunto, corpo, {htmlBody: corpo, nome: "empresa"}); }entre com o código aqui
Respostas
Que tal essa modificação?
Pontos de modificação:
- Os valores recuperados por
getValues()
é uma matriz bidimensional. No seu caso,emailList
devar emailList = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Subscribers").getRange("A2:A").getValues()
é como[["email1"],["email2"],,,]
. E também, no caso do intervalo "A2: A", nenhum valor pode ser incluído. - Em
MailApp.sendEmail(recipient, subject, body, options)
,recipient
é uma string.
Por pontos acima, tal erro ocorre. Quando quiser enviar o e-mail para todos os e-mails em emailList
, é necessário usar um loop. Ou, quando quiser usar os e-mails em emailList
como cc
, não é necessário usar o loop. Portanto, aqui, gostaria de propor os 2 padrões a seguir.
Padrão 1:
Nesse padrão, todos os e-mails em emailList
são usados como recipient
.
Script modificado:
Quando seu script for modificado, modifique da seguinte maneira.
De:MailApp.sendEmail(emailList, subject, body, {htmlBody: body, name: "company"});
Para:
emailList.forEach(([email]) => {
if (email != "") {
MailApp.sendEmail(email, subject, body, {htmlBody: body, name: "company"});
}
})
Padrão 2:
Nesse padrão, todos os e-mails em emailList
são usados como cc
.
Script modificado:
Quando seu script for modificado, modifique da seguinte maneira.
De:MailApp.sendEmail(emailList, subject, body, {htmlBody: body, name: "company"});
Para:
MailApp.sendEmail("email address", subject, body, {htmlBody: body, name: "company", cc: emailList.filter(String).toString()});
ou
MailApp.sendEmail(null, subject, body, {htmlBody: body, name: "company", cc: emailList.filter(String).toString()});
- Nesse caso, quando
null
é usado comorecipient
, o e-mail não inclui o endereço de e-mail deto
.
Nota:
- Use este script modificado com a ativação do V8.
Referências:
- getValues ()
- sendEmail (destinatário, assunto, corpo, opções)