Adding Google Calendar to EverLog

With the advent of 2019 I wanted to create a new daily calendar log with my EverLog application. My daily log is a place where I record notes on programming, applications, machine, and other information. Updates to applications, error reports, meeting notes, code snippets, and links to articles I would like to read later are all kept in my log.

I record events by the date with one Evernote notebook per year. I thought it would be a good exercise to import my Google Calendar events into my daily log.

UI Changes

Text boxes for a Gmail Address and for a holiday calendar were added. I have defaulted the holiday box to the English USA calendar. You can skip adding adding either of these by clearing their “Include” checkbox.

Birthday Entry

Digging in to the Google Calendar API

Google provides a nice .Net client library. In Visual Studio you can search for a new package to find it or use the Install Command:

1
PM>  Install-Package Google.Apis.Calendar.v3

At the Google developer site I started with the Dot Net quick start to enable the Google Calendar API. This involves creating a JSON credential file. The quick start instructs you to save the file in the project but it can be anywhere.

I found the quick start app lacking and I found a very nice collection of sample code by Linda Lawton. The part of the repository I used is here in Github. The only change I made was to remove the duplicate SampleHelpers from all but one file I used.

Authenticating with OAuth

In FormMain.cs the file path to the client secret JSON is set in the variable ClientSecretPath. We call to YearList.GetService with a scope of CalendarService.Scope.CalendarEventsReadonly. If we use CalendarService.Scope.Calendar would be requesting read/write/delete. It is always best to request only the permissions you need. We then call the API function OAuth2.GetCalendarService. This will show this dialog with your email and application name:

Google OAuth Dialog

Google.Apis.Calendar.v3.Data.Events

A list of Event items is returned by 2 API calls to CalEvents.list. One gets the personal calendar and the other gets one of the Google standard holiday calendars. To use this information I needed to put the information into an array of new objects with the data reformatted so I could insert the data into my existing calendar routine. First I had to understand the API Event object. The key items in the API Event are of course the Start and End EventTimeDate objects such as this birthday on my calendar:

Birthday Entry

Start.Date has the year 2017 which is the year I entered this into Google Calendar. It doesn’t really matter because all we are interested in is the June 14. This is a recurring event that occurs every year, so I just take the June 14 and combine it with the target year to create a DateTime object in my new CalendarLib object with a time of 12am so it will appear first when sorted. Also note here that Start.Date.DateTime is null. You don’t need a time component to a yearly date. Next look at the calendar entry for a symphony concert:

Symphony Concert Entry

Here the concert starts at 8pm so there is an Start.EventDateTime.DateTime object, with the date and 8pm. So all I have to do is copy this DateTime into my new CalendarLib object. I also copy the End.EventDateTime into my new object, it has the same date and 10pm.

The Summary field is the name of the event, I copy that along with the HtmlLink which allows me to create link to the calendar entry on the EverLog note. I also copy a lot of other fields that are currently unused. I then create the calendar text starting with the first day of the year. For each day I seach my collection of CalendarLib objects for a matching date and add any that are found to that day’s entry. Here is a sample of a day with a holiday, a birthday, and a party event on the same day.

Christmas Eve 2019 in EverLog

When you click on the Xmas Eve Party link a Google Calendar page is opened with a popup dialog of the event:

Christmas Eve in Google Calendar

If you would like just the EverLog end product you can find the Evernote export format file EverLog2019.enex along with the updated source code in the Github repository. This file just has the dates and US Holidays.