Search code examples
google-apps-scriptgoogle-calendar-api

How do I construct a link to a Google Calendar event?


If I have the event ID of a Google Calendar event, for example:

[email protected]

How can I construct a URL to view the event details in the Google Calendar interface?

For example, if I go to a google calendar event, I see a URL such as this:

https://calendar.google.com/calendar/r/eventedit/NGh0Z3BtbTFobWFrNzQ0cjBrYmtkY29kYXIgZXVndTlrYW4xZGRpMXBtaTZzazNpYjWoNmdAZw

What function/algorithm can I run to go from [email protected] to NGh0Z3BtbTFobWFrNzQ0cjBrYmtkY29kYXIgZXVndTlrYW4xZGRpMXBtaTZzazNpYjWoNmdAZw?

Bonus: A bonus would be if there was also a shortcut method that worked on a CalendarEvent via Google Apps Script. For example, if there was a method such as getUrl() just like there is a getId() method.


Solution

  • Since the same event can exist in multiple calendars (e.g. by inviting others to it), the event id is not sufficient information.

    To construct a URL you need both the event id (refresher for how find it), AND the Calendar ID (see below for instructions as to how to find it). You'll then need to encode both as base64, and finally you'll you have a valid Google Calendar event link.

    For the following instructions, imagine we have an event, Event1, and it exists in both calendars, CalendarA and CalendarB.

    In the Google Calendar interface, you would be able to view any of these combinations:

    • Event1 in CalendarA
    • Event1 in CalendarB

    Let's assign the following IDs:

    To construct an event URL:

    1. Get the event-id, which is the event id without the @google.com

      E.g. for Event1 we have 4htgpmm1hmak744r0kbkdcodar

    2. Get the calendar-id, which is the calendar id with the @group.calendar.google.com replaced with @g

      E.g. for CalendarA we have eugu9kan1ddi1pmi6sk3ib56g@g

    3. Join the two values with a space in the middle.

      <event-id> <calendar-id>
      

      E.g. for Event1 + CalendarA, we have:

      4htgpmm1hmak744r0kbkdcodar eugu9kan1ddi1pmi6sk3ib56g@g
      
    4. Encode the value as base64 (e.g. on https://www.base64decode.org):

      NGh0Z3BtbTFobWFrNzQ0cjBrYmtkY29kYXIgZXVndTlrYW4xZGRpMXBtaTZzazNpYjWoNmdAZw==
      
    5. Remove the training ==:

      NGh0Z3BtbTFobWFrNzQ0cjBrYmtkY29kYXIgZXVndTlrYW4xZGRpMXBtaTZzazNpYjWoNmdAZw
      
    6. You can now append this value to https://www.google.com/calendar/event?eid= (view event page) or https://calendar.google.com/calendar/r/eventedit/ (edit event page):

      https://www.google.com/calendar/event?eid=NGh0Z3BtbTFobWFrNzQ0cjBrYmtkY29kYXIgZXVndTlrYW4xZGRpMXBtaTZzazNpYjWoNmdAZw
      
      or
      
      https://calendar.google.com/calendar/r/eventedit/NGh0Z3BtbTFobWFrNzQ0cjBrYmtkY29kYXIgZXVndTlrYW4xZGRpMXBtaTZzazNpYjWoNmdAZw
      
    7. You're done! That URL will take you to the calendar event in the Google interface.


    How to find the calendar ID

    If you need instructions for how to find the calendar id...

    To find the calendar ID:

    1. Go to the calendar settings
    2. Scroll down until you find the calendar id field.

      This might be a value that ends with @group.calendar.google.com, your email address, or some other value.

    If you are having trouble finding the calendar id, another tool that might help is adding the eventdeb=1 parameter to the URL, going to the Troubleshooting info of an event, and then looking for either the organizer or participant values, both of which contain calendar ids.


    In Apps Script

    var eventUrl = "https://www.google.com/calendar/event?eid=" +
                      Utilities.base64Encode(event.getId().split('@')[0] +
                      " " + 
                      event.getOriginalCalendarId())
                      .replace(/\=/g, '');