Guest User

Untitled

a guest
Oct 18th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.28 KB | None | 0 0
  1. /*
  2. * Copyright: Dale Stevens 2018
  3. *
  4. * About:
  5. * The script below will pull events from the calendar of your choice, and add them to the
  6. * primary calendar of the account you employ this script under.
  7. * There are a couple "knobs" (variables) at the top of the script to assist in tweeking
  8. * to your liking.
  9. *
  10. * Setup:
  11. * 1. Go to your Google Drive under your desired account (I recommend your "work" account).
  12. * 2. Create a new Google Apps Script under "more". (you may need to "enable" this service by going to "connect more apps")
  13. * 3. Paste the code below into 'code.gs'. Save.
  14. * 4. Click the "clock" icon to setup a new trigger.
  15. * 5. Choose "Run: sync"
  16. * 6. Choose "Events: From calendar, Event Updated"
  17. * 7. Enter your calendar ID (usually your personal email address) in the text box.
  18. * 8. Save.
  19. * 9. Run.
  20. * 10. You may need be prompted to authorize and sign-in to your account. Do this for your primary calendar (the account you're on)
  21. */
  22.  
  23. var calendarId="YOUR CALENDAR ID HERE"; // Typically this is your personal gmail address "someone@somewhere.com"
  24. var days_in_advance = 14; // how many days in advance to monitor and block off time
  25. var eventPrefix="BLOCKED"; // update this to the text you'd like to appear in the new events created in primary calendar
  26. var weekdays_only = false;
  27.  
  28. function copy_event(event, to_calendar) {
  29. new_event = to_calendar.createEvent(event.getTitle(),event.getStartTime(),event.getEndTime()); // change the Booked text to whatever you would like your merged event titles to be
  30. new_event.removeAllReminders(); // so you don't get double notifications. Delete this if you want to keep the default reminders for your newly created primary calendar events
  31. new_event.setTag(calendarId, event.getId());
  32. return new_event;
  33. }
  34.  
  35. function update_event(pEvent, sEvent) {
  36. pEvent.setTitle(eventPrefix+" ("+sEvent.getTitle()+")");
  37. pEvent.setDescription(sEvent.getDescription());
  38. pEvent.setTime(sEvent.getStartTime(), sEvent.getEndTime());
  39. pEvent.setVisibility(CalendarApp.Visibility.PRIVATE); // set blocked time as private appointments in work calendar
  40. }
  41.  
  42. function is_on_weekday(event) {
  43. var day = event.getStartTime().getDay();
  44. return (day > 0 && day < 6);
  45. }
  46.  
  47. function sync() {
  48.  
  49. var today=new Date();
  50. var enddate=new Date();
  51. enddate.setDate(today.getDate()+days_in_advance);
  52.  
  53. var secondaryCal=CalendarApp.getCalendarById(calendarId);
  54. var secondaryEvents=secondaryCal.getEvents(today,enddate);
  55.  
  56. var primaryCal=CalendarApp.getDefaultCalendar();
  57. var primaryEvents=primaryCal.getEvents(today,enddate); // all primary calendar events
  58.  
  59. var primaryEventsFiltered = {}; // to contain primary calendar events that were previously created from secondary calendar
  60. var primaryEventsUpdated = []; // to contain primary calendar events that were updated from secondary calendar
  61. var primaryEventsCreated = []; // to contain primary calendar events that were created from secondary calendar
  62.  
  63. Logger.log('Number of primaryEvents: ' + primaryEvents.length);
  64. Logger.log('Number of secondaryEvents: ' + secondaryEvents.length);
  65.  
  66. // create filtered list of existing primary calendar events that were previously created from the secondary calendar
  67. for each (var pEvent in primaryEvents)
  68. {
  69. if ((pid = pEvent.getTag(calendarId)) != null) {
  70. primaryEventsFiltered[pid] = pEvent;
  71. }
  72. }
  73.  
  74. // process all events in secondary calendar
  75. for each (var sEvent in secondaryEvents)
  76. {
  77. var pEvent = null;
  78. var sId = sEvent.getId();
  79. // if the secondary event has already been blocked in the primary calendar, update it
  80. if ((pEvent = primaryEventsFiltered[sId]) != null)
  81. {
  82. delete primaryEventsFiltered[sId];
  83. // primaryEventsUpdated.push(pEvent.getId());
  84. } else {
  85. if (!weekdays_only || is_on_weekday(sEvent)) {
  86. pEvent = copy_event(sEvent, primaryCal);
  87. primaryEventsCreated.push(pEvent.getId());
  88. }
  89. }
  90.  
  91. update_event(pEvent, sEvent);
  92. // TODO: Handle All-day events
  93. // if (evi.isAllDayEvent())
  94. // {
  95. // return; // Do nothing if the event is an all-day or multi-day event. This script only syncs hour-based events
  96. // }
  97. }
  98.  
  99. // if a primary event previously created no longer exists in the secondary calendar, delete it
  100. for each (var pEvent in primaryEventsFiltered)
  101. {
  102. pEvent.deleteEvent();
  103. }
  104.  
  105. }
Add Comment
Please, Sign In to add comment