Automatically changing Event Color in google calendar; more than one event triggers same execution

369 views
Skip to first unread message

Julius Emmanuel Davis

unread,
Aug 24, 2020, 4:03:18 PM8/24/20
to Google Apps Script Community
I have no idea what I'm doing, truly. I took a coding class like five years ago so feel free to openly ream/flame roast me below for being bad at this. Ok, so I found a code that lets me do the first part of the above statement;
function ColorEvents() {

  var today = new Date();
  var nextweek = new Date();
  nextweek.setDate(nextweek.getDate() + 7);
  Logger.log(today + " " + nextweek);

  var calendars = CalendarApp.getAllOwnedCalendars();
  Logger.log("found number of calendars: " + calendars.length);

  for (var i=0; i<calendars.length; i++) {
    var calendar = calendars[i];
    var events = calendar.getEvents(today, nextweek);
    for (var j=0; j<events.length; j++) {
      var e = events[j];
      var title = e.getTitle();
      if (title[0] == "[") {
        e.setColor(CalendarApp.EventColor.CYAN);
      }
      if (title[0] == "!") {
        e.setColor(CalendarApp.EventColor.RED);
      }
      if (title[0] == '#') {
        e.setColor(CalendarApp.EventColor.GREEN);
      }
    }
  }
}

it works wonderfully and I changed the symbols to actually be the title of events in my life. Everything worked until I tried to add two events to the same color. I tried to look up the difference between logical AND and OR statements and ive been messing around for about an hour but everything just ends up being yellow. 
Here's what I have... (p.s. big thanks to whoever can help me. script is mad interesting and I may have just found another quarantine hobby)


function ColorEvents() {

  var today = new Date();
  var nextweek = new Date();
  nextweek.setDate(nextweek.getDate() + 7);
  Logger.log(today + " " + nextweek);

  var calendars = CalendarApp.getAllOwnedCalendars();
  Logger.log("found number of calendars: " + calendars.length);

  for (var i=0; i<calendars.length; i++) {
    var calendar = calendars[i];
    var events = calendar.getEvents(today, nextweek);
    for (var j=0; j<events.length; j++) {
      var e = events[j];
      var title = e.getTitle();
      if (title[0-5] == "EDP") {
        e.setColor(CalendarApp.EventColor.PALE_RED);
      }
      
      if (title[0-1] == "Ro" ) {
        e.setColor(CalendarApp.EventColor.PALE_BLUE);
      }
      
      if (title[0-4] == "Sbux") {
        e.setColor(CalendarApp.EventColor.GREEN);
      }
      if (title[0-4] == "Stud") {
        e.setColor(CalendarApp.EventColor.YELLOW)
      }
              
      if (title[0-4] == "Keys") {
        e.setColor(CalendarApp.EventColor.YELLOW)
      }
    }
  }
}

 
 

Cor van Dooren

unread,
Aug 25, 2020, 12:01:28 AM8/25/20
to google-apps-sc...@googlegroups.com

Your are checking a position in the array title (title[0-1]) which in fact is position -1.

Or in case title[0-4] it is -4.

 

You should get de substring from title[0] to test against.

Syntax:  string.substr(start, length)

Example:

var str = "Hello world!";
var res = str.substr(1, 4);

// res is now ello

 

For better explaination : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring

 

Best regards

Cor van Dooren

The Netherlands

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/d879cb49-eb23-4424-b79c-4dabcf5a185eo%40googlegroups.com.

Reply all
Reply to author
Forward
0 new messages