Google Groups and Google Apps Script

Friday, December 16, 2011 | 3:08 PM

Labels: ,

Google Groups is a great way to foster communication over email and on the web, connecting people and allowing them to participate in and read archived discussions. Today, we are introducing the Google Groups Service in Google Apps Script. Groups Service will allow a script to check if a user belongs to a certain group, or to enumerate the members of a particular group. The Google Groups Service works with groups created through the Google Groups web interface as well as groups created by enterprise customers with their own domain using the control panel and the Google Apps Provisioning API.

This opens a wide range of possibilities, such as allowing a script with Ui Services to show additional buttons to the members of a particular group - for example teachers or managers - and sending customized emails to all the members of a group.

Here are a few sample scripts to help you get started with the new API. To try out these samples, select Create > New Spreadsheet and then Tools > Script Editor from the menu. You can then copy the code into the script editor. The scripts’ output will appear back in the spreadsheet.

List Your Google Groups

The Groups Services can be used to fetch a list of the Google Groups of which you’re a member.

Below is a function which returns all the groups of which you’re a member. Copy and paste it into the script editor and run it. The editor will prompt you to grant READ access to the Google Groups Service before the script can run successfully.

If you receive a message stating that you’re not a member of any group, open up Google Groups and join any of the thousands of groups there.

function showMyGroups() {
  var groups = GroupsApp.getGroups();
  var s;
  if (groups.length > 0) {
    s = "You belong to " + groups.length + " groups: ";
    for (var i = 0; i < groups.length; i++) {
      var group = groups[i];
      if (i > 0) {
        s += ", ";
      }
      s += group.getEmail();
    }
  } else {
    s = "You are not a member of any group!";
  }
  Browser.msgBox(s);
}

Test Group Membership

Brendan plays trumpet in a band. He also runs the band’s website and updates its Google+ page. He’s created a web application with Google Apps Script and now he wants to add to it some additional features for members of the band. Being a model Google user, he’s already subscribed each band member to a Google Group. Although building a complete UI with Google Apps Script is beyond the scope of this article, Brendan could adapt the following function to help make additional features available only to members of that Google Group.

Of course, this is not just useful for tech-savvy trumpet players: schools may wish to make certain features available just to teachers or others just to students; businesses may need to offer certain functionality to people managers or simply to show on a page or in a UI operations of interest to those in a particular department. Before running this example yourself, replace test@example.com with the email address of any group of which you’re a member.

Note: the group’s member list must be visible to the user running the script. Generally, this means you must yourself be a member of a group to successfully test if another user is a member of that same group. Additionally, group owners and managers can restrict member list access to group owners and managers. For such groups, you must be an owner or manager of the group to query membership.

function testGroupMembership() {
  var groupEmail = "test@example.com";
  var group = GroupsApp.getGroupByName(groupEmail);
  if (group.hasUser(Session.getActiveUser().getEmail())) {
    Browser.msgBox("You are a member of " + groupEmail);
  } else {
    Browser.msgBox("You are not a member of " + groupEmail);
  }
}

Get a List of Group Members

Sending an email to the group’s email address forwards that message to all the members of the group. Specifically, that message is forwarded to all those members who subscribe by email. Indeed, for many users, discussion over email is the principal feature of Google Groups.

Suppose, however, that you want to send a customised message to those same people. Provided you have permission to view a group’s member list, the Google Groups Service can be used to fetch the usernames of all the members of a group. The following script demonstrates how to fetch this list and then send an email to each member.

Before running this script, consider if you actually want to send a very silly message to all the members of the group. It may be advisable just to examine how the script works!

function sendCustomizedEmail() {
  var groupEmail = "test@example.com";
  var group = GroupsApp.getGroupByEmail(groupEmail);
  var users = group.getUsers();
  for (var i = 0; i < users.length; i++) {
    var user = users[i];
    MailApp.sendEmail(user.getEmail(), "Thank you!",
        "Hello " + user.getEmail() + ", thank you for joining this group!");
  }
}

Find Group Managers

The Google Groups Service lets you query a user’s role within a group. One possible role is MANAGER (the other roles are described in detail in the Google Groups Service’s documentation): these users can perform administrative tasks on the group, such as renaming the group and accepting membership requests. Any user’s Role can be queried with the help of the Group class’ getRole() method.

This sample function may be used to fetch a list of a group’s managers. Once again, you must have access to the group’s member list for this function to run successfully:

function getGroupOwners(group) {
  var users = group.getUsers();
  var managers = [];
  for (var i = 0; i < users.length; i++) {
    var user = users[i];
    if (group.getRole(user) == GroupsApp.Role.MANAGER) {
      managers.push(user);
    }
  }
  return managers;
}

Let us know what you end up building, or if you have any questions about this new functionality, by posting in the Apps Script forum.

Trevor Johnston  

Trevor is a software engineer at Google. Before joining the Google Apps Script team in New York, he developed travel products in Switzerland and supported Google’s help centers in Dublin. Prior to joining Google, he worked on Lotus branded products at IBM’s Dublin Software Lab.

10 comments:

Shahana said...

Good example but little tough for general people.

Alberto Bartoli said...

Any plan to allow a manager to add/invite group members ?

I realize there are many configuration possibilities for the membership status, but supporting even just a few of them would be VERY useful.

SHT said...

Great example!

What about a script to add users to a particular goup? I have form that users complete to join a group. However, I have to manually add each user after they complete the form.

Thanks for work!

Ron said...

I need to generate a simple report of all groups in my domain. Is there a way to do that?

Also, I second Alberto's suggestion.

Saurabh Gupta said...

Thanks for the suggestion. If you can post these as feature requests on our tracker when we put it up for vote.

Ron said...

We can post feature requests in on tracker? If so, how?

Ron said...

I still need the ability to produce administrative reports on groups/members.

Jan Zítko said...

Hi Trevor ,

thank you for your article ( Google Groups and Google Apps Script ) . I have one question if I can :)

It is possible delete users from Google Apps across the APi ? I found some manual functions for APi https://developers.google.com/google-apps/groups-settings/v1/reference/groups?hl=cs , but functions for remove users from Google Groups missing :( .

We need create console for manage groups, because we have for example 500 groups and when someone is member for example 5 groups. We need him remove from all groups at one time or from some.

So our request for the Google groups is create consol and then find user automaticly in all groups where is the member and then choose if delete from all groups or for example only from three groups. As needed.

If it possible to add function for add members to google groups it will be fantastic.

I hope you understand me :) . Have you some idea or experiences with this? thank you
Jan Zitko

George said...

This blog entry seems to be out-of-date. Might be worth putting a warning up to that effect. Users (like me) may come to it, without realising it's no longer valid.

There's a dead link: Provisioning API points to:
https://code.google.com/googleapps/domain/gdata_provisioning_api_v2.0_developers_protocol.html
Which no longer exists.

When I ran the script testGroupMembership, I got the error "TypeError: Cannot find function getGroupByName in object GroupsApp. (line 21, file "Code")"

It looks like the method getGroupByName has been replaced by getGroupByEmail (and works if you do replace it).

There's no reference to getGroupByName in the documentation below:
https://developers.google.com/apps-script/reference/groups/groups-app

Karunaker Reddy said...

Can anybody help me out to find API for posting to google groups from my web application.