Skip to content

Instantly share code, notes, and snippets.

@JLarky
Last active February 7, 2024 06:42
Show Gist options
  • Save JLarky/cbb12a6fc87e886479c9ef5e4e10ce80 to your computer and use it in GitHub Desktop.
Save JLarky/cbb12a6fc87e886479c9ef5e4e10ce80 to your computer and use it in GitHub Desktop.
Google Auto Decline calendar events
// @ts-check
function runOnSchedule() {
declineEventsForDate();
}
function declineEventsForDate() {
var calendar = CalendarApp.getDefaultCalendar(); // Gets your default calendar
var now = new Date();
var sevenDaysFromNow = new Date(now.getTime() + 7 * 24 * 60 * 60 * 1000);
var events = CalendarApp.getDefaultCalendar().getEvents(
now,
sevenDaysFromNow
);
for (var i = 0; i < events.length; i++) {
var event = events[i];
var myStatus = event.getMyStatus();
// Check if already declined or needs action
if (
myStatus !== CalendarApp.GuestStatus.NO &&
myStatus !== CalendarApp.GuestStatus.YES
) {
const eventStarts = event.getStartTime();
const eventEnds = event.getEndTime();
const startHours = eventStarts.getHours();
const startMinutes = eventStarts.getMinutes();
// now check if that time is falling in my break from 1:30pm to 4pm
console.log(
"Event",
event.getTitle(),
event.getMyStatus().toString(),
startHours,
startMinutes
);
if (
((startHours === 13 && startMinutes >= 30) || startHours > 13) &&
startHours < 16
) {
// Decline the event
console.log("Event declined");
console.log(eventStarts, eventEnds);
event.setMyStatus(CalendarApp.GuestStatus.NO);
} else {
console.log("Event not declined");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment