2
votes

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(), [email protected]'}; 

or

var options = {cc: 'currentemail, [email protected]'};

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

Obviously these do not work :)

Many thanks in advance

2

2 Answers

5
votes

this can be done like below :

function sendWithCc(){
   var currentemail = Session.getActiveUser().getEmail();
   var options = {};// create object
   options['cc'] = currentemail+',[email protected]';// add key & values (comma separated in a string)
   GmailApp.sendEmail('[email protected]', 'subject', 'body', options);
   // I stringified 'subject' and 'body' to make that test work without defining values for it
}
1
votes

Your examples should be modified as follows:

var options = {cc: Session.getActiveUser().getEmail()+', [email protected]'};

Or

var options = {cc: currentemail+', [email protected]'};

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()