有条件地更改Google日历事件的颜色

人气:1,233 发布:2022-10-16 标签: google-apps-script google-calendar-api

问题描述

我使用以下代码根据活动名称更改Google日历中的颜色。

我使用的是我在这里找到的代码片段https://webapps.stackexchange.com/questions/85625/change-the-color-of-multiple-google-calendar-events。它会运行,但实际上不会改变颜色。我检查了日志,它似乎一直工作到第70行,这是包含颜色代码的if语句的末尾。

function myFunction() {

var calendar = "myemail@something.com"; //The name of the calendar you want to modify (WITH quotes)

var startDate = new Date("Apr 01 GMT 2019"); //The start of the time range in which the events exist

var keyword = "Time sheets"; //The keyword to search for in the event title (WITH quotes; IS case-sensitive)

var where = 0;        //Where to search for events (0 = title; 1 = description)

var color = "blue"; //The color to change the events to (WITH the quotes)


var calendarId = CalendarApp.getCalendarsByName(calendar)[0].getId();

var optionalArgs = {
  timeMin: startDate.toISOString(),
  showDeleted: false,
  singleEvents: true,
  orderBy: 'startTime'
};


var service = Calendar.Events;
var response = Calendar.Events.list(calendarId, optionalArgs);
var events = response.items;

for (i = 0; i < events.length; i++) {    
// Logger.log(events[i].summary);

  if (where == 0)
    var searchResult = events[i].summary.search(keyword);
  else if (where == 1){
    if (events[i].description == undefined)
      continue;

  var searchResult = events[i].description.search(keyword);
}

if (searchResult > -1){
  Logger.log(events[i].summary);


  if (color == "bold blue")
    events[i].colorId = 9;
  else if (color == "blue")
    events[i].colorId = 1;
  else if (color == "turquoise")
    events[i].colorId = 7;
  else if (color == "green")
    events[i].colorId = 2;
  else if (color == "bold green")
    events[i].colorId = 10;
  else if (color == "yellow")
    events[i].colorId = 5;
  else if (color == "orange")
    events[i].colorId = 6;
  else if (color == "red")
    events[i].colorId = 4;
  else if (color == "bold red")
    events[i].colorId = 11;
  else if (color == "purple")
    events[i].colorId = 3;
  else if (color == "gray")
    events[i].colorId = 8;
  Logger.log(events[i].colorId);


  try{
    service.update(events[i], calendarId, events[i].id);
  }
  catch(e){
    Logger.log(e);
    }
  }
 }
}

但是,这些行似乎没有任何作用?

      try{
    service.update(events[i], calendarId, events[i].id);
  }
  catch(e){
    Logger.log(e);

我是Google AppScript新手,我不明白最后一行代码是做什么用的,也许我应该用一些SetColor?

推荐答案

是的,当然。这是一个非常简单的版本,但计划对其进行扩展。

function ColorEvents() {

var today = new Date();
var nextmonth = new Date();
nextmonth.setDate(nextmonth.getDate() + 30);
Logger.log(today + " " + nextmonth);

var calendars = CalendarApp.getOwnedCalendarsByName("someone@somewhere.com");
Logger.log("found number of calendars: " + calendars.length);

var calendar = calendars[0];

var events = calendar.getEvents(today, nextmonth);

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

  var title = e.getTitle();
  Logger.log(title);
  var desc = e.getDescription();


  if (title == "Time sheets") {
    e.setColor(CalendarApp.EventColor.CYAN);
   }
  } 
 }  

475