Skip to content

Instantly share code, notes, and snippets.

@itdependsnetworks
Last active January 4, 2022 03:06
Show Gist options
  • Save itdependsnetworks/d087eae399dc49f904b9dea57e011158 to your computer and use it in GitHub Desktop.
Save itdependsnetworks/d087eae399dc49f904b9dea57e011158 to your computer and use it in GitHub Desktop.
Calendar-Charge-Code
function myFunction() {
//---------The following variables are meant to be changed to meet your specfic needs---------
const domainSuffix = 'networktocode.com';
const daysOut = 90;
const msgBody = "This invite has been declined automatically, as it was received from a networktocode.com email domain, without a subject line ending with a charge code.\n\n";
// Regex searches for XXX-DD, (XXX-DD), and [XXX-DD] with optional whitespace at the end
const regex = /([\(|\[])?\S+?\-\d+([\)|\]])?($|\s+)/;
//---------The code below should only be required to be edited if you wish to change the functionality of the process---------
var startDate = new Date();
var endDate = new Date(new Date().setDate(new Date().getDate() + daysOut));
var calendar = CalendarApp.getDefaultCalendar();
var seenInvites = [];
var events = calendar.getEvents(startDate, endDate);
for (var i = 0; i < events.length; i++){
var originalSender = events[i].getOriginalCalendarId();
var subjectLine = events[i].getTitle().toString();
if (!originalSender.endsWith(domainSuffix))
continue;
else if (subjectLine.match(regex))
continue;
else if (seenInvites.includes(subjectLine))
continue;
else if (events[i].getMyStatus() == CalendarApp.GuestStatus.NO )
continue;
else if (events[i].isOwnedByMe())
continue;
seenInvites.push(subjectLine)
Logger.log("Sending email to : '" + originalSender + "' to let them know this email has been deleted without proper subject: " + subjectLine);
// Send mail
GmailApp.sendEmail(
originalSender,
"⚡⚡ Call to Action ⚡⚡ - An invite for '" + subjectLine + "' has been automatically declined, please see email for details",
msgBody,
);
if (events[i].isRecurringEvent()) {
// events[i].getEventSeries().deleteEventSeries() // You can use this to delete this altogether.
events[i].getEventSeries().setMyStatus(CalendarApp.GuestStatus.NO)
}
else {
// events[i].deleteEvent() // You can use this to delete this altogether.
events[i].setMyStatus(CalendarApp.GuestStatus.NO)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment