BazR
BazR

Reputation: 25

How to add other users to a calendar

I'm trying to add a user programatically to some Google calendars via Google Apps Script. Basically, it's for when a new teacher starts, I want to add them to the 5 calendars we have, without having to do it manually (note, these are personal accounts).

I want something like the subscribeToCalendar method but to add a user that isn't the active one, i.e. me. The script already collects their email address and I have the calendar IDs. I've looked in the Google Calendar documentation and can't find any way to do this.

I have also found this in the Google Calendar API documentation which talks about updating the role to for example, "reader", which is want I want, but I have no idea how this is implemented with Google Apps Script, as the examples on the page are Java, etc. https://developers.google.com/google-apps/calendar/v3/reference/acl/update#examples

I found this question which I think is in the right direction but seems awfully complex for just adding a user to the calendar: Create a POST body using Google Apps Script

Can anyone help? Thanks in advance. Baz

Upvotes: 1

Views: 2145

Answers (1)

user3075569
user3075569

Reputation:

You're heading in the right direction, you need to use the Advanced Calendar Service (That must be enabled before use. In the Script Editor select Resources > Advanced Google services... and then enable it in the Google Developers Console.)

Once enabled you can share the calendar using the ACL: insert, here's an example:

function myFunction() {

  var calendarIds = ['CAL_ID_1','CAL_ID_2','CAL_ID_3','CAL_ID_4','CAL_ID_5'];
  var userEmail = 'USER_EMAIL';
  var resource = {
      'scope': {
        'type': 'user',
        'value': userEmail
      },
      'role': 'reader'
    };

  for (var i = 0; i < calendarIds.length; i++) {
    Calendar.Acl.insert(resource, calendarIds[i]);
  }
}

Upvotes: 1

Related Questions