【问题标题】:Conditionally change the colour of Google calendar events有条件地更改 Google 日历活动的颜色
【发布时间】:2019-12-09 13:29:59
【问题描述】:

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

我正在使用我在这里找到的代码 sn-p 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 中使用一些东西?

【问题讨论】:

  • 很明显service.update(events[i], calendarId, events[i].id);这行给你一个错误。你能告诉它是哪一个吗? try catch 没有运行,因为代码由于错误而无法继续。我尝试了代码,它确实改变了我的事件的颜色。
  • 就是这样,我没有收到错误消息。但是,我发现了我现在正在使用的不同代码 sn-p。
  • 我明白了。您检查过View &gt; Execution transcript 中的日志吗?另外,如果您使用不同的工作代码 sn-p,您可以将其发布为答案吗?谢谢。

标签: google-apps-script google-calendar-api


【解决方案1】:

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

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);
   }
  } 
 }  

【讨论】:

    猜你喜欢
    • 2012-01-03
    • 1970-01-01
    • 2016-03-16
    • 2016-12-12
    • 1970-01-01
    • 1970-01-01
    • 2023-03-09
    • 1970-01-01
    • 2015-09-14
    相关资源
    最近更新 更多