Advertisement
notovny

Kludged Calendar Copier Script 2

Oct 12th, 2017
85
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. */
  10.  
  11. var SOURCE_CALENDAR_LIST = ["SourceCal1","SourceCal2","SourceCal3","SourceCal4"];
  12. var TARGET_CALENDAR_NAME = "TestCal";
  13. var SEARCH_STRING = "Bob";  /* Name to find in Source Calendar event list titles*/
  14.  
  15. var NOW = new Date();
  16. var START_SEARCH_DATE = new Date(NOW.getFullYear(), NOW.getMonth() - 1, 1); //Midnight before First day of last month
  17. var END_SEARCH_DATE = new Date(NOW.getFullYear(), NOW.getMonth() + 2, 1);   //Midnight After last day of next month
  18.  
  19. 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 */
  20.  
  21. function CalendarCopier_Insane_Main() {
  22.  
  23.  
  24.   /* Find all the calendars in the list */
  25.   var sourceCalendars = [];
  26.   for(var i = 0; i < SOURCE_CALENDAR_LIST.length; i++){
  27.     var cals = CalendarApp.getCalendarsByName(SOURCE_CALENDAR_LIST[i]);
  28.     if (cals && cals.length > 0){
  29.       sourceCalendars.push(cals[0]);
  30.     }
  31.   }
  32.  
  33.   var targetCalendars = CalendarApp.getCalendarsByName(TARGET_CALENDAR_NAME);
  34.  
  35.   /* Ensure that we have at least one source calendar, and at least one target calendar*/  
  36.   if (targetCalendars && sourceCalendars && targetCalendars.length > 0 && sourceCalendars.length > 0){
  37.     targetCal = targetCalendars[0];
  38.    
  39.     var targetEvents = targetCal.getEvents(START_SEARCH_DATE,END_SEARCH_DATE);
  40.    
  41.     /*Loop through the Target Calendar and delete its events in the range, if desired */
  42.     if(CLEAR_CALENDAR_IN_RANGE){
  43.       for(var i = 0 ; i < targetEvents.length; i++){
  44.         var targetEvent = targetEvents[i];
  45.         targetEvent.deleteEvent();
  46.       }
  47.     }
  48.    
  49.     /*Loop through EACH source calendar's events */
  50.     for(var j = 0; j < sourceCalendars.length; j++){
  51.       var sourceCal = sourceCalendars[j];
  52.       var sourceEvents = sourceCal.getEvents(START_SEARCH_DATE,END_SEARCH_DATE)
  53.      
  54.       for(var i = 0; i < sourceEvents.length; i++){
  55.         var sourceEvent = sourceEvents[i];
  56.         var sourceTitle = sourceEvent.getTitle();
  57.        
  58.         if(sourceTitle.indexOf(SEARCH_STRING) >= 0 && sourceEvent.isAllDayEvent() ){
  59.           var newEvent = false;
  60.           var description = sourceEvent.getDescription();
  61.           var startDate = sourceEvent.getStartTime();
  62.          
  63.           /* Assumption: Last part of the title is always going to be the time range, separated by at least one space.*/
  64.           var titleSplit = sourceTitle.split(" ");
  65.           var timeRangeString = "";
  66.           if(titleSplit.length > 0){
  67.             timeRangeString = titleSplit[titleSplit.length - 1];
  68.           }
  69.          
  70.           var dateString = startDate.getFullYear() + "/" + (startDate.getMonth() + 1)+ "/" + startDate.getDate()  /* format YYYY/M/D */
  71.           var createString = "PLACEHOLDER TITLE " + dateString + " " + timeRangeString;
  72.          
  73.           /* Attempts to create the new event on the target calendar */
  74.           try{
  75.             newEvent = targetCal.createEventFromDescription(createString);
  76.             /*If successful, renames the new Event */
  77.             if(newEvent){
  78.               newEvent.setTitle(sourceTitle); // Give the new event the original Title
  79.               newEvent.setDescription(description); // Give the new event the original Description.
  80.             }
  81.             Utilities.sleep(50); /* Hamfisted attempt to avoid API limits by delaying 1/20 of a second, could be done better.*/
  82.           }catch(e){
  83.             Logger.log(e);
  84.           }
  85.          
  86.  
  87.          
  88.         }
  89.       }
  90.      
  91.     }
  92.   }
  93.  
  94.  
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement