Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Copyright: Dale Stevens 2018
- *
- * About:
- * The script below will pull events from the calendar of your choice, and add them to the
- * primary calendar of the account you employ this script under.
- * There are a couple "knobs" (variables) at the top of the script to assist in tweeking
- * to your liking.
- *
- * Setup:
- * 1. Go to your Google Drive under your desired account (I recommend your "work" account).
- * 2. Create a new Google Apps Script under "more". (you may need to "enable" this service by going to "connect more apps")
- * 3. Paste the code below into 'code.gs'. Save.
- * 4. Click the "clock" icon to setup a new trigger.
- * 5. Choose "Run: sync"
- * 6. Choose "Events: From calendar, Event Updated"
- * 7. Enter your calendar ID (usually your personal email address) in the text box.
- * 8. Save.
- * 9. Run.
- * 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)
- */
- var calendarId="YOUR CALENDAR ID HERE"; // Typically this is your personal gmail address "someone@somewhere.com"
- var days_in_advance = 14; // how many days in advance to monitor and block off time
- var eventPrefix="BLOCKED"; // update this to the text you'd like to appear in the new events created in primary calendar
- var weekdays_only = false;
- function copy_event(event, to_calendar) {
- 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
- 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
- new_event.setTag(calendarId, event.getId());
- return new_event;
- }
- function update_event(pEvent, sEvent) {
- pEvent.setTitle(eventPrefix+" ("+sEvent.getTitle()+")");
- pEvent.setDescription(sEvent.getDescription());
- pEvent.setTime(sEvent.getStartTime(), sEvent.getEndTime());
- pEvent.setVisibility(CalendarApp.Visibility.PRIVATE); // set blocked time as private appointments in work calendar
- }
- function is_on_weekday(event) {
- var day = event.getStartTime().getDay();
- return (day > 0 && day < 6);
- }
- function sync() {
- var today=new Date();
- var enddate=new Date();
- enddate.setDate(today.getDate()+days_in_advance);
- var secondaryCal=CalendarApp.getCalendarById(calendarId);
- var secondaryEvents=secondaryCal.getEvents(today,enddate);
- var primaryCal=CalendarApp.getDefaultCalendar();
- var primaryEvents=primaryCal.getEvents(today,enddate); // all primary calendar events
- var primaryEventsFiltered = {}; // to contain primary calendar events that were previously created from secondary calendar
- var primaryEventsUpdated = []; // to contain primary calendar events that were updated from secondary calendar
- var primaryEventsCreated = []; // to contain primary calendar events that were created from secondary calendar
- Logger.log('Number of primaryEvents: ' + primaryEvents.length);
- Logger.log('Number of secondaryEvents: ' + secondaryEvents.length);
- // create filtered list of existing primary calendar events that were previously created from the secondary calendar
- for each (var pEvent in primaryEvents)
- {
- if ((pid = pEvent.getTag(calendarId)) != null) {
- primaryEventsFiltered[pid] = pEvent;
- }
- }
- // process all events in secondary calendar
- for each (var sEvent in secondaryEvents)
- {
- var pEvent = null;
- var sId = sEvent.getId();
- // if the secondary event has already been blocked in the primary calendar, update it
- if ((pEvent = primaryEventsFiltered[sId]) != null)
- {
- delete primaryEventsFiltered[sId];
- // primaryEventsUpdated.push(pEvent.getId());
- } else {
- if (!weekdays_only || is_on_weekday(sEvent)) {
- pEvent = copy_event(sEvent, primaryCal);
- primaryEventsCreated.push(pEvent.getId());
- }
- }
- update_event(pEvent, sEvent);
- // TODO: Handle All-day events
- // if (evi.isAllDayEvent())
- // {
- // return; // Do nothing if the event is an all-day or multi-day event. This script only syncs hour-based events
- // }
- }
- // if a primary event previously created no longer exists in the secondary calendar, delete it
- for each (var pEvent in primaryEventsFiltered)
- {
- pEvent.deleteEvent();
- }
- }
Add Comment
Please, Sign In to add comment