0

I cannot have CC in my reminder email generated from google sheets script.

Here the code:

    //seleziono il file
var file_ID = SpreadsheetApp.getActiveSpreadsheet().getId();
var file = DriveApp.getFileById(file_ID);

//creo una matrice di editors
var editors = file.getEditors();
var File_Editors = [];
for (var e = 0; e < editors.length; e++) {
File_Editors[e] = [editors[e].getEmail()];
}

//creo una matrice di visualizzatori
var viewers = file.getViewers();
var File_Viewers = [];
for (var e = 0; e < viewers.length; e++) {
File_Viewers[e] = [viewers[e].getEmail()];
}

var FileUrl = SpreadsheetApp.getActiveSpreadsheet().getUrl();
var FileName = SpreadsheetApp.getActiveSpreadsheet().getName();

var Today = Utilities.formatDate(new Date(), "UTC", "dd/MM/YYYY"); 

var EmailAddressTo = File_Editors;
var EmailAddressCC = File_Viewers;

var Subject = FileName +" UPDATE @ " + Today;
var HtmlMessage = "Hi All";

GmailApp.createDraft(EmailAddressTo, Subject,'',{htmlBody:HtmlMessage}, {cc:EmailAddressCC});

Here the return I have with last line:

Exception: The parameters (String,String,String,(class),(class)) don't match the method signature for GmailApp.createDraft. reminderWeeklyMail @ KPI_File_Scripts.gs:278

Thanks

  • Replace _GmailApp.createDraft(EmailAddressTo, Subject,'',{htmlBody:HtmlMessage}, {cc:EmailAddressCC});_ with _GmailApp.createDraft(EmailAddressTo, Subject,'',{htmlBody:HtmlMessage, cc:EmailAddressCC});_ [Class GmailApp] (https://developers.google.com/apps-script/reference/gmail/gmail-app?hl=ja#createDraft(String,String,String,Object)) – Sergey May 06 '21 at 16:14
  • I did It. Same mistake outcome: invalid email. Seems that the problem Is in the variabile EmailAddreseCc. Error Is invalid email. – Andrea Miassot May 07 '21 at 04:24

2 Answers2

1

Try this: GmailApp.createDraft(EmailAddressTo, Subject,'',{htmlBody:HtmlMessage,cc:EmailAddressCC});

reference

Try

if (EmailAddressCC && HtmlMessage && EmailAddressTo && Subject) {
  GmailApp.createDraft(EmailAddressTo, Subject, '', { htmlBody: HtmlMessage , cc: EmailAddressCC });
} else {
  throw 'Email Draft not created'
}
Cooper
  • 59,616
  • 6
  • 23
  • 54
  • I did It. Same mistake outcome: invalid email. Seems that the problem Is in the variabile EmailAddreseCc. Error Is invalid email. – Andrea Miassot May 07 '21 at 04:26
  • If the change doesn't work then single step through the program to determine what's missing and why. – Cooper May 07 '21 at 05:02
  • Hi I tried to include cc in the { } with htmlBody. I tried also with IF steatement. Error is as following Exception: Invalid email: [Ljava.lang.Object;@5ab0825a reminderWeeklyMail @ KPI_File_Scripts.gs:279. I tried also to put EmailAddressCC in the TO section and it works (so I deduct the array is not wrong). – Andrea Miassot May 07 '21 at 08:06
  • One question: if you run the script by your own does it works? – Andrea Miassot May 07 '21 at 08:28
  • Finally, I tried to write a simple email 'abc@gmail.com' instead of the variable EmailAddressCC (array of emails) and it works. So the matter is in the cc option that has some problem with arrays. At this point I see 2 solution: add EmailAddressCC not in CC but in TO or create a deployed string of the array. – Andrea Miassot May 07 '21 at 08:33
0

I solved with a trick. I did several tests. My discovery is that CC option in GmailApp is not working with array of emails. To skip the problem I created a string of emails from the array and set that as variable (string not array) now it does the job. Here the code in the updated part:

    //creo una matrice di visualizzatori
var viewers = file.getViewers();
var File_Viewers = [];
var EmailAddressCC = '';
for (var e = 0; e < viewers.length; e++) {
File_Viewers[e] = [viewers[e].getEmail()];
EmailAddressCC = EmailAddressCC + File_Viewers[e] + ',';

}

Now with same createDraft instruction works.

I think this is a bug to solve in Google Script side.

  • The real trick here is to go to the documentation and read what it says [here](https://developers.google.com/apps-script/reference/gmail/gmail-app#createDraft(String,String,String,Object)) and what it says about the cc is `a comma-separated list of email addresses to CC` so I would consider doing something like this `var EmailAddressCC = File_Viewers.join(',');` which turn's it into a comma separated string which is what it wants. The is true for recipients. But earlier in the process you should check to insure that `File_Viewer.length >0` otherwise your just wasting time. – Cooper May 07 '21 at 15:43