1

I have a project to send mail to multiple users, I have written script to send multiple users an Email using App script, but to make the mail more personalized, I want to start mail as "Hi Abhishek Mehta"

Using AdminDirectory.Users.get(userKey, {fields:'name'}) I am able to fetch the name of the users, but not sure how to list them in the sheet.

In normal words, I have 50 Email ids and want to fetch full name of the Email id and want to write it in a Google sheet using Google App Script.

Request help from the community.

2 Answers 2

2

name of getUser() returns an object, you'll need to access that object to get the first and surname of the user.

The following script will get all the users in your domain (provided you have admin access) sorted by email and list them in a sheet named Users

function listAllUsers() {
    var ss = SpreadsheetApp.getActiveSheet();
    var pageToken,
    page;
    var userInfo = []
    do {
        page = AdminDirectory.Users.list({
                domain: '---------',
                orderBy: 'email',
                maxResults: 500,
                pageToken: pageToken
            });

        var users = page.users;
        if (users) {
            for (var i = 0; i < users.length; i++) {
                var user = users[i];
                try {
                    userInfo.push([user.primaryEmail, user.name.givenName, user.name.familyName]);
                } catch (e) {
                    userInfo.push([user.primaryEmail, "", ""]);
                }
            }
        } else {
            Logger.log('No users found.');
        }
        pageToken = page.nextPageToken;
    } while (pageToken);
    var sheet = ss.getSheetByName('Users')
        sheet.getRange(2, 1, userInfo.length, userInfo[0].length).setValues(userInfo);
}

To get the names of a list of users you can use the following script. It assumes that the usernames (email addresses) are listed in column A starting in row 2 and without breaks.

There are 3 values in the name object

  1. .givenName is the users first name.
  2. .familyName is the users surname
  3. .fullName is the first name and surname separated by a space

    function usersNames() {
    
    var ss = SpreadsheetApp.getActiveSheet();
    var data = ss.getRange('A2:A').getValues();
    var userNames = [];
    
    for (var i = 0; i < data.length; i++) {
        if (data[i][0]) {
            var names = AdminDirectory.Users.get(data[i][0]).name
    
                userNames.push([names.givenName, names.familyName]);
    
        } else {
            break;
        }
      }
    
     ss.getRange(2, 2, userNames.length, 2).setValues(userNames)
    
    }
    
Sign up to request clarification or add additional context in comments.

3 Comments

You need to add this var on the code: var ss = SpreadsheetApp.getActiveSpreadsheet();
I don't think you have understood the question properly, I don't want to fetch the name of all users, I want to fetch the name of specific users mentioned on the sheet, so let's say I have 1000 users I want the name of only 100 users mentioned on sheet.
Hi James, Thank you so much for your answer, meanwhile, I have also created one script, sharing here as one more option.
0

Here is the script for the same.

File id = Example in Bold Letters (https://docs.google.com/spreadsheets/d/1JeNKU366pzsdH-Pg9EsjKENLO0orqunduoOGJJMnKjM4/edit#gid=63023734)

File Name = Name of the sheet under the main spreadsheet, like sheet1 or sheet2

`function getUserName() {

      var ss = SpreadsheetApp.openById("File ID");
      var sheet = ss.getSheetByName("File Name")
      var values = sheet.getDataRange().getValues()
      var fileArray = [["User Name"]]

      for(i=1; i <values.length; i++)
      {

        var userKey = values[i][2] // '2' = Cloumn C

        try{

          var status = "No Name Found"
          var status = AdminDirectory.Users.get(userKey, {fields:'name'})
          var fullname = status.name.fullName

          Logger.log(fullname)

          if (status != "No Name Found"){

            status = fullname

          }

        }

        catch (e) {

          Logger.log(e.message)
          var status = e.message

        }

        fileArray.push([status]) 

      }

      var range = sheet.getRange(1, 4, fileArray.length, 1).setValues(fileArray)

    }`

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.