0
votes

I'm able to change an event colour based on Enum Event Colour (https://developers.google.com/apps-script/reference/calendar/event-color), but I'd like to pass through a variable which holds a user-selected Enum Event Colour. If I run the below code, I get the error Invalid argument: undefined, so it doesn't seem to find var greenEv. Is it possible to use a variable instead of a hard-coded colour?

I looked at some other approaches which seem to use colorId, but I think that was phased out by Google as I couldn't find anything in their documentation.

function ColorEvents() {

//test to change colours of past events
var start = new Date();
var end = new Date();
start.setDate(start.getDate() - 11);


var calendars = CalendarApp.getDefaultCalendar();
var calColour = CalendarApp.getColor();
var calName = calendars.getName();
Logger.log("Name of calendar: " + calName);
Logger.log("Colour of calendar: " + calColour);

var events = calendars.getEvents(start, end);
Logger.log("Number of events: " + events.length);

for (var j = 0; j < events.length; j++) {
    var ev = events[j];

    var title = ev.getTitle();
    var desc = ev.getDescription();
    var guestList = ev.getGuestList();


    var internalOnly = 1;

    for (var d = 0; d < guestList.length; d++) {
        var guest = guestList[d];

        var guestName = guest.getEmail();
        if (guestName.indexOf("gmail") == -1) {
            internalOnly = 0;
        }
    }

        var greenEv = "GREEN"; //but this could also be RED depending on what the user selects on the spreadsheet

    //internal event
    if (internalOnly == 1) {
        ev.setColor(CalendarApp.EventColor.greenEv); //this breaks
    }

    //personal
    if (guestList.length == 0) {
        ev.setColor(CalendarApp.EventColor.MAUVE); //this works
    }

}
}
1

1 Answers

0
votes

A simple if statement solves this problem:

        if (intColour == "GREEN") {
    var internalEv = CalendarApp.EventColor.GREEN;
    } else if (intColour == "RED") {
    var internalEv = CalendarApp.EventColor.RED;
    }

    //internal event
    if (internalOnly == 1) {
        ev.setColor(internalEv);
    }