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.
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.
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.
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?
ReplyDeleteI have made the link public now.
ReplyDeleteThe link doesn't open so that I can make a copy.
ReplyDelete