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
Asked
Active
Viewed 559 times
1

Claude Martin
- 745
- 6
- 21
-
[StackOverflow](https://stackoverflow.com/questions/10553846/get-link-url-to-an-calendar-event-in-google-apps-script) – RemcoE33 Aug 23 '22 at 19:26
1 Answers
2
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 = "user@email.com"; // 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;
}

Fernando Lara
- 2,263
- 2
- 4
- 14
-
this seems to work and I can just get the calendarId using `getOriginalCalendarId`, but why is this so complicated and why isn't there a way to open some URL using Apps Scripts? – Claude Martin Aug 25 '22 at 22:11
-
I'm now using setLinkUrl on the cell and then just click it: `const richText = SpreadsheetApp.newRichTextValue().setText(event.getId()) .setLinkUrl(getEventUrl(event, false)) .build(); idCell.setRichTextValue(richText);` – Claude Martin Aug 25 '22 at 22:16