Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Kludged Google Apps Script to attempt to turn all-day events with titles like "Bob 10-6" into time spanning calendar events.
- Searching between the first day of last month and the last day of next month, it should:
- * Optionally (If CLEAR_CALENDAR_IN_RANGE = true) delete all events on the calendar TARGET_CALENDAR_NAME in the range
- * Find all calendar events in the owned/subscribed calendars with names listed in SOURCE_CALENDAR_LIST
- * Create an event on the owned calendar named by TARGET_CALENDAR_NAME on the same day using the QuickAdd syntax to input time information.
- *
- Changes follow:
- * Script uses Exponential Backoff to try to avoid API lockout.
- * Script attempts to parse title string with regular expressions, with a fallback to using the original method, while trimming the spaces from the source event.
- * Script logs success and failure of attempts to create new events, or delete old events.
- * Title of target event is built from concatenating the SEARCH_STRING and the name of the source calendar,
- * Event description includes the original event title, and the Quick Add string used to create the new event.
- */
- var SOURCE_CALENDAR_LIST = ["SourceCal1","SourceCal2","SourceCal3","SourceCal4"];
- var TARGET_CALENDAR_NAME = "TestCal";
- var SEARCH_STRING = "Bob"; /* Name to find in Source Calendar event list titles*/
- var NOW = new Date();
- var START_SEARCH_DATE = new Date(NOW.getFullYear(), NOW.getMonth() - 1, 1); //Midnight before First day of last month
- var END_SEARCH_DATE = new Date(NOW.getFullYear(), NOW.getMonth() + 2, 1); //Midnight After last day of next month
- var CLEAR_CALENDAR_IN_RANGE = false; /* ONLY SET THIS TO TRUE IF YOU ARE SURE YOU WANT TO ERASE EVERYTING IN THE TARGET CALENDAR IN THE RANGE */
- function CalendarCopier_Insane_Main_V2() {
- /* Find all the calendars in the list */
- var sourceCalendars = []
- for(var i = 0; i < SOURCE_CALENDAR_LIST.length; i++){
- var cals = CalendarApp.getCalendarsByName(SOURCE_CALENDAR_LIST[i]);
- if (cals && cals.length > 0){
- sourceCalendars.push(cals[0]);
- }
- }
- var targetCalendars = CalendarApp.getCalendarsByName(TARGET_CALENDAR_NAME);
- /* Ensure that we have at least one source calendar, and at least one target calendar*/
- if (targetCalendars && sourceCalendars && targetCalendars.length > 0 && sourceCalendars.length > 0){
- targetCal = targetCalendars[0];
- var targetEvents = targetCal.getEvents(START_SEARCH_DATE,END_SEARCH_DATE);
- /*Loop through the Target Calendar and delete its events in the range, if desired */
- if(CLEAR_CALENDAR_IN_RANGE){
- for(var i = 0 ; i < targetEvents.length; i++){
- var targetEvent = targetEvents[i];
- for(var n = 0; n <= 6; n++){
- try{
- targetEvent.deleteEvent();
- Logger.log("Deleted Event on "+ targetEvent.getStartTime()+" : \"" + targetEvent.getTitle() + "\"")
- break;
- }catch(e){
- if(n == 5){
- Logger.log("Failed to delete Event on "+ targetEvent.getStartTime()+" : \"" + targetEvent.getTitle() + "\"");
- Logger.log(e);
- }
- }
- /* Exponential backoff */
- Utilities.sleep((Math.pow(2,n)*1000) + (Math.round(Math.random() * 1000)));
- }
- }
- }
- /*Loop through EACH source calendar's events */
- for(var j = 0; j < sourceCalendars.length; j++){
- Logger.log("Processing Calendar: " + SOURCE_CALENDAR_LIST[j]);
- var sourceCal = sourceCalendars[j];
- var sourceEvents = sourceCal.getEvents(START_SEARCH_DATE,END_SEARCH_DATE)
- for(var i = 0; i < sourceEvents.length; i++){
- var sourceEvent = sourceEvents[i];
- var sourceTitle = sourceEvent.getTitle();
- var titleSplit = [];
- if(sourceTitle.indexOf(SEARCH_STRING) >= 0 ){
- var newEvent = false;
- var description = sourceEvent.getDescription();
- var startDate = sourceEvent.getStartTime();
- /* Assumption: Last part of the title is always going to be the time range, separated by at least one space.*/
- var timeRangeString = "";
- var dateString = startDate.getFullYear() + "/" + (startDate.getMonth() + 1)+ "/" + startDate.getDate() /* format YYYY/M/D */
- /* Attempt to parse with regular expressions*/
- var regExp = new RegExp(/(.*)\s+(\d+:?\d*)\s*-\s*(\d+:?\d*)/);
- var match = regExp.exec(sourceTitle);
- if(match){
- var timeRangeString = match[2] + " to " + match[3];
- }else{ /* Use String.split() instead */
- titleSplit = sourceTitle.trim().split(" ");
- if(titleSplit.length > 0){
- timeRangeString = titleSplit[titleSplit.length - 1];
- }
- }
- var createString = SEARCH_STRING + " "+ SOURCE_CALENDAR_LIST[j] +" on " + dateString + " from " + timeRangeString;
- /* Attempts to create the new event on the target calendar, using exponential backoff */
- for(var n = 0; n <= 6; n++){
- try{
- newEvent = targetCal.createEventFromDescription(createString);
- if(newEvent){
- description = description + "\nOriginal Title: \"" + sourceTitle + "\""
- + "\nOriginal Calendar: \"" + SOURCE_CALENDAR_LIST[j] + "\""
- + "\nQuickAdd Syntax: \"" + createString + "\""
- newEvent.setDescription(description); // Give the new event the original Description.
- }
- Logger.log("Created event on " + dateString + " from \"" + sourceTitle +"\"");
- break;
- }catch(e){
- if(n == 5){
- Logger.log("Failed to create Event on " + dateString + " from \"" + sourceTitle +"\"");
- Logger.log(e);
- }
- }
- /* Exponential backoff */
- Utilities.sleep((Math.pow(2,n)*1000) + (Math.round(Math.random() * 1000)));
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement