我能够根据Enum事件颜色(https://developers.google.com/apps-script/reference/calendar/event-color)更改事件颜色,但是我想传递一个变量,该变量包含用户选择的Enum事件颜色。如果运行以下代码,则会收到错误Invalid参数:undefined,因此似乎找不到var greenEv。是否可以使用变量代替硬编码的颜色?
[我看过其他一些似乎使用colorId的方法,但是我认为它已被Google淘汰,因为我在他们的文档中找不到任何内容。
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
}
}
}
一个简单的if语句解决了这个问题:
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);
}