Skip to main content

Add / Delete Calendar Events with Google Apps Script

Let's take a look at how we can add and delete calendar events using Google Apps Script. Follow the previous tutorial on "Writing Google Apps Script" if you do not have the basics.

You can make a copy of the example sheet I will be using from this link thanks to +Melina Mattos. The spreadsheet we will be using has 2 main sheets; Calendar of Activities and Variables

The "Calendar of Activities" sheet contains the information required to create an event in our calendar as well as the emails of the guests we want to invite to our events. 


On sheet “Calendar of Activities”, add date (column A), beginning time (column B), end time (column C), events (columns E onwards), Guests email addresses (E3 onwards). You can also add names (optional - E2 onwards). In the example sheet, I have included only two guests, if you want to add more, make sure you update the "Cell range of Guests" row in the "Variables" sheet. 

On sheet "Variables" go ahead and configure parameters such as the year of event, timezone, whether to send email invitation or not (this takes either true or false), cell range of guests, and the name for the sheet which contains our event information (in this case "Calendar of Activities").

Now that you are done going through basic stuff; the spreadsheet and the sheets, lets take a look at the script which will really make this work. From the spreadsheet go to Tools -> Script Editor. Because I have already attached a script to it, the default window which opens up for you to create a new script won't open but rather the already existing script will open in the script editor. 

Below is the code from the script editor. 

The code has 3 main functions created in it. We have:

addToCalendar

This function can be found from line 11 - 46. It's main purpose is to add all the various events you added in the Calendar of Activities sheet starting from row 4. It will push values it gets from the Calendar of Activities sheet into the variable GUEST. 

It will then proceed to call the deleteEventsCalandar function, before iterating through the various rows and columns to create the calendar entries and invites. Note that the function cal.createEvent will create the calendar entry in your personal calendar of the account you have logged in with and running the script from before sending the invites to your guests.  

cal.createEvent is a built in function from Class Calendar, in fact this is what does most of the job here creating the calendar entry. The class allows you to manipulate calendar from creating an all day event to unsubscribing from an event. Read more about it here

deleteEventsCalendar


This function will delete entries based on the startDateAndTime and endDateAndTime set in the Calendar of Activities. It starts from line 49 - 65. 

addMenuItemAddEventsToCalendar


This adds the menus; Add Events, Delete Events, and Execute in order to manually run script from menu (and not from source). It starts from line 67 - 71.

To run this script, go to the Run menu of your script editor and run the addMenuItemAddEventsToCalendar function. From your spreadsheet, you should now see the Execute menu which has been added right after the Help menu. You can now go ahead and select Add Events which will try adding new events from the information you have provided or Delete Events which will delete events from your calendar at the times you have provided in the Calendar of Activities sheet. 

Comments

  1. Not able to make a copy of the google spreadsheet per permissions. Can you please look into this for me so I can make a copy?

    ReplyDelete
  2. I have made the link public now.

    ReplyDelete
  3. The link doesn't open so that I can make a copy.

    ReplyDelete

Post a Comment

Popular posts from this blog

EC isn't prepared to take technology to the next level in Elections

In Election 2012, Ghana's EC introduces a new process to verify voters before they are allowed to cast their vote. This was in the form of biometric verification, a good idea but poorly not so well implemented.

How does tonaton make money?

Ever wondered how the Swedish company Tonaton makes money? Typical of most classifieds they make or intend making money from premium promotions and advertisements. With extra paid options, posters who want to stand out and reach more interested buyers quickly and easily can have their ad At the top of every ad listing page, where they have up to 2 spots reserved for Top Ads. They currently provide two options for promotions via Airtel, Tigo Cash and Vouchers; 1. Top Ad   This promotion gives your ad a chance to be displayed as a Top Ad over the course of 7 days. 2.  Daily Bump .   This promotion will move your ad up to the top of the listings once every day for 7 days. Meaning your Ad will be on top of the listings 7 times!

Be a part of the experience, and make sure your Polling Station is represented on Google Maps!

Excited to see the number of polling stations that have been mapped by Ghanaian voters. It is validation of the belief in the internet’s role in helping to ensure that the 2012 elections will be the best ever held in Ghana.