Skip to content

Commit

Permalink
feat(calendar): Fixed broken link and added support for indirect memb…
Browse files Browse the repository at this point in the history
…ers or multiple groups to Team Calendar (#390)

* Added support for also adding indirect members or multiple groups

* Fixed broken link
  • Loading branch information
lwcorp authored Apr 17, 2023
1 parent cf41613 commit 25dad78
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ align="left"
width="96px"/>
### Calendar
- [List upcoming events](calendar/quickstart)
- [Create a vacation calendar](calendar/vacationCalendar)
- [Create a vacation calendar](solutions/automations/vacation-calendar/Code.js)

<img
src="https://www.gstatic.com/images/branding/product/2x/classroom_96dp.png"
Expand Down
55 changes: 54 additions & 1 deletion solutions/automations/vacation-calendar/Code.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,12 @@ limitations under the License.
let TEAM_CALENDAR_ID = 'ENTER_TEAM_CALENDAR_ID_HERE';
// Set the email address of the Google Group that contains everyone in the team.
// Ensure the group has less than 500 members to avoid timeouts.
// Change to an array in order to add indirect members frrm multiple groups, for example:
// let GROUP_EMAIL = ['ENTER_GOOGLE_GROUP_EMAIL_HERE', 'ENTER_ANOTHER_GOOGLE_GROUP_EMAIL_HERE'];
let GROUP_EMAIL = 'ENTER_GOOGLE_GROUP_EMAIL_HERE';

let ONLY_DIRECT_MEMBERS = false;

let KEYWORDS = ['vacation', 'ooo', 'out of office', 'offline'];
let MONTHS_IN_ADVANCE = 3;

Expand Down Expand Up @@ -55,7 +59,12 @@ function sync() {
lastRun = lastRun ? new Date(lastRun) : null;

// Gets the list of users in the Google Group.
let users = GroupsApp.getGroupByEmail(GROUP_EMAIL).getUsers();
if (ONLY_DIRECT_MEMBERS)
let users = GroupsApp.getGroupByEmail(GROUP_EMAIL).getUsers();
else if (Array.isArray(GROUP_EMAIL))
let users = getUsersFromGroups(GROUP_EMAIL);
else
let users = getAllMembers(GROUP_EMAIL);

// For each user, finds events having one or more of the keywords in the event
// summary in the specified date range. Imports each of those to the team
Expand Down Expand Up @@ -177,3 +186,47 @@ function shouldImportEvent(user, keyword, event) {
function formatDateAsRFC3339(date) {
return Utilities.formatDate(date, 'UTC', 'yyyy-MM-dd\'T\'HH:mm:ssZ');
}

/**
* Get both direct and indirect members (and delete duplicates).
* @param {string} the e-mail address of the group.
* @return {object} direct and indirect members.
*/
function getAllMembers(groupEmail) {
var group = GroupsApp.getGroupByEmail(groupEmail);
var users = group.getUsers();
var childGroups = group.getGroups();
for (var i = 0; i < childGroups.length; i++) {
var childGroup = childGroups[i];
users = users.concat(getAllMembers(childGroup.getEmail()));
}
// Remove duplicate members
var uniqueUsers = [];
var userEmails = {};
for (var i = 0; i < users.length; i++) {
var user = users[i];
if (!userEmails[user.getEmail()]) {
uniqueUsers.push(user);
userEmails[user.getEmail()] = true;
}
}
return uniqueUsers;
}

/**
* Get indirect members from multiple groups (and delete duplicates).
* @param {array} the e-mail addresses of multiple groups.
* @return {object} indirect members of multiple groups.
*/
function getUsersFromGroups(groupEmails) {
let users = [];
for (let groupEmail of groupEmails) {
let groupUsers = GroupsApp.getGroupByEmail(groupEmail).getUsers();
for (let user of groupUsers) {
if (!users.some(u => u.getEmail() === user.getEmail())) {
users.push(user);
}
}
}
return users;
}

0 comments on commit 25dad78

Please sign in to comment.