Skip to content

Instantly share code, notes, and snippets.

@itdependsnetworks
Created September 26, 2024 18:32
Show Gist options
  • Select an option

  • Save itdependsnetworks/6596989712ea0174ce58137cf441bbcb to your computer and use it in GitHub Desktop.

Select an option

Save itdependsnetworks/6596989712ea0174ce58137cf441bbcb to your computer and use it in GitHub Desktop.
calendar-charge-code-add
function myFunction() {
//---------The following variables are meant to be changed to meet your specfic needs---------
const daysOut = 14;
const regex = /([\(|\[])?\S+?\-\d+([\)|\]])?($|\s+)/;
const regexCapture = /(?:[\(|\[])?(\S+?\-\d+)([\)|\]])?($|\s+)/g;
// An example, modify as required
const customerDomain = {
"coke.com": "COKE-10",
"pepsi.com": "PEP-89",
"acme.com": "ACME-111",
"acme-corp.com": "ACME-111",
};
//---------The preceeding variables are meant to be changed to meet your specfic needs---------
const customerKeys = Object.keys(customerDomain);
function getMatches(string, regex, index) {
index || (index = 1); // default to the first capturing group
var matches = [];
var match;
while (match = regex.exec(string)) {
matches.push(match[index]);
}
return matches;
}
var startDate = new Date();
var endDate = new Date(new Date().setDate(new Date().getDate() + daysOut));
var calendar = CalendarApp.getDefaultCalendar();
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();
var guests = events[i].getGuestList().map(function(eventGuest){return eventGuest.getEmail();});
// Code to set charge code based on domain suffix
if (!subjectLine.match(regex)) {
var address = originalSender.split('@').pop()
if (!customerKeys.includes(address)) {
continue
}
Logger.log("Matched NOT REGEX: " + subjectLine + " " + address + " " + customerKeys.includes(address));
var newTitle = subjectLine + " [" + customerDomain[address] + "]"
events[i].setTitle(newTitle)
Logger.log("Set new title of " + newTitle)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment