Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google apps script 有条件地更改Google日历事件的颜色_Google Apps Script_Google Calendar Api - Fatal编程技术网

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

Google apps script 有条件地更改Google日历事件的颜色,google-apps-script,google-calendar-api,Google Apps Script,Google Calendar Api,我正在使用下面的代码根据活动名称更改谷歌日历中的颜色 我正在使用我在这里找到的代码片段。它运行,但实际上不会改变颜色。我检查了日志,它似乎一直工作到第70行,这是包含颜色代码的if语句的结尾 function myFunction() { var calendar = "myemail@something.com"; //The name of the calendar you want to modify (WITH quotes) var startDate = new Date("Ap

我正在使用下面的代码根据活动名称更改谷歌日历中的颜色

我正在使用我在这里找到的代码片段。它运行,但实际上不会改变颜色。我检查了日志,它似乎一直工作到第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);
    }
  }
 }
}

我是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);
   }
  } 
 }  
函数ColorEvents(){
var today=新日期();
var nextmonth=新日期();
nextmonth.setDate(nextmonth.getDate()+30);
Logger.log(今天+下一个月);
var calendars=CalendarApp.getOwnedCalendarsByName(“someone@somewhere.com");
Logger.log(“找到的日历数:“+日历.长度”);
var calendar=日历[0];
var events=calendar.getEvents(今天,下个月);

对于(var j=0;jIt显然是行
service.update(事件[i],日历id,事件[i].id)
给你一个错误。你能告诉我是哪一个吗?
try catch
没有运行,因为代码由于错误而无法继续。我尝试了代码,它确实更改了我事件的颜色。问题是,我没有收到错误消息。但是,我发现了我现在使用的另一个代码段。我明白了。你检查了吗d执行记录中的日志
查看?另外,如果您使用的是不同的工作代码段,能否将其作为答案发布?谢谢。
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);
   }
  } 
 }