0

I am new to Google script and trying to learn it.

I have the following code executing in one of my google spreadsheets. I have enabled both the Admin Directory and the Admin SDK, and i also created an OnEdit trigger to authorize the script. But the Execution transcript still shows this error :

Execution failed: TypeError: Cannot call method "getEmail" of undefined. (line 10, file "Code")

 1. function onEdit(e) {
 2. var sheet = e.source.getActiveSheet().getName();
 3. if (sheet == "Sheet1") {
 4.    var sheet1 = e.source.getSheetByName('Sheet1');
 5.    var range = e.range;
 6.    var getCol = range.getColumn();
 7.    var getRow = range.getRow();
 8.
 9.    if (getCol == 1) {
10.         var user = e.Session.getActiveUser().getEmail();
11.            var name = AdminDirectory.Users.get(user).name.fullName; // familyName, fullName or givenName
12.            sheet1.getRange(getRow, 2).setValue(name);
13.        var htmlEmail = "Cell " + range.getA1Notation() +
14.            " edited by " + name;
15.        GmailApp.sendEmail("----------", "Spreadsheet Update " + new Date(), "", {
16.            htmlBody : htmlEmail
17.        });
18.     }
19.  }
20.}
viv227295
  • 387
  • 2
  • 6
  • 17
  • Without knowing google script: Your e.user is not defined. So the element throwing the onEdit is not providing it. – Christoph Sonntag Jun 08 '17 at 18:35
  • how are you testing this script ? – Serge insas Jun 08 '17 at 18:39
  • @Compufreak I tried to define it now taking hint from this link : [link]https://developers.google.com/apps-script/reference/base/user#getEmail() but still it does not work. – viv227295 Jun 08 '17 at 19:13
  • @Sergeinsas as I said in the post, the script is bound to my spreadsheet. – viv227295 Jun 08 '17 at 19:16
  • Do you get the same error or a different one if you use `Session.getActiveUser().getEmail()` ? – Christoph Sonntag Jun 08 '17 at 19:19
  • Absolutely same. – viv227295 Jun 08 '17 at 19:27
  • Sorry, it has changed now : _Execution failed: TypeError: Cannot read property "getActiveUser" from undefined. (line 10, file "Code")_ – viv227295 Jun 08 '17 at 19:34
  • you didn't answer my question, I was asking "how to you test it ?" meaning by making an edit or running it from the script editor ? just in case, have a look at this post, it will be helpful : https://stackoverflow.com/questions/16089041/how-can-i-test-a-trigger-function-in-gas/16089067#16089067 – Serge insas Jun 08 '17 at 19:58
  • Oh sorry! I test it by making an edit in column A of the spreadsheet, the script is supposed to put the user name in column B. – viv227295 Jun 08 '17 at 20:03
  • @Sergeinsas Thanx for the link but the problem is not with the onEdit(e) the problem is in the 10th line. As, the Execution transcript says : _Execution failed: TypeError: Cannot read property "getActiveUser" from undefined. (line 10, file "Code")_ – viv227295 Jun 08 '17 at 20:22
  • 1
    I'd suggest you log the content of e using Logger.log(JSON.stringify(e)) to see if this parameter is available – Serge insas Jun 08 '17 at 20:28

1 Answers1

0

Try something like this and see if you get results: https://developers.google.com/apps-script/reference/base/user#getEmail()

// Log the email address of the person running the script.
 Logger.log(Session.getActiveUser().getEmail());
OblongMedulla
  • 1,471
  • 9
  • 21
  • Perhaps: If security policies do not allow access to the user's email address, this method returns a blank string. – OblongMedulla Jun 08 '17 at 19:30
  • 1
    I am the owner and admin of my domain in G Suite.. I have enabled both the Admin Directory and Admin SDK and given all the authorizations for this script.... Actually, there seems to be some issues in the code above which i am unable to figure out, unfortunately. – viv227295 Jun 08 '17 at 19:38
  • When I put the above in I do receive my email in the logs. Do you not see your email in the logs? – OblongMedulla Jun 08 '17 at 19:42
  • 1
    Yes I do get the email. However, i want the names. – viv227295 Jun 08 '17 at 19:52
  • 1
    Have you tried to replace line 10 with: var user = Session.getActiveUser().getEmail(); – OblongMedulla Jun 08 '17 at 20:28