You are on page 1of 6

TIKTOK VIDEO OUTCOME - WATCH THIS VIDEO FIRST

This will assist you in identifying conflicting events within a specified date range in your Google
Calendar. Moreover, it sends a detailed email report of these conflicts to a specified recipient.

Instruction:

Setting Up the Script

1. Create a New Google Apps Script:


a. Navigate to Google Drive.
b. Click on “New” > “More” > “Google Apps Script.”
c. Delete any pre-existing code and paste the provided script into the code editor.
2. Authorize the Script:
a. Click on the disk icon or navigate to "File" > "Save" and name your project.
b. Click on the run (play) icon in the toolbar, and select the main function to
execute.
c. If it's your first time running the script, you will be prompted to review
permissions. Click on "Review Permissions," choose your account, and click
"Allow."

Using the Script

1. Specify Parameters:
a. Within the main function, set the recipientEmail to the email address that will
receive the conflict report.
b. Specify startDateStr and endDateStr for the date range you want to check for
conflicts. Ensure dates are in MM/DD/YYYY format.

function main() {
var recipientEmail = "recipient@example.com";
var startDateStr = "10/09/2023"; //
var endDateStr = "10/15/2023"; //

checkForConflictsAndSendEmail(recipientEmail, startDateStr, endDateStr);


}
Code

NOTE: Make sure to Change: “checkForConflictsAndSendEmail” to “main”

Version 1: Specified Dates (Default or Main Calendar)


