2

I have a script behind a Google spreadsheet that sends an email once certain cells of a row is completed. The script works by sending to multiple hard coded email addresses. How can I add the current user's email address as a 2nd CC? One of the CC is hard coded but the 2nd changes depending on the person updating the spreadsheet. I know how to grab the email address but how do I pass it as a variable so it actually gets CC-ed?

var currentemail = Session.getActiveUser().getEmail();

var options = {cc: 'Session.getActiveUser().getEmail(), someotheremail@domain.com'}; 

or

var options = {cc: 'currentemail, someotheremail@domain.com'};

GmailApp.sendEmail(email, subject, body, options);

Obviously these do not work :)

Many thanks in advance

JC66
  • 45
  • 1
  • 7

2 Answers2

5

this can be done like below :

function sendWithCc(){
   var currentemail = Session.getActiveUser().getEmail();
   var options = {};// create object
   options['cc'] = currentemail+',someotheremail@domain.com';// add key & values (comma separated in a string)
   GmailApp.sendEmail('somemail@domain.com', 'subject', 'body', options);
   // I stringified 'subject' and 'body' to make that test work without defining values for it
}
Serge insas
  • 45,904
  • 7
  • 105
  • 131
1

Your examples should be modified as follows:

var options = {cc: Session.getActiveUser().getEmail()+', someotheremail@domain.com'};

Or

var options = {cc: currentemail+', someotheremail@domain.com'};

You cannot call a function, or reference a variable, from within a string. Instead, use the + operator to join the value of your variable (or the return value of your function call) with the string.

Note, depending on the context in which this code will be used, the script may not have permission to access the users identity regardless. See the notes under GetActiveUser() here: https://developers.google.com/apps-script/reference/base/session#getActiveUser()

Cameron Roberts
  • 7,127
  • 1
  • 20
  • 32