Google Apps Script

Class Group

A group object whose members and those members' roles within the group can be queried. Here's an example which shows the members of a group. Before running it, replace the email address of the group with that of one on your domain.

 
function listGroupMembers() {
     var group = GroupsApp.getGroupByEmail("example@googlegroups.com");
     var s = group.getEmail() + ': ';
     var users = group.getUsers();
     for (var i = 0; i < users.length; i++) {
       var user = users[i];
       s = s + user.getEmail() + ", ";
     }
     Logger.log(s);
   }
 

Methods

MethodReturn typeBrief description
getEmail()StringGets this group's email address.
getRole(email)RoleRetrieves a user's role in the context of the group.
getRole(user)RoleRetrieves a user's role in the context of the group.
getUsers()User[]Retrieves all the direct members of the group.
hasUser(email)BooleanTests if a user is a direct member of the group.
hasUser(user)BooleanTests if a user is a direct member of the group.

Detailed documentation

getEmail()

Gets this group's email address. This example lists the email address of all the groups the user belongs to.

 
function listMyGroupEmails() {
     var groups = GroupsApp.getGroups();
     for (var i = 0; i < groups.length; i++) {
       Logger.log(groups[i].getEmail());
     }
   }
 

Return

String — the group's email address


getRole(email)

Retrieves a user's role in the context of the group. A user who is a direct member of a group has exactly one role within that group. Throws an exception if the user is not a member of the group or if you do not have permission to view the group's member list. This example lists the owners of a group:

 
var group = GroupsApp.getGroupByEmail("example@googlegroups.com");
   var users = group.getUsers();
   Logger.log('These are the group owners:');
   for (var i = 0; i < users.length; i++) {
     var user = users[i];
     if (group.getRole(user.getEmail()) == GroupsApp.Role.OWNER) {
       Logger.log(user.getEmail());
     }
   }
 

Parameters

NameTypeDescription
emailStringa user's email address

Return

Role — that user's role within the group


getRole(user)

Retrieves a user's role in the context of the group. A user who is a direct member of a group has exactly one role within that group. Throws an exception if the user is not a member of the group or if you do not have permission to view the group's member list. This example lists the owners of a group:

 
var group = GroupsApp.getGroupByEmail("example@googlegroups.com");
   var users = group.getUsers();
   Logger.log('These are the group owners:');
   for (var i = 0; i < users.length; i++) {
     var user = users[i];
     if (group.getRole(user) == GroupsApp.Role.OWNER) {
       Logger.log(user.getEmail());
     }
   }
 

Parameters

NameTypeDescription
userUserthe user whose role to retrieve

Return

Role — that user's role within the group

See also


getUsers()

Retrieves all the direct members of the group. Throws an exception if you do not have permission to view the group's member list. Note: if you are a member of a group B which is itself a member of another group A then you are indirectly subscribed to group A. Although you receive copies of messages sent to it, you are not actually subscribed to the parent group A. Here's an example which shows the members of a group. Before running it, replace the email address of the group with that of one on your domain.

 
function listGroupMembers() {
     var GROUP_EMAIL = "example@googlegroups.com";
     var group = GroupsApp.getGroupByEmail(GROUP_EMAIL);
     var users = group.getUsers();
     var s = "Group " + GROUP_EMAIL + " has " + users.length +
     " members: ";
     for (var i = 0; i < users.length; i++) {
       var user = users[i];
       s = s + user.getEmail() + ", ";
     }
     Logger.log(s);
   }
 

Return

User[] — all the direct members of the group


hasUser(email)

Tests if a user is a direct member of the group. Throws an exception if you do not have permission to view the group's member list. Here's an example which checks if the current user is a member of a group:

 
var group = GroupsApp.getGroupByEmail("example@googlegroups.com");
   var currentUser = Session.getActiveUser();
   if (group.hasUser(currentUser.getEmail())) {
     Logger.log("You are a member");
   }
 

Parameters

NameTypeDescription
emailStringa user's email address

Return

Boolean — true if that user is a member of the group


hasUser(user)

Tests if a user is a direct member of the group. Throws an exception if you do not have permission to view the group's member list. Here's an example which checks if the current user is a member of a group:

 
var group = GroupsApp.getGroupByEmail("example@googlegroups.com");
   var currentUser = Session.getActiveUser();
   if (group.hasUser(currentUser)) {
     Logger.log("You are a member");
   }
 

Parameters

NameTypeDescription
userUserthe user whose membership to test

Return

Boolean — true if that user is a member of the group

Authentication required

You need to be signed in with Google+ to do that.

Signing you in...

Google Developers needs your permission to do that.