1

My question is an extension of this fantastic solution but am hoping to take it one step further. Whenever a user other than me marks the checkbox as TRUE, the sender of the email is always me, since:

Installable triggers always run under the account of the person who created them

Is it possible to capture the user currently logged in and make them the sender, and if so, what am I missing in my code to make that happen?

What I've tried

I believed I had found my answer, but no such luck. This still posts the timestamp to the sheet successfully, but the email sender still shows as me. Any help would be greatly appreciated.

Edit(s):

I noticed in the above solution if (activeUser === effectiveUser) { only "worked" if typed as if (activeUser !== effectiveUser) {. In my attempts at making it work, I made that edit and forgot to revert it.

function sendEmail(e){
  var sheet = e.source.getActiveSheet();
  var cell = e.range;
  var activeUser = Session.getActiveUser().getEmail();
  var effectiveUser = Session.getEffectiveUser().getEmail();
  
  if (activeUser !== effectiveUser) {

    Logger.log(cell.getColumn());
    Logger.log(cell.isChecked());
    //Check if the checkbox in column G(index 7) was checked
    if(sheet.getName() == "actionItems" && cell.getColumn() == 7 && cell.isChecked()){

      //get current row values from column A to column F
      var values = sheet.getRange(cell.getRow(),1,1,6).getDisplayValues().flat();
      Logger.log(values);
      var transmittalNumber = values[0];
      var email = values[5];
      var query = values[1];

      //create and update the email's hmtl body
      var templ = HtmlService.createTemplateFromFile('html_template');
      templ.query = query;
      var message = templ.evaluate().getContent();

      //Send email
      MailApp.sendEmail({
        to: email,
        subject: "Oatsies Action Item: "+transmittalNumber,
        htmlBody: message
      });

      //Add timestamp at column H(index 8)
      var timeZone = "GMT-7";
      var date = Utilities.formatDate(new Date(),timeZone, "yyyy-MM-dd HH:mm");
      sheet.getRange(cell.getRow(),8).setValue(date);
    }
  }
}
Nate
  • 9
  • 2
  • Where is your function declaration? – Cooper Jan 20 '22 at 22:10
  • @Cooper I'm fairly new a coding and this was compiled from a few different answers. I'm planning to pick up a Javascript book from the library to dig into this a little more. It seems like you're referring to confusion on my end between a declaration and an expression? – Nate Jan 24 '22 at 15:29
  • @Nate the problem was that some part of the code whas put in the same place line as the code fences. I fixed that in my edit from Jan 21. – Rubén Jan 24 '22 at 15:42
  • @Rubén thanks for correcting that. I do have that in my code, but must have a mistakenly omitted it when I copied and pasted it here. Because of that, unfortunately, my problem still exists. – Nate Jan 25 '22 at 16:50
  • Frustratingly, I've still made no forward progress on this, just spinning my wheels. I was hoping @Ron M might happen to see the question since he wrote 99% of the code in the post lined above. Any help would be greatly appreciated. – Nate Feb 02 '22 at 15:28

1 Answers1

0

From the question

Is it possible to capture the user currently logged in and make them the sender?

If you are using a free Google account (usually gmail.com account) it might be possible if you use the Gmail API and set a way for active users to authorize the access to their Gmail account to send emails. Also it might be possible if you and the user are using a Google Workspace accounts and if you are able to take advantage of company-wide delegation of authority (also might be possible if you are able to configure the user's email addresses as emails aliases of your account).

Regarding the use of Session.getActiveUser().getEmail() it will return the active user email based on a complex rules i.e. your account and active user belongs to the same Google Workspace domains.

Related

Rubén
  • 34,714
  • 9
  • 70
  • 166
  • I appreciate the input, but to be clear, I was looking for specific coding help versus the more general answer of whether or not it was conceptually possible. I see now that my question was worded as the latter so I've updated it in the original post. – Nate Jan 24 '22 at 15:34
  • @Nate question updates should not invalidate an appropriate answer, if that will happens you should post a new question. – Rubén Jan 24 '22 at 15:39
  • I don't feel the edit I made constitutes a categorical change. On the contrary I feel that you misunderstood what was originally being asked, and updated it to make that more clear. I didn't go through the trouble of posting code with a full description of what I have tried previously along with the the result (problem) I was encountering all to pose the preemptive question of "theoretically would this be possible." I am seeing assistance on determining what I have incorrect in my code. – Nate Jan 24 '22 at 16:52