Google Apps Script: How to force subscribe Google Group Members to a Google Calendar for G Suite

force change calendar subscriptions for Google Group members in Google Apps Script

Google Apps Script, G Suite Admins, Google Calendar, Calendar API,  AdminDirectory, GroupsAp

Note! This article is for G Suite users and admins. You won’t be able to apply the same code to Consumer accounts. 

Google Groups in  G Suite can be a really convenient and clean way to share users to drive folders and file locations, email users and set up Google+ community locations. However, I have come across some issues with sharing Google Calendar to Google Groups.

When you assign a Google Group email to a Google Calendar it sends out an email inviting the users in the group to accept the calendar into their list of shared calendars. The user must then accept the invitation before the calendar is added to their Google Calendar list of shared calendars…calendar.

The users receive an email message like this:

Hello Goat_GroupAlpha,

We are writing to let you know that Billy.Goat@yagisanatode.com has given you access to view events on the Google Calendar called “Test”.

Click this link to add the calendar.

– The Google Calendar Team

The Problem

In a large organisation, not all users in a group will add the calendar using the link. This might be okay but if the calendar is important or the user, as is often the case, simply just forgot to add it, then there will be emails to you the admin in the future asking you why they don’t have the calendar that everyone else has and has now missed some important event…sigh.

Another issue is that when a new user is added to a group when the group has already been added to a calendar, then that user will not receive an email invitation to the calendar and won’t be able to see the calendar.

Almost a Solution

When users are added individually to a calendar, the calendar is added automagically to their Google Calendar profile and placed in the Other calendars section of their calendar dashboard. An accompanying slightly different email is sent:

Hello sid@yagisanatode.com,

We are writing to let you know that Bill.Goat@yagisanatode.com has given you access to view events on the Google Calendar called “Test”.

We have automatically added this calendar to your Google Calendar account. You can hide or completely remove this calendar at any time.

– The Google Calendar Team

View Your Calendar.

This is optimal. It adds the calendar for the user and the user doesn’t have to do anything.

The problem is that we don’t want to waste time adding hundreds of users to multiple calendars by hand and have to manage them when we need to add new users or remove old users when they have left a group.

The Solution

To solve this problem we use Google Apps Script to get all the emails addresses of the members in a G Suite group, compare that against all the current members in the Calendar and add anyone who is missing.

We will use two Advance Google Services:

  1. Advanced Calendar Service
  2. Admin SDK Directory Service

In your Google Apps Script Editor, you will need to go to Resources > Advanced Google Services and activate these services.

If you are not an administrator for your organisation, that’s cool too. I’ll explain how you might be able to achieve the same thing by replacing the Admin SDK Directory Service with the GroupsApp service in the bonus material at the end.

The Code

The two main external variables for this program are the:

  • calendarId
  • groupEmail

I have stored them in the main run function addCalendarToGroupMembers() but you could equally make them global variables or draw this data from something like a Google Sheet, a form or a web-app.

calendarId

As the name suggests, the calendar ID is the specific ID of the calendar. To find it, go to the calendar in your Google Calendar dashboard – in our case, this is the Test calendar – got down the left side-bar until you find one of your calendars you want to use. If you hover over it three ellipses will appear to the right of the calendar. Click on them and a window will appear. Click the option Settings and sharing.

Google Calendar Setting and Sharing option

On the left side-bar again under our Test calendar, there will be a list of options. We are going to click > Integrate calendar.  The first option here will be the Calendar Id.

Calendar Setting Integrate Calendar Calendar ID

groupEmail

Fortunately, the Group email is also the Id of your group. I’m pretty certain you can figure out how to get that one on your one.

Next, let’s dive into what the functions do:

addCalendarToGroupMembers()

As we mentioned above, this is the main run function for this code.

Once we added our external variables, our first task is to compare all the members of the calendar against all the members in our selected group.

We could simply re-add all the members of the group to the calendar, but we really don’t want to spam our users and we want to also improve our runtime.

Line 6, takes the groupEmail and calendarId variables and sends it to our getMissingEmailsFromGroup() function. It then returns a list of users in the group who have not been added to the calendar.

Line 9 then loops through all the users in the remaining userEmails.

Next, we get to work with the Advanced Calendar Service.

Let’s jump down to row 19 to see what we are using.

From the Calendar service, we are going to use the Access Control List (Acl) method. This method contains all the rules for managing the settings of the calendar. For our purpose, it also contains the names of all added members into the calendar and their associated permissions.

