0

I am trying to pull an active user's email when they check a box. I wish to paste the active user's email in a separate column (adjacent currently, but the ability to be flexible would be nice). I have done this before in a different worksheet, but for some reason I cannot get it to work on this one. I can get it to paste a simple variable, or even the edit date, so I am thinking there must be something erroring out with the user specifically. I am on a work owned domain with company addresses, but so is the other sheet that is working. Anyway, here is what I have currently (amature, be gentle):

function onEditAdded(e) {
  var activeSheet = e.source.getActiveSheet();
  if (activeSheet.getName() == "New Hires") {
    var aCell = e.source.getActiveCell(), col = aCell.getColumn();
    if (col == 20) {
      var dateCell = aCell.offset(0,1);
      if (aCell.getValue() === true) {
        var email = Session.getActiveUser().getEmail();
          Logger.log(email);
        dateCell.setValue(email);
      } else {
        dateCell.setValue("");
      }
    }
  }
}
Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
  • aCell.getValue() === true are sure that getValue is returning a boolean value and not a string of "true"? Also I find it helpful when debugging to insert console.log("here") lines in the code so that you can see if the code is hitting a certain line. I think aCell.getValue() === true is always false. But I am not sure. – Nick Cordova Dec 22 '20 at 23:25

1 Answers1

0

I test your code and it should run without issues. You just need to add it as an Installable Triggers

Manually create installable trigger:

  • Open your Apps Script project.
  • click Triggers alarm.
  • At the bottom right, click Add Trigger.
  • Select and configure the type of trigger you want to create.
  • Click Save.

enter image description here

function onEditAdded(e) {
  var activeSheet = e.source.getActiveSheet();
  if (activeSheet.getName() == "New Hires") {
    var aCell = e.source.getActiveCell(), col = aCell.getColumn();
    if (col == 1) {
      var dateCell = aCell.offset(0,1);
      if (aCell.getValue() === true) {
        var email = Session.getActiveUser().getEmail();
          Logger.log(email);
        dateCell.setValue('user@example.com');
      } else {
        dateCell.setValue("");
      }
    }
  }
}

Output:

enter image description here

You can also explore on how to create installable triggers programmatically, so that you wont need to add it manually every time you copy this script to a different workbook.

References:

https://developers.google.com/apps-script/guides/triggers/installable#managing_triggers_programmatically

Is there any way to automatically create installable triggers?

Ron M
  • 5,791
  • 1
  • 4
  • 16