Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Kludged Google Apps Script to attempt to turn all-day events with titles like "Bob 10-6" into time spanning calendar events.
- Searching between the first day of last month and the last day of next month, it should:
- * Optionally (If CLEAR_CALENDAR_IN_RANGE = true) delete all events on the calendar TARGET_CALENDAR_NAME in the range
- * Find all calendar events in the owned/subscribed calendar named by SOURCE_CALENDAR_NAME
- * Create an event on the owned calendar named by TARGET_CALENDAR_NAME on the same day using the QuickAdd syntax to input time information.
- *
- */
- var SOURCE_CALENDAR_NAME = "TestCal2";
- var TARGET_CALENDAR_NAME = "TestCal";
- var SEARCH_STRING = "Bob"; /* Name to find in Source Calendar event list titles*/
- var NOW = new Date();
- var START_SEARCH_DATE = new Date(NOW.getFullYear(), NOW.getMonth() - 1, 1); //Midnight before First day of last month
- var END_SEARCH_DATE = new Date(NOW.getFullYear(), NOW.getMonth() + 2, 1); //Midnight After last day of next month
- 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 */
- function CalendarCopier_Main() {
- var sourceCalendars = CalendarApp.getCalendarsByName(SOURCE_CALENDAR_NAME);
- var targetCalendars = CalendarApp.getCalendarsByName(TARGET_CALENDAR_NAME);
- if (sourceCalendars && targetCalendars && sourceCalendars.length > 0 && targetCalendars.length > 0 ){
- sourceCal = sourceCalendars[0];
- targetCal = targetCalendars[0];
- var targetEvents = targetCal.getEvents(START_SEARCH_DATE,END_SEARCH_DATE);
- var sourceEvents = sourceCal.getEvents(START_SEARCH_DATE,END_SEARCH_DATE);
- /*Loop through the Target Calendar and delete its events in the range. */
- if(CLEAR_CALENDAR_IN_RANGE){
- for(var i = 0 ; i < targetEvents.length; i++){
- var targetEvent = targetEvents[i];
- targetEvent.deleteEvent();
- }
- }
- /*Loop through the source calendar's events */
- for(var i = 0; i < sourceEvents.length; i++){
- var sourceEvent = sourceEvents[i];
- var sourceTitle = sourceEvent.getTitle();
- if(sourceTitle.indexOf(SEARCH_STRING) >= 0 && sourceEvent.isAllDayEvent() ){
- var newEvent;
- var description = sourceEvent.getDescription();
- var startDate = sourceEvent.getStartTime();
- /* Assumption: Last part of the title is always going to be the time range, separated by at least one space.*/
- var titleSplit = sourceTitle.split(" ");
- var timeRangeString = "";
- if(titleSplit.length > 0){
- timeRangeString = titleSplit[titleSplit.length - 1];
- }
- var dateString = startDate.getFullYear() + "/" + (startDate.getMonth() + 1)+ "/" + startDate.getDate() /* format YYYY/M/D */
- var createString = "PLACEHOLDER TITLE " + dateString + " " + timeRangeString;
- /* Attempts to create the new event on the target calendar */
- try{
- newEvent = targetCal.createEventFromDescription(createString);
- Utilities.sleep(50);
- }catch(e){
- Logger.log(e);
- }
- /*If successful, renames the new Event */
- if(newEvent){
- newEvent.setTitle(sourceTitle); // Give the new event the original Title
- newEvent.setDescription(description); // Give the new event the original Description.
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment