Class Group

  • A Group object allows querying its members and their roles within the group.

  • You can retrieve a group's email address, its direct child groups, and direct members.

  • You can check if a specific user or group is a direct member of the group.

  • You can get the role of a user or a list of users within the group.

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 emailaddress of the group with that of one on your domain.

functionlistGroupMembers(){constgroup=GroupsApp.getGroupByEmail('example@googlegroups.com');console.log(`${group.getEmail()}:`);constusers=group.getUsers();for(leti=0;i <users.length;i++){constuser=users[i];console.log(user.getEmail());}}

Methods

MethodReturn typeBrief description
getEmail()StringGets this group's email address.
getGroups()Group[]Retrieves the direct child groups of the group.
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.
getRoles(users)Role[]Retrieves users' roles in the context of the group.
getUsers()User[]Gets the direct members and banned members of the group that have a known corresponding Googleaccount.
hasGroup(group)BooleanTests if a group is a direct member of this group.
hasGroup(email)BooleanTests if a group is a direct member of this 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.

functionlistMyGroupEmails(){constgroups=GroupsApp.getGroups();for(leti=0;i <groups.length;i++){console.log(groups[i].getEmail());}}

Return

String — The group's email address.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

  • https://www.googleapis.com/auth/groups

getGroups()

Retrieves the direct child groups of the group. Throws an exception if you do not havepermission to view the group's member list.

In addition to this method, you can use theAdmin SDK Directory advanced service toretrieve group members in a domain.

functionlistGroupMembers(){constGROUP_EMAIL='example@googlegroups.com';constgroup=GroupsApp.getGroupByEmail(GROUP_EMAIL);constchildGroups=group.getGroups();console.log(`Group${GROUP_EMAIL} has${childGroups.length} groups:`);for(leti=0;i <childGroups.length;i++){constchildGroup=childGroups[i];console.log(childGroup.getEmail());}}

Return

Group[] — All the direct child groups of the group.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

  • https://www.googleapis.com/auth/groups

getRole(email)

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

This example lists the owners of a group:

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

Parameters

NameTypeDescription
emailStringA user's email address.

Return

Role — That user's role within the group.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

  • https://www.googleapis.com/auth/groups

getRole(user)

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

This example lists the owners of a group:

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

Parameters

NameTypeDescription
userUserThe user whose role to retrieve.

Return

Role — That user's role within the group.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

  • https://www.googleapis.com/auth/groups

See also


getRoles(users)

Retrieves users' roles in the context of the group. A user who is a direct member of a grouphas exactly one role within that group. Throws an exception if any user is not a member of thegroup or if you do not have permission to view the group's member list.

This example lists the owners of a group:

constgroup=GroupsApp.getGroupByEmail('example@googlegroups.com');constusers=group.getUsers();constroles=group.getRoles(users);console.log('These are the group owners:');for(leti=0;i <users.length;i++){if(roles[i]===GroupsApp.Role.OWNER){console.log(users[i].getEmail());}}

Parameters

NameTypeDescription
usersUser[]The users whose roles are requested.

Return

Role[] — The roles of those users within the group.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

  • https://www.googleapis.com/auth/groups

getUsers()

Gets the direct members and banned members of the group that have a known corresponding Googleaccount. Throws an exception if you don't have permission to view the group's member list orthe member emails.

Note: if you are a member of a group B which is itself a member of another group A then youareindirectly subscribed to group A. Although you receive copies of messages sent toit, 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 emailaddress of the group with that of one on your domain.

functionlistGroupMembers(){constGROUP_EMAIL='example@googlegroups.com';constgroup=GroupsApp.getGroupByEmail(GROUP_EMAIL);constusers=group.getUsers();console.log(`Group${GROUP_EMAIL} has${users.length} members:`);for(leti=0;i <users.length;i++){constuser=users[i];console.log(user.getEmail());}}
In addition to this method, you can use theAdmin SDK Directory advanced service toretrieve group members in a domain.

Return

User[] — All the direct members of the group.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

  • https://www.googleapis.com/auth/groups

hasGroup(group)

Tests if a group is a direct member of this group. The method does not returntrue ifthe tested group is nested more than one level below this group. Throws an exception if you donot have permission to view the group's member list.

constgroup=GroupsApp.getGroupByEmail('example@googlegroups.com');constchildGroup=GroupsApp.getGroupByEmail('childgroup@googlegroups.com');if(group.hasGroup(childGroup)){console.log('childgroup@googlegroups.com is a child group');}

Parameters

NameTypeDescription
groupGroupThe group whose membership to test.

Return

Booleantrue if that group is a child group of this group;false otherwise.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

  • https://www.googleapis.com/auth/groups

hasGroup(email)

Tests if a group is a direct member of this group. The method does not returntrue ifthe tested group is nested more than one level below this group. Throws an exception if you donot have permission to view the group's member list.

constgroup=GroupsApp.getGroupByEmail('example@googlegroups.com');if(group.hasGroup('childgroup@googlegroups.com')){console.log('childgroup@googlegroups.com is a child group');}

Parameters

NameTypeDescription
emailStringA group's email address.

Return

Booleantrue if that group is a child group of this group;false otherwise.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

  • https://www.googleapis.com/auth/groups

hasUser(email)

Tests if a user is a direct member of the group. Throws an exception if you do not havepermission to view the group's member list.

Here's an example which checks if the current user is a member of a group:

constgroup=GroupsApp.getGroupByEmail('example@googlegroups.com');constcurrentUser=Session.getActiveUser();if(group.hasUser(currentUser.getEmail())){console.log('You are a member');}

Parameters

NameTypeDescription
emailStringA user's email address.

Return

Booleantrue if that user is a member of the group;false otherwise.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

  • https://www.googleapis.com/auth/groups

hasUser(user)

Tests if a user is a direct member of the group. Throws an exception if you do not havepermission to view the group's member list.

Here's an example which checks if the current user is a member of a group:

constgroup=GroupsApp.getGroupByEmail('example@googlegroups.com');constcurrentUser=Session.getActiveUser();if(group.hasUser(currentUser)){console.log('You are a member');}

Parameters

NameTypeDescription
userUserThe user whose membership to test.

Return

Booleantrue if that user is a member of the group;false otherwise.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

  • https://www.googleapis.com/auth/groups

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-12-11 UTC.