Bulk-Add Emails to Google Calendar with Apps Script
How to save time and hassle by creating Google Calendar events with multiple guests using Apps Script
Do you want to save time and hassle when inviting multiple people to your Google Calendar events? Do you wish there was a simple way to automate this process without manually entering each email address? If so, this tutorial is for you!
In this tutorial, you will learn how to use Apps Script, a coding language that lets you do more with Google applications like Drive, Docs, Sheets, Calendar, Gmail, and more. You will write a simple script that can bulk add emails to any Google Calendar event from a spreadsheet. You will also learn how to run your script from a custom menu in your spreadsheet. By the end of this tutorial, you will be able to create and share Google Calendar events with ease and efficiency.
What you will need for this project:
- Google Sheet
- Google Calendar Event ID
Step 1: Set Up Your Google Sheet
I made my sheet pretty simple. I have a column header called email and below it I have a list of emails.
Step 2: Open the Apps Script Editor
On the toolbar, you will select Extensions → Apps Script. This will open a new window in your browser. This is where you will add all the code for the project.
Step 3: Copy the Code
You can copy the code below into your Apps Script editor and click save.
/**
* This will create a custom menu in your Google Sheet everytime you open the Sheet
*/
function onOpen(){
const ui = SpreadsheetApp.getUi();
ui.createMenu('Custom Menu')
.addItem('Add People to Event','addAttendeesToEvent')
.addToUi()
}
function addAttendeesToEvent(){
const calendar = CalendarApp.getDefaultCalendar(); // Gets your default calendar from your Google Calendar
const ui = SpreadsheetApp.getUi(); // Gets access to the Spreadsheet UI to post prompts and messages
// Get the Calendar Event ID from a UI Option
const response = ui.prompt('Enter the eventID of the Calendar Event you want to add attendees to:');
const eventID = response.getResponseText(); // Gets the text from the above prompt
const event = calendar.getEventById(eventID); // Creates a Google Calendar Event Object with the ID
if(event !== null){
const guests = getGuests(); // Gets the guests from the spreadsheet
guests.forEach(guest => event.addGuest(guest)); // Adds the guests to the event
ui.alert('✅ Success',`${guests.length} ${guests.length == 1 ?'person': 'people'} ${guests.length == 1 ?'was': 'were'} added to the calendar event`,ui.ButtonSet.OK);
} else{
ui.alert('🚨 Error','Not a valid EventID',ui.ButtonSet.OK);
}
}
/**
* This will return an array of emails that are in the first column
* @returns {string[]} array of emails
*/
function getGuests(){
const activeSheet = SpreadsheetApp.getActiveSheet();
const sheetData = activeSheet.getDataRange().getValues();
sheetData.shift()
return sheetData.map(item => item[0])
}
Step 4: Run the onOpen Function
At the top of the Apps Script editor you will see a banner that looks like this:
You will want to make sure in the drop-down that it says onOpen. Now you will click “Run.”
You will get a pop-up that is going to ask for your permission to execute the script.
Step 5: Go back to the Google Sheet
You should now see a custom menu on your Google Sheet. When you click that Custom Menu you should be able to click “Add People to Event.” When you do that you will be prompted to enter the eventID, this way the code knows which event the emails should be added to.
Step 6: Emails Added
You will get a pop-up that tells you how many people were successfully added to your calendar event.
Congratulations! You have successfully completed this tutorial on how to use Apps Script to bulk-add emails to a Google Calendar event from a spreadsheet. You have learned how to write a simple script that can automate this process and save you time and hassle. You have also learned how to run your script from a custom menu in your spreadsheet.
Apps Script is a powerful and versatile coding language that lets you do more with Google applications like Drive, Docs, Sheets, Calendar, Gmail, and more. You can use it to create many custom tools and solutions for your personal and professional needs. If you enjoyed this tutorial and found it useful, please share it with your friends and colleagues who might benefit from it as well. Thank you for reading!