How can I copy (duplicate) a calendar event into another calendar using Google Script?

后端 未结 2 450
悲&欢浪女
悲&欢浪女 2021-01-06 11:57

I have several calendars in Google Calendar. I\'m learning Google Script and would like to create a script that copies an event from one of my calendars to another with a ch

相关标签:
2条回答
  • 2021-01-06 12:09

    Some code to start with:

    function myFunction() {
      var calendarSource = CalendarApp.getCalendarById("calendarID");
      var calendarDestination = CalendarApp.getCalendarById("calendarID");
      var eventToCopy = calendarSource.getEvents(new Date("July 21, 2009 EST"), new Date("July 22, 2009 EST"));
    
      //read up: https://developers.google.com/apps-script/class_recurrence
      var newRecurrence = CalendarApp.newRecurrence().addWeeklyRule().times(10);
    
      for (var i in eventToCopy){
        if (eventToCopy[i].getTitle() == "event name"){
          var newEvent = calendarDestination.createEventSeries(eventToCopy[i].getTitle(), eventToCopy[i].getStartTime(), eventToCopy[i].getEndTime(), newRecurrence);
        }
    
    }
    

    this should get you started,

    0 讨论(0)
  • 2021-01-06 12:16

    This is an old thread but here is how to get the recurrence field:

    function test_allEventsFromTo() {
      var events = allEventsFromTo("primary", "1 feb 2018", "1 jul 2018");
    
      //display
      Logger.log("Events #%s\n", events.length);
      for (var i = 0; i < events.length; i++) {
        Logger.log("Title: %s, From: %s, To: %s, recurrence: %s", 
                    events[i].summary, events[i].start.dateTime, events[i].end.dateTime, events[i].recurrence);
      }
    }
    
    
    function allEventsFromTo(calendarId, startDate, endDate) { 
      //filter setup
      var optionalArgs = {
        timeMin: (new Date(startDate)).toISOString(),
        timeMax: (new Date(endDate)).toISOString(),
        showDeleted: false,
        singleEvents: false,  //true expands recurring events
      };
    
      //Filter by calendarId and date range
      var response = Calendar.Events.list(calendarId, optionalArgs);
    
      return response.items;
    }
    

    More info on Events:

    Event, resource representation: https://developers.google.com/calendar/v3/reference/events

    Recurring field string format: https://developers.google.com/calendar/concepts/events-calendars#recurring_events

    0 讨论(0)
提交回复
热议问题