How to extract a username from an email address using Google App Script

Advertisements

I’m using the following script to get the user’s email address:

  function getEmail() {
  var userEmail = Session.getActiveUser().getEmail()
  Logger.log(userEmail);
  }

It logs something like this: username@domain.com

I’d like to extract the username (everything to the left of @) and log it.

>Solution :

About It logs something like this: username@domain.com, when your showing script is run, you can see username@domain.com in the log. And, you want to retrieve username from username@domain.com, how about the following modification?

Modified script:

function getEmail() {
  var userName = Session.getActiveUser().getEmail().split("@")[0];
  Logger.log(userName);
}

Reference:

Leave a ReplyCancel reply