How can I copy (duplicate) a calendar event into a

2019-02-19 19:52发布

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 chance to modify parameters such as recurrence.

2条回答
家丑人穷心不美
2楼-- · 2019-02-19 20:26

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,

查看更多
Luminary・发光体
3楼-- · 2019-02-19 20:34

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

查看更多
登录 后发表回答