As you can see on line 19 we will be inserting new users into our Acl. Acl.insert take two parameters:

  1. The resources we want to add.
  2. The calendar ID.

Each Acl element contains a list of item objects. Each of these are basically the calendar rules for each member or resource. For us to add a new member to the calendar we need to add a new resource, provide it with a role, which in our case is, reader, then the scope for that resource. We want to make the user scope ‘user’ and the value of the scope will be the new member’s email.

You can see how we’ve added the new member details to lines 11-17.

We’ll encompass this in a try and catch statement just in case we get some bad emails or something.

Finally, we will use the Calendar.Calendars.get method to grab a summary (title) of the calendar. Then, we will log it out with a total count of users added to the calendar. You could change this around to an alert, toast on a Google Sheet or even an update back to a web app.

getMissingEmailsFromGroup(groupID, calendarID)

This function is called from the main addCalendarToGroupMembers() function. Its job is to compare the calendar member emails against the group emails and if there is a match, remove it from the group email list.

On lines 16 and 17 you can see that it calls two other functions to get the email lists from the calendar and group respectively.

Line 19 and 20 then begin the nested loops that go through each array of emails.

Line 22 then compares the two arrays and if there is a match, line 23 removes or splices out that email from the groupEmails array.

Finally, the function returns the remaining emails from the group to the main function addCalendarToGroupMembers().

getUserEmailsInGroup(groupID)

In this function we use the Admin SDK Google Advanced Service to grab the member emails of a group. It then returns the list of emails back to getMissingEmailsFromGroup(groupID, calendarID) function.

To do this we use the Members method of the AdminDirectory retrieving a list of members and their details. This will retrieve data like this:

All we need out of this is the user’s email address.  To access this we first need to get to the members object array. From there we can use map to grab each email address. Briefly, map allow you to neatly take specific details of each item set in a loop and even modify it before returning the details as a new array.

Finally, we return our list of member’s emails.

getUserEmailsInCalendar(calendarID)

This function is called from the getMissingEmailsFromGroup(groupID, calendarID) function. It uses the Calendar Advanced Services Acl again. It returns a list of member emails from the calendar.

Line 19 grabs the Access Control List…erh...list from the calendar. This is the list of all the rules and permissions added to the calendar.  It looks a little something like this:

As you can see all our member data is in the items array. So on line 22, we create the variable users to directly reference that array.

We can then remove one of the extraneous items that have a scope type of domain. We will do this by using filter on lines 26 and 27.

We don’t want all the other data for each member of the calendar. We only want the emails. To achieve this we then chained a map method to our filter that will iterate through each user item and only store the email (Lines 29-30).

All of the emails collected from the filter and map chain are stored in the emails variable on line 25. These are then returned back to the getMissingEmailsFromGroup(groupID, calendarID) function.

Bonus Code

While the above is really all you need to add new users to calendars, below are a few more tools for adding and removing users from calendars with Google Apps Script.

Remove All Users From a Calendar

This function removes all the users from a calendar who have the ‘reader’ role, which is essentially anyone who cannot make changes to the calendar.

Get Group Member Emails For Non-Admins

If you don’t have G Suite Admin privileges in your organisations. You might be able to use swap out the getUserEmailsInGroup(groupID) function with this one. The only provision here is that you must be a member of the group.

Force Calendar Subscriptions for Users not in a Group

This function simply forces calendar subscription for users who are not in a group. You can add multiple users and multiple calendars.

It is just like going into your calendar settings and manually adding users.

Conclusion

For me, I have found this pretty useful. It has significantly reduced complaints from users in groups who forgot to manually add a pertinent calendar ensuring they have the information that they need. It has also helped to ensure that new members of the group are added to the calendar and it will be ready for them to see.

To make things convenient, I added a bunch of buttons to a Google Sheet that lists all the groups from my domain and all the calendars I have edit access to. I can manually update a calendar with a group or set a twice-yearly time trigger to update group members on a calendar.

I hope you find the code useful for your own projects.

Looking to learn more about Google Apps Scripts in a more structured format? Udemy has some great courses that can get your from the basics to a real Google Apps Script pro!

Got a more specific problem you need help with, but don’t have the time to develop the skills? Fiverr’s your best bet to find a skilled Google Apps Script professional to solve your problem quickly and cheaply. *

 

*The above affiliate links have been carefully researched to get you to what you specifically need. If you decide to click on one of these links it will cost you just the same as going to the site. If you decide to sign up, I just get a little pocket money to help pay for the costs of running this website.

Leave a Reply