function checkForConflictsAndSendEmail(recipientEmail, ccEmail, startDateStr, endDateStr) {
var startDate = new Date(startDateStr);
var endDate = new Date(endDateStr);
var calendar = CalendarApp.getDefaultCalendar();
var events = calendar.getEvents(startDate, endDate);
var conflicts = {};
var timeZone = calendar.getTimeZone();

events.sort(function(a, b) {
return a.getStartTime().getTime() - b.getStartTime().getTime();
});

for (var i = 1; i < events.length; i++) {


if (events[i].getStartTime().getTime() < events[i - 1].getEndTime().getTime()) {
var dateStr = Utilities.formatDate(events[i].getStartTime(), timeZone, "EEE, MMM dd,
yyyy");
if (!conflicts[dateStr]) {
conflicts[dateStr] = [];
}

var duration = (events[i - 1].getEndTime().getTime() -


events[i].getStartTime().getTime()) / 60000;
conflicts[dateStr].push({
event1: events[i - 1].getTitle() + ' - ' + Utilities.formatDate(events[i -
1].getStartTime(), timeZone, "hh:mm a") + ' to ' + Utilities.formatDate(events[i -
1].getEndTime(), timeZone, "hh:mm a") + ', ' + timeZone,
event2: events[i].getTitle() + ' - ' + Utilities.formatDate(events[i].getStartTime(),
timeZone, "hh:mm a") + ' to ' + Utilities.formatDate(events[i].getEndTime(), timeZone, "hh:mm
a") + ', ' + timeZone,
duration: duration + ' minutes'
});
}
}

var body = 'Hi amazing person,<br><br>';


body += 'I hope your day is going great!<br><br>';
body += 'Here are the conflicting events from ' + Utilities.formatDate(startDate, timeZone,
"MMMM dd, yyyy") + ' to ' + Utilities.formatDate(endDate, timeZone, "MMMM dd, yyyy") +
':<br><br>';

for (var date in conflicts) {


body += '<b>' + date + '</b><br><br>';
conflicts[date].slice(0, 20).forEach(function(conflict, index) {
body += (index + 1) + '. Conflict Duration: ' + conflict.duration + '<br>';
body += '• ' + conflict.event1.split(' - ')[0] + ' - ' + conflict.event1.split(' - ')[1] + '<br>';
body += '• ' + conflict.event2.split(' - ')[0] + ' - ' + conflict.event2.split(' - ')[1] +
'<br><br>';
});
}

body += 'Best,<br><br>Your loyal assistant';

var subject = 'Conflict Events - ' + Utilities.formatDate(startDate, timeZone, "MMMM dd,


yyyy") + ' to ' + Utilities.formatDate(endDate, timeZone, "MMMM dd, yyyy");

var emailOptions = {htmlBody: body};


if (ccEmail) {
emailOptions.cc = ccEmail;
}

GmailApp.sendEmail(recipientEmail, subject, "", emailOptions);


}

function main() {
var recipientEmail = "recipient@example.com";
var ccEmail = "cc@example.com";
var startDateStr = "05/29/2023";
var endDateStr = "06/03/2023";

checkForConflictsAndSendEmail(recipientEmail, ccEmail, startDateStr, endDateStr);


}

Version 2: Specified Dates (All Calendars)


function checkForConflictsAndSendEmail(recipientEmail, ccEmail, startDateStr, endDateStr) {
var startDate = new Date(startDateStr);
var endDate = new Date(endDateStr);
var allCalendars = CalendarApp.getAllCalendars();
var allEvents = [];
var timeZone;

allCalendars.forEach(function(calendar) {
var events = calendar.getEvents(startDate, endDate);
allEvents = allEvents.concat(events);
if (!timeZone) {
timeZone = calendar.getTimeZone();
}
});

allEvents.sort(function(a, b) {
return a.getStartTime().getTime() - b.getStartTime().getTime();
});

var conflicts = {};


for (var i = 1; i < allEvents.length; i++) {
if (allEvents[i].getStartTime().getTime() < allEvents[i - 1].getEndTime().getTime()) {
var dateStr = Utilities.formatDate(allEvents[i].getStartTime(), timeZone, "EEE, MMM
dd, yyyy");
if (!conflicts[dateStr]) {
conflicts[dateStr] = [];
}

var duration = (allEvents[i - 1].getEndTime().getTime() -


allEvents[i].getStartTime().getTime()) / 60000;
conflicts[dateStr].push({
event1: allEvents[i - 1].getTitle() + ' - ' + Utilities.formatDate(allEvents[i -
1].getStartTime(), timeZone, "hh:mm a") + ' to ' + Utilities.formatDate(allEvents[i -
1].getEndTime(), timeZone, "hh:mm a") + ', ' + timeZone,
event2: allEvents[i].getTitle() + ' - ' + Utilities.formatDate(allEvents[i].getStartTime(),
timeZone, "hh:mm a") + ' to ' + Utilities.formatDate(allEvents[i].getEndTime(), timeZone,
"hh:mm a") + ', ' + timeZone,
duration: duration + ' minutes'
});
}
}

var body = 'Hi amazing person,<br><br>';


body += 'I hope your day is going great!<br><br>';
body += 'Here are the conflicting events from ' + Utilities.formatDate(startDate, timeZone,
"MMMM dd, yyyy") + ' to ' + Utilities.formatDate(endDate, timeZone, "MMMM dd, yyyy") +
':<br><br>';

for (var date in conflicts) {


body += '<b>' + date + '</b><br><br>';
conflicts[date].forEach(function(conflict, index) {
body += (index + 1) + '. Conflict Duration: ' + conflict.duration + '<br>';
body += '• ' + conflict.event1.split(' - ')[0] + ' - ' + conflict.event1.split(' - ')[1] + '<br>';
body += '• ' + conflict.event2.split(' - ')[0] + ' - ' + conflict.event2.split(' - ')[1] +
'<br><br>';
});
}

body += 'Best,<br><br>Your loyal assistant';

var subject = 'Conflict Events - ' + Utilities.formatDate(startDate, timeZone, "MMMM dd,


yyyy") + ' to ' + Utilities.formatDate(endDate, timeZone, "MMMM dd, yyyy");

var emailOptions = {htmlBody: body};


if (ccEmail) {
emailOptions.cc = ccEmail;
}

GmailApp.sendEmail(recipientEmail, subject, "", emailOptions);


}

function main() {
var recipientEmail = "recipient@example.com";
var ccEmail = "cc@example.com";
var startDateStr = "05/29/2023";
var endDateStr = "06/03/2023";

checkForConflictsAndSendEmail(recipientEmail, ccEmail, startDateStr, endDateStr);


}

Version 3: Recurring Emails

function checkForConflictsAndSendEmail(recipientEmail, ccEmail, startDateStr, endDateStr) {


var startDate = new Date(startDateStr);
var endDate = new Date(endDateStr);
var calendar = CalendarApp.getDefaultCalendar();
var events = calendar.getEvents(startDate, endDate);
var conflicts = {};
var timeZone = calendar.getTimeZone();

events.sort(function(a, b) {
return a.getStartTime().getTime() - b.getStartTime().getTime();
});

for (var i = 1; i < events.length; i++) {


if (events[i].getStartTime().getTime() < events[i - 1].getEndTime().getTime()) {
var dateStr = Utilities.formatDate(events[i].getStartTime(), timeZone, "EEE, MMM dd,
yyyy");
if (!conflicts[dateStr]) {
conflicts[dateStr] = [];
}

var duration = (events[i - 1].getEndTime().getTime() -


events[i].getStartTime().getTime()) / 60000;
conflicts[dateStr].push({
event1: events[i - 1].getTitle() + ' - ' + Utilities.formatDate(events[i -
1].getStartTime(), timeZone, "hh:mm a") + ' to ' + Utilities.formatDate(events[i -
1].getEndTime(), timeZone, "hh:mm a") + ', ' + timeZone,
event2: events[i].getTitle() + ' - ' + Utilities.formatDate(events[i].getStartTime(),
timeZone, "hh:mm a") + ' to ' + Utilities.formatDate(events[i].getEndTime(), timeZone, "hh:mm
a") + ', ' + timeZone,
duration: duration + ' minutes'
});
}
}
var body = 'Hi amazing person,<br><br>';
body += 'I hope your day is going great!<br><br>';
body += 'Here are the conflicting events from ' + Utilities.formatDate(startDate, timeZone,
"MMMM dd, yyyy") + ' to ' + Utilities.formatDate(endDate, timeZone, "MMMM dd, yyyy") +
':<br><br>';

for (var date in conflicts) {


body += '<b>' + date + '</b><br><br>';
conflicts[date].slice(0, 20).forEach(function(conflict, index) {
body += (index + 1) + '. Conflict Duration: ' + conflict.duration + '<br>';
body += '• ' + conflict.event1.split(' - ')[0] + ' - ' + conflict.event1.split(' - ')[1] + '<br>';
body += '• ' + conflict.event2.split(' - ')[0] + ' - ' + conflict.event2.split(' - ')[1] +
'<br><br>';
});
}

body += 'Best,<br><br>Your loyal assistant';

if (body) {
var subject = 'Conflict Events - ' + Utilities.formatDate(startDate, timeZone, "MMMM dd,
yyyy") + ' to ' + Utilities.formatDate(endDate, timeZone, "MMMM dd, yyyy");
GmailApp.sendEmail(recipientEmail, subject, "", {htmlBody: body, cc: ccEmail});
}
}

function main() {
var recipientEmail = "recipient@example.com";
var ccEmail = "cc@example.com";
var weeksFromNow = 1;
var currentDate = new Date();
var daysUntilNextCheck = (weeksFromNow * 7) - currentDate.getDay();
var startDate = new Date(currentDate.setDate(currentDate.getDate() +
daysUntilNextCheck));
var endDate = new Date(startDate);
endDate.setDate(endDate.getDate() + 6);

checkForConflictsAndSendEmail(recipientEmail, ccEmail, Utilities.formatDate(startDate,


"GMT", "MM/dd/yyyy"), Utilities.formatDate(endDate, "GMT", "MM/dd/yyyy"));
}

You might also like