Advertisement
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 calendars with names listed in SOURCE_CALENDAR_LIST
- * 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_LIST = ["SourceCal1","SourceCal2","SourceCal3","SourceCal4"];
- 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 = false; /* ONLY SET THIS TO TRUE IF YOU ARE SURE YOU WANT TO ERASE EVERYTING IN THE TARGET CALENDAR IN THE RANGE */
- function CalendarCopier_Insane_Main() {
- /* Find all the calendars in the list */
- var sourceCalendars = [];
- for(var i = 0; i < SOURCE_CALENDAR_LIST.length; i++){
- var cals = CalendarApp.getCalendarsByName(SOURCE_CALENDAR_LIST[i]);
- if (cals && cals.length > 0){
- sourceCalendars.push(cals[0]);
- }
- }
- var targetCalendars = CalendarApp.getCalendarsByName(TARGET_CALENDAR_NAME);
- /* Ensure that we have at least one source calendar, and at least one target calendar*/
- if (targetCalendars && sourceCalendars && targetCalendars.length > 0 && sourceCalendars.length > 0){
- targetCal = targetCalendars[0];
- var targetEvents = targetCal.getEvents(START_SEARCH_DATE,END_SEARCH_DATE);
- /*Loop through the Target Calendar and delete its events in the range, if desired */
- if(CLEAR_CALENDAR_IN_RANGE){
- for(var i = 0 ; i < targetEvents.length; i++){
- var targetEvent = targetEvents[i];
- targetEvent.deleteEvent();
- }
- }
- /*Loop through EACH source calendar's events */
- for(var j = 0; j < sourceCalendars.length; j++){
- var sourceCal = sourceCalendars[j];
- var sourceEvents = sourceCal.getEvents(START_SEARCH_DATE,END_SEARCH_DATE)
- 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 = false;
- 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);
- /*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.
- }
- Utilities.sleep(50); /* Hamfisted attempt to avoid API limits by delaying 1/20 of a second, could be done better.*/
- }catch(e){
- Logger.log(e);
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement