How I Used Apps Script to Never Forget My Gmail Vacation Responder Again
As a Developer Advocate for Google Workspace, I live and breathe productivity tools. But even the most tech-savvy among us can have frustratingly simple oversights. My recurring pain point? Forgetting to turn on my Gmail vacation responder to automatically notify people of my absence before heading out of office. 🤦🏽♀️
It was a classic case of “out of sight, out of mind.” Once my calendar event for time off hit, I was already mentally checked out. Very often, I would remember during my day off that I had forgotten to set the vacation responder. That always annoyed me. 🤯
Enough was enough. I decided to leverage the power of Google Apps Script to create a seamless automation that would save my sanity.
The Problem: A Disconnect Between Calendar and Gmail
When I request days off in our internal HR system it adds an out of office event to my Google Calendar. That’s a great automation because it means that my calendar is blocked out during my planned vacation without me having to do so myself. So far so good.
The core issue was a lack of integration between my Google Calendar and Gmail settings. While the HR — Calendar integration diligently scheduled my time off in Calendar, there was no automatic trigger to update my Gmail settings. This left me relying on my faulty memory, which clearly wasn’t cutting it.
The Solution: A Simple Yet Powerful Apps Script Automation
Being a massive fan of Apps Script, I knew there was a way to automate my pain away. No more being annoyed that I forgot to turn on the Gmail vacation reply while I’m enjoying a day at the beach.
I created a standalone Apps Script that uses the Calendar API advanced service as well as the Gmail API advanced service. Once enabled in the Apps Script IDE, these two services allow you to use the respective APIs in Apps Script.
The script itself is very straightforward. First, it gets all the events of type ‘outOfOffice’ for the current day. If there are such events, and they have ‘OOO’ as summary, this is the default summary text the HR — Calendar integration sets, the function calls the turnAutoResponder_() function.
That function in turn first checks to see if the auto responder is set. This is that for consecutive out of office days we don’t unnecessarily set the auto responder day after day. If that check results in “false”, the function goes ahead and sets the auto responder in the Gmail settings.
/**
* Using the Calendar advanced Apps Script service, this function gets a list of all the day's
* Calendar events that are of type 'outOfOffice'. To identify the events added from the HR system,
* we make sure it contains the summary 'OOO'. Only then do we call the function that enables the Gmail auto reply setting.
*/
function getOutOfOfficeEvents() {
const calendarId = 'primary';
const optionalArgs = {
eventTypes: ['outOfOffice'],
showDeleted: false,
timeMin: getDateAndHours_(0, 01),
timeMax: getDateAndHours_(23, 59),
}
let response = Calendar.Events.list(calendarId, optionalArgs);
if (response.items.length != 0) {
if (response.items[0].eventType === 'outOfOffice' && response.items[0].summary === 'OOO') {
turnOnAutoResponder_()
}
}
}
/**
* The timeMin and timeMax parameter for Calendar.Events.list has to be a RFC3339 timestamp.
* This helper function allows for getting the date and time in the necessary format.
*/
function getDateAndHours_(hour, minutes) {
const date = new Date();
date.setHours(hour);
date.setMinutes(minutes);
date.setSeconds(0);
date.setMilliseconds(0);
return date.toISOString();
}
/**
* This function checks to see if the auto reply in Gmail is enables. If it is not enabled yet, the function
* enables the auto reply.
*/
function turnOnAutoResponder_() {
const autoResponderStatus = Gmail.Users.Settings.getVacation('me');
if (autoResponderStatus.enableAutoReply === false) {
Gmail.Users.Settings.updateVacation({
enableAutoReply: true,
responseSubject: 'I am out of the office today',
responseBodyHtml: "I am out of the office today and I'm not checking my emails. <br>Once I'm back, I'll respond to your message.",
restrictToContacts: true,
restrictToDomain: true,
}, 'me');
}
}
Given I wanted the script to run every day, I set up a time-driven trigger that runs daily between 4am and 5am. I also chose to be notified immediately whenever the script fails. There is no specific reason why I chose that notification setting other than that it’s the first in the drop down list. It saved me an unnecessary click. Hey, it’s all about being as efficient as possible. 😆
Why This Matters (Beyond My Own Sanity)
This little automation highlights the broader potential of Apps Script. It’s a testament to the power of customization and the ability to tailor our digital environments to our specific needs.
So, if you find yourself struggling with a repetitive or forgettable task, don’t hesitate to explore the possibilities of Apps Script. Your future, less-stressed self will thank you!