Skip to content

Instantly share code, notes, and snippets.

@science
Created August 2, 2022 21:27
Show Gist options
  • Save science/e7977547636c9d3fe02c7d0cf596d7cd to your computer and use it in GitHub Desktop.
Save science/e7977547636c9d3fe02c7d0cf596d7cd to your computer and use it in GitHub Desktop.
GAppsScript that looks in Calendar to find all day event with keyword, on presence, turns on gmail autoresponder, otherwise turns off autoresponder
function autoReply() {
Logger.log('Script start');
var strCalendarOOFKey = 'OOF';
var strUserEmailToSetVacationOn = '[email protected]';
// find calendar event
var today = new Date();
var msTodayStart = today.getTime();
var msTodayEnd = today.getTime()+60000; // adding 1 min, but in effect it means start and end on the same day
var unavailableToday = false;
Logger.log('Looking for Calendar trigger "'+strCalendarOOFKey+'" for today '+today.toDateString());
var events = CalendarApp.getDefaultCalendar().getEventsForDay(today, { search: 'OOF' });
for (var i = 0; i < events.length; i++) {
if(events[i].isAllDayEvent() && events[i].isOwnedByMe()) {
Logger.log("Setting vacation response to true due to event titled '"+events[i].getTitle()+"'")
unavailableToday = true;
break;
}
}
Logger.log("Setting Vacation response to: "+unavailableToday.toString());
var jsonVacationSettingsOn = {
"enableAutoReply": true,
"responseSubject": "I'm currently out of the office",
"responseBodyPlainText": "If you need any help while I'm gone contact [email protected]",
"responseBodyHtml": "If you need any help while I'm gone contact [email protected]",
"restrictToContacts": true,
"restrictToDomain": false,
"startTime": msTodayStart,
"endTime": msTodayEnd
};
var jsonVacationSettingsOff = {
"enableAutoReply": false,
};
if(unavailableToday == true){
Logger.log('Updating Vacation Responder to On');
var vacationSettings = Gmail.Users.Settings.updateVacation(
jsonVacationSettingsOn,
strUserEmailToSetVacationOn
);
} else {
Logger.log('Updating Vacation Responder to Off');
var vacationSettings = Gmail.Users.Settings.updateVacation(
jsonVacationSettingsOff,
strUserEmailToSetVacationOn
);
};
Logger.log('Finished updating vacation responder:'+today.toDateString());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment