Advertisement
notovny

Kludged Calendar Script 3

Oct 13th, 2017
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Kludged Google Apps Script to attempt to turn all-day events with titles like "Bob 10-6" into time spanning calendar events.
  2.  
  3. Searching between the first day of last month and the last day of next month, it should:
  4.   * Optionally (If CLEAR_CALENDAR_IN_RANGE = true) delete all events on the calendar TARGET_CALENDAR_NAME in the range
  5.   * Find all calendar events in the owned/subscribed calendars with names listed in SOURCE_CALENDAR_LIST
  6.   * Create an event on the owned calendar named by TARGET_CALENDAR_NAME on the same day using the QuickAdd syntax to input time information.
  7.   *
  8.  
  9.   Changes follow:
  10.   * Script uses Exponential Backoff to try to avoid API lockout.
  11.   * 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.
  12.   * Script logs success and failure of attempts to create new events, or delete old events.
  13.   * Title of target event is built from concatenating the SEARCH_STRING and the name of the source calendar,
  14.   * Event description includes the original event title, and the Quick Add string used to create the new event.
  15.  
  16. */
  17.  
  18. var SOURCE_CALENDAR_LIST = ["SourceCal1","SourceCal2","SourceCal3","SourceCal4"];
  19. var TARGET_CALENDAR_NAME = "TestCal";
  20. var SEARCH_STRING = "Bob";  /* Name to find in Source Calendar event list titles*/
  21.  
  22. var NOW = new Date();
  23. var START_SEARCH_DATE = new Date(NOW.getFullYear(), NOW.getMonth() - 1, 1); //Midnight before First day of last month
  24. var END_SEARCH_DATE = new Date(NOW.getFullYear(), NOW.getMonth() + 2, 1);   //Midnight After last day of next month
  25.  
  26. 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 */
  27.  
  28. function CalendarCopier_Insane_Main_V2() {
  29.  
  30.  
  31.   /* Find all the calendars in the list */
  32.   var sourceCalendars = []
  33.   for(var i = 0; i < SOURCE_CALENDAR_LIST.length; i++){
  34.     var cals = CalendarApp.getCalendarsByName(SOURCE_CALENDAR_LIST[i]);
  35.     if (cals && cals.length > 0){
  36.       sourceCalendars.push(cals[0]);
  37.     }
  38.   }
  39.  
  40.   var targetCalendars = CalendarApp.getCalendarsByName(TARGET_CALENDAR_NAME);
  41.  
  42.   /* Ensure that we have at least one source calendar, and at least one target calendar*/  
  43.   if (targetCalendars && sourceCalendars && targetCalendars.length > 0 && sourceCalendars.length > 0){
  44.     targetCal = targetCalendars[0];
  45.    
  46.     var targetEvents = targetCal.getEvents(START_SEARCH_DATE,END_SEARCH_DATE);
  47.    
  48.     /*Loop through the Target Calendar and delete its events in the range, if desired */
  49.     if(CLEAR_CALENDAR_IN_RANGE){
  50.       for(var i = 0 ; i < targetEvents.length; i++){
  51.         var targetEvent = targetEvents[i];
  52.        
  53.         for(var n = 0; n <= 6; n++){
  54.           try{
  55.             targetEvent.deleteEvent();
  56.             Logger.log("Deleted Event on "+ targetEvent.getStartTime()+" : \"" + targetEvent.getTitle() + "\"")
  57.             break;
  58.           }catch(e){
  59.             if(n == 5){
  60.               Logger.log("Failed to delete Event on "+ targetEvent.getStartTime()+" : \"" + targetEvent.getTitle() + "\"");
  61.               Logger.log(e);
  62.             }
  63.           }
  64.           /* Exponential backoff */
  65.           Utilities.sleep((Math.pow(2,n)*1000) + (Math.round(Math.random() * 1000)));
  66.         }
  67.        
  68.       }
  69.     }
  70.    
  71.    
  72.     /*Loop through EACH source calendar's events */
  73.     for(var j = 0; j < sourceCalendars.length; j++){
  74.       Logger.log("Processing Calendar: " + SOURCE_CALENDAR_LIST[j]);
  75.       var sourceCal = sourceCalendars[j];
  76.       var sourceEvents = sourceCal.getEvents(START_SEARCH_DATE,END_SEARCH_DATE)
  77.      
  78.       for(var i = 0; i < sourceEvents.length; i++){
  79.         var sourceEvent = sourceEvents[i];
  80.         var sourceTitle = sourceEvent.getTitle();
  81.         var  titleSplit = [];
  82.         if(sourceTitle.indexOf(SEARCH_STRING) >= 0 ){
  83.           var newEvent = false;
  84.           var description = sourceEvent.getDescription();
  85.           var startDate = sourceEvent.getStartTime();
  86.          
  87.           /* Assumption: Last part of the title is always going to be the time range, separated by at least one space.*/
  88.          
  89.           var timeRangeString = "";
  90.           var dateString = startDate.getFullYear() + "/" + (startDate.getMonth() + 1)+ "/" + startDate.getDate()  /* format YYYY/M/D */
  91.          
  92.           /* Attempt to parse with regular expressions*/
  93.           var regExp = new RegExp(/(.*)\s+(\d+:?\d*)\s*-\s*(\d+:?\d*)/);
  94.           var match = regExp.exec(sourceTitle);
  95.           if(match){
  96.            
  97.             var timeRangeString = match[2] + " to " + match[3];
  98.          
  99.           }else{ /* Use String.split() instead */
  100.          
  101.             titleSplit = sourceTitle.trim().split(" ");
  102.             if(titleSplit.length > 0){
  103.               timeRangeString = titleSplit[titleSplit.length - 1];
  104.             }
  105.           }
  106.          
  107.          
  108.           var createString = SEARCH_STRING + " "+ SOURCE_CALENDAR_LIST[j] +" on " + dateString + " from " + timeRangeString;
  109.          
  110.           /* Attempts to create the new event on the target calendar, using exponential backoff */
  111.           for(var n = 0; n <= 6; n++){
  112.             try{
  113.               newEvent = targetCal.createEventFromDescription(createString);
  114.               if(newEvent){
  115.                 description = description + "\nOriginal Title: \"" + sourceTitle + "\""
  116.                                           + "\nOriginal Calendar: \"" + SOURCE_CALENDAR_LIST[j] + "\""
  117.                                           + "\nQuickAdd Syntax: \"" + createString + "\""
  118.                 newEvent.setDescription(description); // Give the new event the original Description.
  119.               }
  120.               Logger.log("Created event on " + dateString + " from \"" + sourceTitle +"\"");
  121.               break;
  122.             }catch(e){
  123.               if(n == 5){
  124.                 Logger.log("Failed to create Event on " + dateString + " from \"" + sourceTitle +"\"");
  125.                 Logger.log(e);
  126.               }
  127.             }
  128.             /* Exponential backoff */
  129.             Utilities.sleep((Math.pow(2,n)*1000) + (Math.round(Math.random() * 1000)));
  130.           }        
  131.         }
  132.       }
  133.      
  134.     }
  135.   }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement