script le field of emails in googledocs

0

Hello, I have a spreadsheet in GoogleDocs, which has a field with one or more emails, in this case, separated by commas. Script le field and send an email to the registered recipient in the field email. However, the email is only received by the recipient if he is alone in the field. If it contains more than one email, no one receives. For example, if the record contains two emails separated by commas, as in "[email protected], [email protected]", no one receives only [email protected], so it normally receives.

    
asked by anonymous 18.05.2018 / 06:40

2 answers

0

Here is my code. I've adapted it with your solution, but it's giving an error:

function sendEmails() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var startRow = 0;  // First row of data to process
  var numRows = sheet.getLastRow();
  var lastCol = sheet.getLastColumn();
      var dataRange = sheet.getRange(2, 1, numRows-startRow,lastCol).getValues();  //Get all values except the header rows


  for (var i=0;i<numRows-startRow;i++){
     var expire = dataRange[i][8]; 

    if (expire < 10) {
      var emailAddress = dataRange[i][7];
      var subject = "Você tem um prazo de auditoria vencendo em " + dataRange[i][8] + " dias";
      var emails = emailAddress.split(",");
      var dataEnvio = Utilities.formatDate(dataRange[i][0], "GMT", "dd/MM/yyyy");
  var dataFim = Utilities.formatDate(dataRange[i][4], "GMT", "dd/MM/yyyy");
      var message = "<P><i style='color: black; font-size: 11px; font-weight:bold';> *Esta é a Proposta de encaminhamento da COAUD, de " + dataEnvio + "\n" + ", que deve ser respondida até "  
      + dataFim + "<P><a style='color: black; font-size: 12px; font-weight:bold';>Identificação do Documento: </a>" + dataRange[i][2] + "<P><b style='color: black; font-size: 12px; "
      + " font-weight:bold';> Recomendação de Auditoria: </b> " + dataRange[i][3] 
      +"<P> ----------------------------------------------------------------------------------------------------------------------------------------------------------------"
      + "------------------------------------------------</P>" 
      + "<P><i style='color: red; font-size: 10px';> NOTA: Amparada na Portaria X monitoramento. Para acessá-lo, consulte o manual de monitoramento, disponível "  
      + "<A href='https://www.a.x'>aqui</A>. Contate a COA pelos telefones: XXXXXXXX/6779 e e-mail: [email protected], para maiores informações </i></P>";
     // for (var email of emails) {
           MailApp.sendEmail(Email.trim(), subject, "", {htmlBody: message});


//  MailApp.sendEmail(Email.trim(), emailAddress, subject, "", {htmlBody: message});
   }

}

}

    
18.05.2018 / 14:43
0

Break the field value into array and call the function that sends the emails within a for...of . This way each email address will be sent separately to the role, not all together when there is more than 1 email in the field:

// quebro a variável emailAddress em array pela vírgula
var Emails = emailAddress.split(",");
for(var Email of Emails){
   // o .trim() é importante para eliminar espaços
   MailApp.sendEmail(Email.trim(), subject, "", {htmlBody: message});
}
    
18.05.2018 / 12:37