user53423103981023
user53423103981023

Reputation: 85

Changing calendar event color

I'd like to be able to programmatically change the color of calendar events. I was going through this documentation, but don't understand what the "key" is referring to here: https://developers.google.com/google-apps/calendar/v3/reference/colors. Is it the event ID?

I'd also like to know if it's possible to change a calendar event color via a spreadsheet. I have a script to add entries from a spreadsheet to a calendar, but I want to be able to define the colors as well. Please point me to helpful reading material or examples if possible.

Edit 12/20/2015- added working script to add events:

function createEvent() {
var calendarId = 'MY_ID';
var event = {
summary: 'test',
description: 'test desc',
"end": {
"date": "2015-12-21"
},
"start": {
"date": "2015-12-21"
},
colorId: 10
};
event = Calendar.Events.insert(event, calendarId);
Logger.log('Event ID: ' + event.getId());
}

Upvotes: 2

Views: 4369

Answers (3)

Felix Jassler
Felix Jassler

Reputation: 1531

For quick reference I thought I'd chip in and reference Google's ColorEnum documentation as well as list off the available calendar colors here:

 1 Pale Blue
 2 Pale Green
 3 Mauve
 4 Pale Red
 5 Yellow
 6 Orange
 7 Cyan
 8 Gray
 9 Blue
10 Green
11 Red

Upvotes: 1

New_2_Code
New_2_Code

Reputation: 328

Old post, but in case someone is looking for some sample code. The below code has all the information stored in a spreadsheet:

function myFunction() {

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Your Sheet Name")
var index = 2;
var lastRow = sheet.getLastRow();

for (;index <= lastRow; index++){
var title = sheet.getRange(index, 1, 1, 1).getValue();
var startTime = sheet.getRange(index, 2, 1, 1).getValue();
var endTime = sheet.getRange(index, 3, 1, 1).getValue();
var description = sheet.getRange(index, 4, 1, 1).getValue();
var location = sheet.getRange(index, 5, 1, 1).getValue();
var guests = sheet.getRange(index, 6, 1, 1).getValue();
var eventColour = sheet.getRange(index, 7, 1, 1).getValue();
var sendInvites = true;

var calendar = CalendarApp.getCalendarById("your Calendar ID").createEvent(title, startTime, endTime,
{description: description, location: location, guests: guests, sendInvites: sendInvites }).getId();

if (eventColour === "Condition1") CalendarApp.getEventById(calendar).setColor("10")
if (eventColour === "Condition2") CalendarApp.getEventById(calendar).setColor("11")
if (eventColour === "Condition3") CalendarApp.getEventById(calendar).setColor("12")
if (eventColour === "Condition4") CalendarApp.getEventById(calendar).setColor("13")


}// End of For Loop
}// End of Function

Upvotes: 0

Jens Astrup
Jens Astrup

Reputation: 2454

Oh, are you confused by Google's lack of documentation on this? Don't worry, so was I. Here's a chart of the numbers (keys) and the associated colors. Looks something like this in the end:

var event = {
    summary: "Summarizing",
    description: "Descriptive",
    start: {
      date: Utilities.formatDate(start, "GMT-5", "yyyy-MM-dd")
    },
    end: {
      date: Utilities.formatDate(end, "GMT-5", "yyyy-MM-dd")
    },
    colorId: 10
  };
  event = Calendar.Events.insert(event, calendarId); 
 }

Hope that helps! For the second part: It should be possible, just make sure you're using the Google Calendar Advanced service. Without more detail about what you're trying to do, it's hard to provide more info :/

Upvotes: 2

Related Questions