10

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?

jankeir
  • 340
  • 4
  • 11

7 Answers7

19

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!

ošky
  • 316
  • 3
  • 4
  • 2
    This is so helpful! Thank you! The URL should probably be `https://calendar.google.com/calendar/event?eid=` now though :) – xd1936 Sep 12 '16 at 19:40
  • 2
    Tip: use [getOriginalCalendarId()](https://developers.google.com/apps-script/reference/calendar/calendar-event#getOriginalCalendarId()) to get the calendar id – Rubén Dec 07 '16 at 00:05
4

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

var calendarID = "somec@lend.ar";
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;   
Jasper Cuvelier
  • 503
  • 6
  • 14
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('=','');
justinis
  • 31
  • 2
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;
}
Dustin
  • 41
  • 4
  • 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). – Petr Vostrel Oct 13 '22 at 23:36
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

Henrique G. Abreu
  • 17,406
  • 3
  • 56
  • 65
  • This answer is obsolete. See the following [answer](http://stackoverflow.com/a/33302612/1595451) – Rubén Dec 07 '16 at 00:03
0

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

7ik7jal8upcnqu30koq3ctj4tn@google.com

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 = '7ik7jal8upcnqu30koq3ctj4tn@google.com';
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.

Murray
  • 1
  • 2
  • 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. – Petr Vostrel Oct 13 '22 at 23:31
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,'');
user16731842
  • 83
  • 1
  • 10