Skip to content

Instantly share code, notes, and snippets.

@printminion
Last active September 8, 2022 00:44
Show Gist options
  • Save printminion/cc23e0ae0e6119c77ec8 to your computer and use it in GitHub Desktop.
Save printminion/cc23e0ae0e6119c77ec8 to your computer and use it in GitHub Desktop.
/**
* @desc Script for sharing Calender with domain users.
* Shared calender will be visible in users Calender interface
*
* Warning: Before running this script enable Calendar API via Advanced Google Services
* https://developers.google.com/apps-script/guides/services/advanced
* @author Misha M.-Kupriyanov https://google.com/+MishaMKupriyanov
* @require Calendar API
* @link https://gist.github.com/printminion/cc23e0ae0e6119c77ec8
*/
var calendarACLCache = {}; //global cache
function test_shareCalender() {
var calendarId = 'XXXXX@group.calendar.google.com';
var email = 'user@domain.org';
shareCalender_(calendarId, email)
}
function shareCalender_(calendarId, userId) {
var aclRule = Calendar.newAclRule();
aclRule.role = "writer";
aclRule.scope = Calendar.newAclRuleScope();
aclRule.scope.type = "user";
aclRule.scope.value = userId;
var response = Calendar.Acl.insert(aclRule, calendarId);
Logger.log(response);
}
function shareCalender_(calendarId, userId) {
if (isUserSubscribed(calendarId, userId)) {
return true;
}
var aclRule = Calendar.newAclRule();
aclRule.role = "writer";
aclRule.scope = Calendar.newAclRuleScope();
aclRule.scope.type = "user";
aclRule.scope.value = userId;
var response = Calendar.Acl.insert(aclRule, calendarId);
calendarACLCache[calendarId] = getCalenderAcl(calendarId);
Logger.log(response);
}
function getCalenderAcl(calendarId) {
var optionalArgs = {'fileds' : 'items(role,scope)'};
var response = Calendar.Acl.list(calendarId, optionalArgs);
return response;
}
function isUserSubscribed(calendarId, userId) {
var aclRules = calendarACLCache[calendarId];
//load ACL rules if empty
if (!aclRules) {
aclRules = getCalenderAcl(calendarId);
calendarACLCache[calendarId] = aclRules;
}
var aclRule = null;
for (var i in aclRules.items) {
aclRule = aclRules.items[i];
//if (aclRule['role'] == 'writer') {
//}
if (aclRule['scope']['value'] == userId) {
return true;
}
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment