Create Calendar Events in Google Calendar using the Calendar API and Google Apps Script.

Aryan Irani
5 min readJul 29, 2021

--

Welcome to the Second part of the Calendar API Tutorial Series.

In this blog I am going to show you how to created calendar events from a Google Sheet using the Calendar API and Google Apps Script.

So let’s get started.

Welcome to Chartmat! Are you tired of struggling to make sense of your data in Google Sheets? Do you want to turn your sheets into powerful apps and dashboards that can help you make better decisions and drive your business forward? Look no further! With Chartmat, you can easily transform your Google Sheets into beautiful and interactive dashboards and apps. Whether you’re a small business owner, a marketer, or a data analyst, Chartmat has something for everyone. With our easy-to-use platform and extensive range of features, you’ll be able to turn your data into actionable insights in no time. So why wait? Sign up for Chartmat today and start unlocking the full potential of your data! Check out the link given below to know more about charmat!

Step 1 : Sample Google Sheet

The Google Sheet that I will be using in this blog, contains the following details . If you prefer working with the sheet click here.

  1. Name of the Event
  2. Start date/time of the Event
  3. End date/time of the Event
  4. Location of the Event
  5. Description of the Event

Step 2 : Add the Google Calendar API

  1. To add the Google Calendar API, follow these steps:
    Open the Script Editor. To open the Script Editor, follow these steps :

    (1) Click on the Tools button next to the Add-ons button.

(2) Next click on the Script Editor option. This brings up the Script Editor as
shown below.

We have reached the Script Editor.

(3) Go to the left side of the Script Editor and click on the Add a Service button.

On clicking on Add a Service, you will see the following Services you can add to your Google Apps Script Project.

Scroll down to Google Calendar API and select it.

After selecting the Google Calendar API, click on Add.

The Google Calendar API has successfully been added to the Project.

Step 3 : Write the Automation Script

We have already opened the Script Editor, it’s time to write the script.

function create_Events(){(1) var ss = SpreadsheetApp.getActiveSpreadsheet();
(2) var sheet = ss.getSheetByName("Calendar_Events");
(3) var last_row = sheet.getLastRow();
(4) var data = sheet.getRange("A1:E" + last_row).getValues();

Declaring the function :

  1. Get the Active Spreadsheet
  2. Get the sheet by name
  3. Get the Last row of the sheet
  4. Get the range followed by getting the values for the range
(1) for(var i = 0;i< data.length;i++){(2)var event = CalendarApp.getDefaultCalendar().createEvent(data[i][0],          (3) new Date(data[i][1]),(4) new Date(data[i][2]),{location: data[i][3]});
  1. First we have created a for loop in so that we can go through each event and add the details to the calendar.
  2. Next we are going to create a variable called event where we are going to create events using the CalendarApp command.

After writing the command we are going to get the default calendar by the getDefaultCalendar command and use the createEvent command to create the events in the Calendar.

Normally the format of the event looks like the above the image. First comes the title of the events, followed by the start and end time of the event and the some options such as description location and more.

In the sheet the first column has the title , so we are going use the data[i][0] to get the title for the first event. The same will be done for the start time and end time. Since this is in a for loop the i (the rows in the sheet) will automatically get incremented.

 Logger.log("Events have been added to the calendar");}

After this we close the brackets and write confirmatory message that says the Events have been added.

We are done with our code.

Step 3 : Check the Output

Our code is complete. Select the correct function (create_Events) as shown below and run the program. Or you can run the code using a trigger, macros and more. Check out the link below to know more.

On successful execution, the script will add all the events to the calendar.

Here you can see the 3 events that we created in the sheet have successfully been added to the Google Calendar.

If you click on it you can see all the details clearly.

The locations, start and end time and the title have been added successfully.

Summary

We saw how you can add events in Google Calendar using the Calendar API and Google Apps Script. To sum up :

  1. Added all the events into the sheet [Title, Start Time, End Time, Location and description of the event]
  2. Accessed the sheet by name
  3. Got the last row
  4. Got the range and the values
  5. Created a for loop to go through each event in the sheet
  6. Used the Calendar App command and created the events in the Google Calendar.

I hope you have understood how to Create Calendar Events in Google Calendar using the Google Calendar API and Google Apps Script. In the next blog I am going to show you how to Add Google meet links to Calendar Events using the Calendar API and Google Apps Script. You can send your feedback to aryanirani123@gmail.com .

--

--

Aryan Irani
Aryan Irani

Written by Aryan Irani

I write and create on the internet :)

Responses (4)