Lucas1283
Lucas1283

Reputation: 69

How do I include the original color when I copy an event on Google Calendar using Apps script?

I want to copy a whole week of events into the next one using Apps-Script instead of selecting the recurrence option when creating by hand the event.

The code is shown below.

I can't figure out how to get the color of the original event and pass it to the new event.

Using e.setColor seems to work but how do I pass the color of the original event ?

function batchCopy() {
  var id = '';
  var cal = CalendarApp.getCalendarById(id);
  var startPeriod = new Date('2019-11-04T00:00:00.000Z');
  var endPeriod = new Date('2019-11-05T00:00:00.000Z');
  var events = cal.getEvents(startPeriod, endPeriod);
  for (var i = 0; i < events.length; i++) { 
    var event = events[i];
    var title = event.getTitle();
    var start = tweak(event.getStartTime());
    var finish = tweak(event.getEndTime());
    var e = cal.createEvent(title, start, finish);
    //e.setColor(event.colorId.background);
    Utilities.sleep(1000); 
  }
}

function tweak(date) {
  var tweaked = new Date(date.getTime());
  tweaked.setDate(date.getDate() + 7);
  return tweaked;  
}

Thanking you all in advance for your help.

Upvotes: 1

Views: 84

Answers (1)

Tanaike
Tanaike

Reputation: 201493

  • You want to copy the color of the event.

If my understanding is correct, how about this modification?

Modified script:

From:
var e = cal.createEvent(title, start, finish);
//e.setColor(event.colorId.background);
To:
var e = cal.createEvent(title, start, finish);
e.setColor(event.getColor()); // Modified

References:

If I misunderstood your question and this was not the result you want, I apologize.

Upvotes: 1

Related Questions