11

I have a Calendar Event in Google Apps Script and I want to allow the user to open it by clicking an Anchor. I think I can the URL has to look like this: http://www.google.com/calendar/event?eid=SOMEID&ctz=Etc/GMT . However I can't seem to get the required ID from the Calendar Event. The ID I get from Event.getId() doesn't work in those URL's and there is no event.getUrl().

Does anyone know if this is possible with apps script?

7 Answers 7

21

For given event object of type CalendarEvent and given calendarId, the simplest and working way to build an URL to view/edit corresponding event in Google Calendar App is the following:

var splitEventId = event.getId().split('@');
var eventURL = "https://www.google.com/calendar/event?eid=" + Utilities.base64Encode(splitEventId[0] + " " + calendarId);

The best thing is that no Google API invocation, authentication, ... needed!

Sign up to request clarification or add additional context in comments.

2 Comments

This is so helpful! Thank you! The URL should probably be https://calendar.google.com/calendar/event?eid= now though :)
Tip: use getOriginalCalendarId() to get the calendar id
5

Allright... In 2020 this is the working version of this in case somebody is still struggling with this...

var calendarID = "[email protected]";
var event = CalendarApp.getCalendarById(calendarID).createEvent(/*SOME OPTIONS HERE*/);

var splitEventId = event.getId().split('@');

// Open the "edit event" dialog in Calendar using this URL:
var event_EDIT_URL = "https://calendar.google.com/calendar/r/eventedit/" + Utilities.base64Encode(splitEventId[0] + " " + calendarID).replace("==",'');

// Open the "view this event in a Calendar" using this URL:
var event_VIEW_IN_CAL_URL = "https://www.google.com/calendar/event?eid=" + Utilities.base64Encode(splitEventId[0] + " " + calendarID).replace("==",'');


return event_EDIT_URL; // OR return event_VIEW_IN_CAL_URL;   

Comments

3

The new version of Google Calendar has broken this. Here's how to fix it:

var mycal = 'username@m'
var splitEventId = event.getId().split('@');
var eventUrl = "https://www.google.com/calendar/event?eid=" + 
Utilities.base64Encode(splitEventId[0] + " " + mycal).toString().replace('=','');

Comments

1

Here is the es6 version of this, as long as you have the v8 engine enabled in settings.

/**
 * Generate calendar event url with Apps Script, given a CalendarEvent
 * @param {CalendarEvent}
 */


function getEventUrl(calendarEvent) {
  const calendarId = calendarEvent.getOriginalCalendarId();
  const eventId = calendarEvent.getId();
  const splitEventId = eventId.split('@')[0];
  const eid = Utilities.base64Encode(`${splitEventId} ${calendarId}`);
  const eventUrl = `https://www.google.com/calendar/event?eid=${eid}`;

  return eventUrl;
}

1 Comment

I'd only suggest an edit to add \@\.test(eventId) check condition to not produce 404 URLs for events created by non-Google calendar apps (MS Outlook, Apple Calendar, and possibly others).
0

It is indeed not possible. There's an enhancement request on apps script issue tracker that mentions this misfeature. You may want to start it to keep track of updates and kind of vote for it.

Issue 627: GAS for Appointments

1 Comment

This answer is obsolete. See the following answer
0

When an event is created for a Google calendar using the Google calendar web interface an eventId looks something like:

[email protected]

and it is possible to get a url for the event with the following:

var calendarId = 'domain.ac.uk_6l495bdvjmo1felggasrvclkc4@group.calendar.google.com';
var eventId = '[email protected]';
var splitEventId = eventId.split('@');
var eventURL = 'https://calendar.google.com/calendar/r/eventedit/' + Utilities.base64Encode(splitEventId[0] + ' ' + calendarId);

//Or for a recurring event
var event = CalendarApp.getCalendarById(calendarId).getEventById(eventId);
var moment = Moment.moment;
var startTime = moment(event.getStartTime()).utc().format('YMMDDTHHmmss')+'Z';
var eventURL = 'https://calendar.google.com/calendar/r/eventedit/' + Utilities.base64Encode(splitEventId[0] + '_' +startTime+ ' ' + calendarId);

Unfortunately the same method doesn't work for events in the same calendar created within Outlook desktop, which instead produces an eventId like this:

040000008200E00074C5B7101A82E00800000000D0E138AFF4C6D501000000000000000010000000C70DD9E87FE9EB46BCE437A98E9CC5BF

So the solution is incomplete but many less people use Outlook desktop these days.

1 Comment

For the record, OSX Calendar app also creates different IDs (XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX in my OSX version) and none of the answers applies as well.
0

Use this code to get the id

var ati = cal.getId().indexOf("@");
var splitEventId = event.getId().split('@');
var eventUrl = "https://www.google.com/calendar/event?eid=" + Utilities.base64Encode(splitEventId[0] + " " + cal.getId().substring(0,ati+2)).replace(/=/gi,'');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.