Reputation: 35
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
Upvotes: 2
Views: 2042
Reputation: 201613
From For that I need event id, not icalUID.
, when you want to retrieve the event ID, how about the following modification?
var EventID = FamilyEvents[i].getID();
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 ###
.From your replying,
what I meant was that my original code and your modifications give the same result: 2C18CFDB-E6B6-4653-AB65-21C990969103 - this is what I get for both options.
In this case, how about using Calendar API? By this, both the event ID and also the hyperlink of the event can be retrieved. When this is reflected in your script, it becomes as follows.
Before you use this, please enable Calendar API at Advanced Google services. And, please set your calendar ID.
function sample() {
var calendarId = "[email protected]"; // Please set your calendar ID
//Gets all events that start/end/or span within next 30 days
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 items = Calendar.Events.list(calendarId, { timeMin: CurrentDate.toISOString(), timeMax: RangeEnd.toISOString(), maxResults: 2500 }).items;
items.forEach(e => {
var EventTitle = e.summary; // FamilyEvents[i].getTitle();
var EventCreatedDate = new Date(e.created); // FamilyEvents[i].getDateCreated();
var EventStartDate = new Date(e.start.dateTime || e.start.date);// FamilyEvents[i].getStartTime();
var EventEndDate = new Date(e.end.dateTime || e.end.date); // FamilyEvents[i].getEndTime();
var EventCreator = e.creator.email; // FamilyEvents[i].getCreators(); //Gets the creator of the event to email notificaiton to
var EventID = e.id; // FamilyEvents[i].getID();
var CalendarID = calendarId; // FamilyCalendar.getId();
var eventUrl = e.htmlLink; // If you want to retrieve the hyperlink of the event, you can use this.
//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;
// do something
};
});
}
eventUrl
is the hyperlink of the event.Upvotes: 1
Reputation: 1
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);
Upvotes: 0