Search code examples
javascriptgoogle-apps-scriptgoogle-calendar-apigoogle-apps

How can I get color from CalendarEvent object on google apps script?


I want to get the color(red) below the picture.

enter image description here


I use next code, but I don't know next step.

run main function.

var mainCalendarName = 'main';

function main() {
    var calendar = getCalendar();

    if (calendar == null) {
        return;
    }

    var now = new Date();
    var calendarEventArray = calendar.getEventsForDay(now);

    Logger.log('current color = ' + calendarEventArray[0].getColor()); // not use!!!
    //log 'current color = #FF0000'
}

function getCalendar() {
    var calendarList = CalendarApp.getAllCalendars();

    for (i in calendarList) {
        if (mainCalendarName === calendarList[i].getName()) {
            return calendarList[i];
        }
    }
    return null;
}

Solution

  • First of all you need to enable the Advanced Google Services.

    Please see here description how do that.

    Then the following code will do the job

    function main(){
      var now = new Date();
      var events = Calendar.Events.list("main", {
        timeMin: now.toISOString(),
        singleEvents: true,
        orderBy: 'startTime',
        maxResults: 10
        });
        for (var i = 0; i < events.items.length; i++) {
           Logger.log(events.items[i].colorId); //Here the color of the specific event
        }
    }