Search code examples
emailgoogle-apps-scriptgoogle-sheetscarbon-copy

how to pass current user's email address to Google Script


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


Solution

  • 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
    }