Google calendar api v3 set color + color chart

After searching for quite a long time (too much for such a thing) I finally found how to set am event color for a calendar event using the new API V3. Also, and before we get to the code, just to have it said, people over on google groups are a little bit like the foruns it self…. poor! I’ve looked at a few threads where this question was asked and people just said “It has been aswered a few times, search it”… C’mon you guys… It’s free info, and keystrokes are relativly as well!!

Right so, I’m not very much of a blogger in the past months, but this I felt I had to have it written on my blog, just because 😉

Well so here it goes. Lets imagine the following sample code being a function.

function addEvent_to_Google($startDate, $startTime, $endDate, $endTime, $weekDay, $recurrent) {
    @session_start();
    require_once dirname(__FILE__) . "/google-api-php-client/src/apiClient.php";
    require_once dirname(__FILE__) . "/google-api-php-client/src/contrib/apiCalendarService.php";

    $apiClient = new apiClient();
    $apiClient->setUseObjects(true);
    $service = new apiCalendarService($apiClient);

    if (isset($_SESSION['g_mat_token'])) {
         $apiClient->setAccessToken($_SESSION['g_mat_token']);
         $event = new Event();
         $event->setSummary('Appointment');
         $event->setLocation('Somewhere');
         $start = new EventDateTime();
         $start->setDateTime(''.$startDate.'T'.$startTime.'');
         $start->setTimeZone('Europe/Lisbon');
         $event->setStart($start);
         $end = new EventDateTime();
         $end->setDateTime(''.$endDate.'T'.$endTime.'');
         $end->setTimeZone('Europe/Lisbon');
         $event->setEnd($end);
         $event->setRecurrence(array('RRULE:FREQ=WEEKLY;UNTIL=20121231'));
         $event->setColorId("11");
         $recurringEvent = $service->events->insert('primary', $event);
         return $recurringEvent->getId();

    } else {
        return "Authorization problem"; // I'm not going trought this much, another war 😉
    }
}

Basically, it’s as simple as $event->setColorId("11"); where the color ID must be one integer value from 1 – 11. Also as a propper “i-can-do-a-research-myself-forums-users-don’t-bother-let-other-people-know-about-your-findings” where is the chart with all the colors, their ID’s and hex equivalents

7 thoughts on “Google calendar api v3 set color + color chart”

    1. Thanks again, now here’s a code hint for you that I found while unlocking these secret Google handshakes…

      this wont work:
      return $recurringEvent->getId();

      In this context, $recurringEvent is an array not an object.

      Use this code and you will have success:
      return $recurringEvent[‘id’];

Leave a Reply

Your email address will not be published. Required fields are marked *