Old calendar scrubber google script stopped working, so I re-wrote it to update it and have it better fit into how my schedule changes

This commit is contained in:
Norm Rasmussen
2025-07-23 14:41:11 -04:00
parent 0e039e4462
commit ffd706dba7
5 changed files with 187 additions and 0 deletions

View File

@ -0,0 +1 @@
{"scriptId":"1hpUsnmWrG4ObGXZNJFxfQ5tzMZo88T8baaHXSn_vxNkQmq7An6i7DyMc","rootDir":"/Users/normrasmussen/Documents/Work/Scripts/GAS_GS/New_HA_Cal_Scrubber"}

View File

@ -0,0 +1,33 @@
var SOURCE_CALENDAR_ID = 'nrasmussen@gainsight.com'; // OR the full calendar id including @something.calendar.google.com
var SCRUBBED_CALENDAR_ID = 'c_bf82fe2c44dd508f465816c6ba96a0ad45931310a634088bd572c40f9a90459b@group.calendar.google.com';
function syncTodaysCalendar() {
var count = 0
// Define the calendar event date range to search.
var today = new Date();
Logger.log(today);
var delEvents = CalendarApp.getCalendarById(SCRUBBED_CALENDAR_ID).getEvents(today, today);
for (let i = 0; i < delEvents.length; i++) {
delEvents[i].deleteEvent();
Logger.log("Today's Events Deleted from Scrubbed Calendar")
};
var mainCal = CalendarApp.getEventsForDay(today);
for (let i = 0; i < mainCal.length; i++) {
Utilities.sleep(1000);
try {
mainCal[i].getAllDayStartDate()
} catch (ad) {
Logger.log(ad)
try {
CalendarApp.getCalendarById(SCRUBBED_CALENDAR_ID).createEvent(mainCal[i].getTitle(), mainCal[i].getStartTime(), mainCal[i].getEndTime());
Logger.log('Importing: %s', mainCal[i].getTitle());
count++;
} catch (e) {
Logger.log(
'Error attempting to import event: %s. Skipping.', e.toString());
}
}
};
Logger.log('Imported ' + count + ' events');
};

View File

@ -0,0 +1,3 @@
function myFunction() {
}

View File

@ -0,0 +1,136 @@
var SOURCE_CALENDAR_ID = 'nrasmussen@gainsight.com'; // OR the full calendar id including @something.calendar.google.com
var SCRUBBED_CALENDAR_ID = 'c_bf82fe2c44dd508f465816c6ba96a0ad45931310a634088bd572c40f9a90459b@group.calendar.google.com';
var WEEKS_IN_ADVANCE = 1;
// The maximum script run time under Apps Script Pro is 30 minutes; this setting
// will be used to report when the script is about to reach that limit.
// var MAX_PRO_RUNTIME_MS = 29 * 60 * 1000;
function syncCalendarAfterScrubbing() {
var count = 0
// Define the calendar event date range to search.
var today = new Date();
Logger.log(today);
var futureDate = new Date();
futureDate.setDate(futureDate.getDate() + WEEKS_IN_ADVANCE*14);
Logger.log(futureDate);
var delEvents = CalendarApp.getCalendarById(SCRUBBED_CALENDAR_ID).getEvents(today, futureDate);
for (let i = 0; i < delEvents.length; i++) {
delEvents[i].deleteEvent();
Logger.log("Event Deleted from Scrubbed Calendar")
};
var dates = getDatesBetween(today, futureDate);
dates.forEach(function(date) {
var mainCal = CalendarApp.getEventsForDay(date);
Logger.log(date);
for (let i = 0; i < mainCal.length; i++) {
Utilities.sleep(1000);
try {
mainCal[i].getAllDayStartDate()
} catch (ad) {
Logger.log(ad)
try {
CalendarApp.getCalendarById(SCRUBBED_CALENDAR_ID).createEvent(mainCal[i].getTitle(), mainCal[i].getStartTime(), mainCal[i].getEndTime());
Logger.log('Importing: %s', mainCal[i].getTitle());
count++;
} catch (e) {
Logger.log(
'Error attempting to import event: %s. Skipping.', e.toString());
}
}
};
}
)
Logger.log('Imported ' + count + ' events');
};
//Logger.log(mainCal[i].getTitle());
//Logger.log(mainCal[i].getStartTime());
//Logger.log(mainCal[i].getEndTime());
function getDatesBetween(startDate, endDate) {
const dates = [];
const currentDate = new Date(startDate);
const lastDate = new Date(endDate);
currentDate.setHours(0, 0, 0, 0);
lastDate.setHours(0, 0, 0, 0);
while (currentDate <= lastDate) {
dates.push(new Date(currentDate));
currentDate.setDate(currentDate.getDate() + 1);
}
return dates;
}
// /**
// * In a given calendar, look for non-all-day events
// * in events within the specified date range and return any such events
// * found.
// * @param {string} cal_id the ID of the source calendar
// * @param {Date} start the starting Date of the range to examine.
// * @param {Date} end the ending Date of the range to examine.
// * @param {Date} opt_since a Date indicating the last time this script was run.
// * @return {object[]} an array of calendar event Objects.
// */
// function findEvents(cal_id, start, end, opt_since) {
// var params = {
// timeMin: formatDate(start),
// timeMax: formatDate(end),
// showDeleted: false
// };
// if (opt_since) {
// // This prevents the script from examining events that have not been
// // modified since the specified date (that is, the last time the
// // script was run).
// params['updatedMin'] = formatDate(opt_since);
// }
// var results = [];
// try {
// var response = Calendar.Events.list(cal_id, params);
// results = response.items.filter(function(item) {
// // Filter out events where the owner of the script has not accepted
// if (item.attendees) {
// for (person of item.attendees) {
// if (person['self'] == true && person['responseStatus'] != "accepted") {
// return false;
// }
// }
// }
// // Filter out events where the event is all-day
// if (item.start.date) {
// return false;
// }
// Logger.log(item.getDateCreated());
// return true;
// });
// } catch (e) {
// Logger.log('Error retriving events for %s; skipping',
// e.toString());
// results = [];
// }
// return results;
// }
// /**
// * Return an RFC3339 formated date String corresponding to the given
// * Date object.
// * @param {Date} date a Date.
// * @return {string} a formatted date string.
// */
// function formatDate(date) {
// return Utilities.formatDate(date, 'UTC', 'yyyy-MM-dd\'T\'HH:mm:ssZ');
// }
// /**
// * Compares two Date objects and returns true if the difference
// * between them is more than the maximum specified run time.
// *
// * @param {Date} start the first Date object.
// * @param {Date} now the (later) Date object.
// * @return {boolean} true if the time difference is greater than
// * MAX_PROP_RUNTIME_MS (in milliseconds).
// */
// function isTimeUp(start, now) {
// return now.getTime() - start.getTime() > MAX_PRO_RUNTIME_MS;
// }

View File

@ -0,0 +1,14 @@
{
"timeZone": "America/New_York",
"dependencies": {
"enabledAdvancedServices": [
{
"userSymbol": "Calendar",
"version": "v3",
"serviceId": "calendar"
}
]
},
"exceptionLogging": "STACKDRIVER",
"runtimeVersion": "V8"
}