notovny

Kludged Calendar Copier Script

Oct 11th, 2017
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.44 KB | None | 0 0
  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 calendar named by SOURCE_CALENDAR_NAME
  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. var SOURCE_CALENDAR_NAME = "TestCal2";
  11. var TARGET_CALENDAR_NAME = "TestCal";
  12. var SEARCH_STRING = "Bob"; /* Name to find in Source Calendar event list titles*/
  13.  
  14. var NOW = new Date();
  15. var START_SEARCH_DATE = new Date(NOW.getFullYear(), NOW.getMonth() - 1, 1); //Midnight before First day of last month
  16. var END_SEARCH_DATE = new Date(NOW.getFullYear(), NOW.getMonth() + 2, 1); //Midnight After last day of next month
  17.  
  18. var CLEAR_CALENDAR_IN_RANGE = true; /* ONLY SET THIS TO TRUE IF YOU ARE SURE YOU WANT TO ERASE EVERYTING IN THE TARGET CALENDAR IN THE RANGE */
  19.  
  20. function CalendarCopier_Main() {
  21. var sourceCalendars = CalendarApp.getCalendarsByName(SOURCE_CALENDAR_NAME);
  22. var targetCalendars = CalendarApp.getCalendarsByName(TARGET_CALENDAR_NAME);
  23.  
  24. if (sourceCalendars && targetCalendars && sourceCalendars.length > 0 && targetCalendars.length > 0 ){
  25. sourceCal = sourceCalendars[0];
  26. targetCal = targetCalendars[0];
  27.  
  28. var targetEvents = targetCal.getEvents(START_SEARCH_DATE,END_SEARCH_DATE);
  29. var sourceEvents = sourceCal.getEvents(START_SEARCH_DATE,END_SEARCH_DATE);
  30.  
  31. /*Loop through the Target Calendar and delete its events in the range. */
  32. if(CLEAR_CALENDAR_IN_RANGE){
  33. for(var i = 0 ; i < targetEvents.length; i++){
  34. var targetEvent = targetEvents[i];
  35. targetEvent.deleteEvent();
  36. }
  37. }
  38.  
  39. /*Loop through the source calendar's events */
  40. for(var i = 0; i < sourceEvents.length; i++){
  41. var sourceEvent = sourceEvents[i];
  42. var sourceTitle = sourceEvent.getTitle();
  43.  
  44. if(sourceTitle.indexOf(SEARCH_STRING) >= 0 && sourceEvent.isAllDayEvent() ){
  45. var newEvent;
  46. var description = sourceEvent.getDescription();
  47. var startDate = sourceEvent.getStartTime();
  48.  
  49. /* Assumption: Last part of the title is always going to be the time range, separated by at least one space.*/
  50. var titleSplit = sourceTitle.split(" ");
  51. var timeRangeString = "";
  52. if(titleSplit.length > 0){
  53. timeRangeString = titleSplit[titleSplit.length - 1];
  54. }
  55.  
  56. var dateString = startDate.getFullYear() + "/" + (startDate.getMonth() + 1)+ "/" + startDate.getDate() /* format YYYY/M/D */
  57. var createString = "PLACEHOLDER TITLE " + dateString + " " + timeRangeString;
  58.  
  59. /* Attempts to create the new event on the target calendar */
  60. try{
  61. newEvent = targetCal.createEventFromDescription(createString);
  62. Utilities.sleep(50);
  63. }catch(e){
  64. Logger.log(e);
  65. }
  66. /*If successful, renames the new Event */
  67. if(newEvent){
  68. newEvent.setTitle(sourceTitle); // Give the new event the original Title
  69. newEvent.setDescription(description); // Give the new event the original Description.
  70. }
  71.  
  72. }
  73. }
  74.  
  75.  
  76. }
  77.  
  78.  
  79. }
Advertisement
Add Comment
Please, Sign In to add comment