Skip to content

Commit 25dad78

Browse files
authored
feat(calendar): Fixed broken link and added support for indirect members or multiple groups to Team Calendar (#390)
* Added support for also adding indirect members or multiple groups * Fixed broken link
1 parent cf41613 commit 25dad78

File tree

2 files changed

+55
-2
lines changed

2 files changed

+55
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ align="left"
2828
width="96px"/>
2929
### Calendar
3030
- [List upcoming events](calendar/quickstart)
31-
- [Create a vacation calendar](calendar/vacationCalendar)
31+
- [Create a vacation calendar](solutions/automations/vacation-calendar/Code.js)
3232

3333
<img
3434
src="https://www.gstatic.com/images/branding/product/2x/classroom_96dp.png"

solutions/automations/vacation-calendar/Code.js

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,12 @@ limitations under the License.
2222
let TEAM_CALENDAR_ID = 'ENTER_TEAM_CALENDAR_ID_HERE';
2323
// Set the email address of the Google Group that contains everyone in the team.
2424
// Ensure the group has less than 500 members to avoid timeouts.
25+
// Change to an array in order to add indirect members frrm multiple groups, for example:
26+
// let GROUP_EMAIL = ['ENTER_GOOGLE_GROUP_EMAIL_HERE', 'ENTER_ANOTHER_GOOGLE_GROUP_EMAIL_HERE'];
2527
let GROUP_EMAIL = 'ENTER_GOOGLE_GROUP_EMAIL_HERE';
2628

29+
let ONLY_DIRECT_MEMBERS = false;
30+
2731
let KEYWORDS = ['vacation', 'ooo', 'out of office', 'offline'];
2832
let MONTHS_IN_ADVANCE = 3;
2933

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

5761
// Gets the list of users in the Google Group.
58-
let users = GroupsApp.getGroupByEmail(GROUP_EMAIL).getUsers();
62+
if (ONLY_DIRECT_MEMBERS)
63+
let users = GroupsApp.getGroupByEmail(GROUP_EMAIL).getUsers();
64+
else if (Array.isArray(GROUP_EMAIL))
65+
let users = getUsersFromGroups(GROUP_EMAIL);
66+
else
67+
let users = getAllMembers(GROUP_EMAIL);
5968

6069
// For each user, finds events having one or more of the keywords in the event
6170
// summary in the specified date range. Imports each of those to the team
@@ -177,3 +186,47 @@ function shouldImportEvent(user, keyword, event) {
177186
function formatDateAsRFC3339(date) {
178187
return Utilities.formatDate(date, 'UTC', 'yyyy-MM-dd\'T\'HH:mm:ssZ');
179188
}
189+
190+
/**
191+
* Get both direct and indirect members (and delete duplicates).
192+
* @param {string} the e-mail address of the group.
193+
* @return {object} direct and indirect members.
194+
*/
195+
function getAllMembers(groupEmail) {
196+
var group = GroupsApp.getGroupByEmail(groupEmail);
197+
var users = group.getUsers();
198+
var childGroups = group.getGroups();
199+
for (var i = 0; i < childGroups.length; i++) {
200+
var childGroup = childGroups[i];
201+
users = users.concat(getAllMembers(childGroup.getEmail()));
202+
}
203+
// Remove duplicate members
204+
var uniqueUsers = [];
205+
var userEmails = {};
206+
for (var i = 0; i < users.length; i++) {
207+
var user = users[i];
208+
if (!userEmails[user.getEmail()]) {
209+
uniqueUsers.push(user);
210+
userEmails[user.getEmail()] = true;
211+
}
212+
}
213+
return uniqueUsers;
214+
}
215+
216+
/**
217+
* Get indirect members from multiple groups (and delete duplicates).
218+
* @param {array} the e-mail addresses of multiple groups.
219+
* @return {object} indirect members of multiple groups.
220+
*/
221+
function getUsersFromGroups(groupEmails) {
222+
let users = [];
223+
for (let groupEmail of groupEmails) {
224+
let groupUsers = GroupsApp.getGroupByEmail(groupEmail).getUsers();
225+
for (let user of groupUsers) {
226+
if (!users.some(u => u.getEmail() === user.getEmail())) {
227+
users.push(user);
228+
}
229+
}
230+
}
231+
return users;
232+
}

0 commit comments

Comments
 (0)