Claude Martin
Claude Martin

Reputation: 765

get URL to Google calendar event from its ID

I have a CalendarEvent object and I can get the unique ID. But how can I get the URL to that event so I can open it in a new tab? I store the ID in a spreadsheet and want an easy way to open that event in the calendar. Creating the event is easy but the ID is not really useful as it looks like a email address and Sheets would want to send an email when I click on it. I need a proper HTTPS-URL for the event or a way to open a new tab using Apps Script or show the event directly inside the Sheet.
I can't find anything in the API reference:
https://developers.google.com/apps-script/reference/calendar/calendar-event

Upvotes: 1

Views: 1325

Answers (1)

Fernando Lara
Fernando Lara

Reputation: 2416

I am writing this answer as a community wiki since the question was resolved in a different post, but there are many answers in the old post that do not work anymore.

Try the following code:

function myFunction() {
  var calendarID = "[email protected]"; // Change this to your desired calendar ID
  var event = CalendarApp.getCalendarById(calendarID).createEvent('Testing event', new Date('August 23, 2022 20:00:00 UTC'), new Date('August 23, 2022 21:00:00 UTC')); //This creates a testing event for the example

  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;
}

Upvotes: 3

Related Questions