Home > Software engineering >  Google Calendar event id, not icalUID
Google Calendar event id, not icalUID

Time:01-25

I'm building a script to retrieve certain events from my calendar and send an email for each qualifying event with a hyperlink to the event. For that I need event id, not icalUID. How do I get that? Here is my code (actual IDs and names were removed):

function GetFamilyEvents () {
  //Gets all events that start/end/or span within next 30 days
  var FamilyCalendar = CalendarApp.getCalendarById("[email protected]");
  var CurrentDate = new Date(); //Gets current date
  var RangeEnd = new Date(CurrentDate.getTime()   720 * 60 * 60 * 1000); //Adds 30 days in milliseconds to the current date
  var FamilyEvents = FamilyCalendar.getEvents(CurrentDate,RangeEnd); //returns events that start, end, or encompass wihtin 30 days starting from today; Time range is Current Date to Range End
  

  for(var i = 0; i<FamilyEvents.length; i  ){
    var EventTitle = FamilyEvents[i].getTitle();
    var EventCreatedDate = FamilyEvents[i].getDateCreated();
    var EventStartDate = FamilyEvents[i].getStartTime();
    var EventEndDate = FamilyEvents[i].getEndTime();
    var EventCreator = FamilyEvents[i].getCreators(); //Gets the creator of the event to email notificaiton to
    var EventID = FamilyEvents[i].getID();
    var CalendarID = FamilyCalendar.getId();
    

    //Check if an event was created today AND does not have our names or "FYI" in its title
    if(EventCreatedDate.valueOf() <= CurrentDate.valueOf() && EventTitle.indexOf('Name1')<0 && EventTitle.indexOf('Name2')<0 && EventTitle.indexOf('Name3')<0 && EventTitle.indexOf('Name4')<0 && EventTitle.indexOf('Name5')<0 && EventTitle.indexOf('FYI')<0) 
      {
        //Creates variables for the HTML body of the email notification. The same variables are referenced in the body of the HTML template.
        var EmailMessage = HtmlService.createTemplateFromFile("EmailMessage"); //"EmailMessage" is the name of the HTML file in this script.
            EmailMessage.recepient = EventCreator;
            EmailMessage.eventTitle = EventTitle;
            EmailMessage.eventStartDate = EventStartDate;
            EmailMessage.eventEndDate = EventEndDate;
            EmailMessage.calendarID = CalendarID;
            EmailMessage.eventID = EventID;
      };

Thank you

CodePudding user response:

From For that I need event id, not icalUID., when you want to retrieve the event ID, how about the following modification?

From:

var EventID = FamilyEvents[i].getID();

To:

var EventID = FamilyEvents[i].getId().split("@")[0];
  • Id of getId() is required to be modifieid.
  • getId() returns the value like ###@google.com which is iCalUID. The event ID is retrieved from this as ###.

Reference:

CodePudding user response:

The Google Event ID can be determined if you have both the iCalUID and the corresponding CalendarID that event belongs to. And once you have the Event ID, assembling a URL for the event is a piece of cake.

Step 1
Grab the first part of the iCalUID.. up to but not including the @ sign.

Step 2
Concatenate the string from step 1 with a the CalendarID separated by a single space.

Step 3
Use the built-in Utilities class to encode the string from step 2 to a web-safe base64 string.

Step 4
Assemble your url

let str = EventID.split('@')[0].toString();
let str2 = str   ' '   CalendarID;
let eid = Utilities.base64EncodeWebSafe(str2, Utilities.Charset.UTF_8);
let url = 'https://www.google.com/calendar/event?eid='   eid;

We can simplify all that into a one liner. Add it to your for loop after the CalendarID variable declaration.

let url = 'https://www.google.com/calendar/event?eid='   Utilities.base64EncodeWebSafe(EventID.split('@')[0].toString()   ' '   CalendarID, Utilities.Charset.UTF_8); 
  •  Tags:  
  • Related