Apps Script Archives - ISbyR https://isbyr.com/tag/apps-script/ Infrequent Smarts by Reshetnikov Sat, 03 Sep 2022 13:00:40 +0000 en-US hourly 1 https://wordpress.org/?v=6.7.1 Colour code Google calendar events automatically using Google Apps Script https://isbyr.com/colour-code-google-calendar-events-automatically-using-google-apps-script/ https://isbyr.com/colour-code-google-calendar-events-automatically-using-google-apps-script/#comments Thu, 21 Feb 2019 05:43:04 +0000 http://isbyr.com/?p=406 Sometimes you want to have a certain colour for the Google Calendar events. I know that you can do it manually, but what if you want it to be colour coded automatically based on some filters? In my case, it was based on a meeting organizer (my wife to be precise 🙂 ). So decided … Continue reading Colour code Google calendar events automatically using Google Apps Script

The post Colour code Google calendar events automatically using Google Apps Script appeared first on ISbyR.

]]>
Sometimes you want to have a certain colour for the Google Calendar events. I know that you can do it manually, but what if you want it to be colour coded automatically based on some filters? In my case, it was based on a meeting organizer (my wife to be precise 🙂 ). So decided to try and colour code Google calendar events automatically using Google Apps Script.

Creating the Google Apps Script

Go to G-Suite Developers Hub
Start a new Project

In the new Project create a new Script file (let’s call it code.gs)
Now let’s paste the below code

Most of the code is around the Google Apps script Calendar Event class

/**
 * Lists 10 upcoming events in the user's calendar.
 */
function listUpcomingEvents() {
  var filterCreatorEmail = 'mywife@gmail.com';
  var filterColorId = '10';
  var calendarId = 'primary';
  var optionalArgs = {
    timeMin: (new Date()).toISOString(),
    showDeleted: false,
    singleEvents: true,
    maxResults: 10,
    orderBy: 'startTime'
  };
  var response = Calendar.Events.list(calendarId, optionalArgs);
  var events = response.items;
  
  
  if (events.length > 0) {
    for (i = 0; i < events.length; i++) {
      var event = events[i];
      var when = event.start.dateTime;
      if (!when) {
        when = event.start.date;
      }
      if (event.creator.email == filterCreatorEmail) {
        Logger.log('%s %s %s (%s)', event.creator.email, event.colorId, event.summary, when);
        if (event.colorId != filterColorId){
          Logger.log('Updating color from %s to %s',event.colorId, filterColorId);
          event.colorId = filterColorId;
          Calendar.Events.patch(event, 'primary', event.id)
        }
      }
     //  Logger.log(JSON.stringify(event));
  }
  }
 else {
    Logger.log('No upcoming events found.');
  }
}
 

That’s it you are almost ready to try running the script. When you will run it for the first time Goggle will ask you for some permissions.

Go to View > Log to see the output of the script. Here is an example

[19-02-20 12:00:15:296 PST] mywife@gmail.com undefined 12-3 doabcdefgy (2019-02-24T12:00:00+11:00)
[19-02-20 12:00:15:296 PST] Updating color from undefined to 10
[19-02-20 12:00:15:594 PST] mywife@gmail.com undefined 11-2 Aabcdne  (2019-02-25T11:00:00+11:00)
[19-02-20 12:00:15:594 PST] Updating color from undefined to 10
[19-02-20 12:00:15:979 PST] mywife@gmail.com undefined 6:15 fashion forum  (2019-02-26T17:30:00+11:00)
[19-02-20 12:00:15:980 PST] Updating color from undefined to 10

And the colour of the events (organized by my wife) in the calendar changed from the boring default colour

Google Calendar before update

To a nice basil one.

Google Calendar after update by Google Apps Script

Adding Triggers

Well, we successfully ran it once, but how will it continuously / periodically run. That’s what Triggers are for.

You can access it in 2 ways

  • While you are on the project page click the little clock button (Current project’s triggers)
  • On the main G-Suite Developers Hub page click the … for your project and the Triggers.

Since I wanted my Google Apps script to run every time the calendar is been updated, here is my trigger condition

Google Apps Script Trigger Condition Screenshot

That’s it. Now every time my calendar is updated the Goole Apps script runs and updates the colour of events that were organized by someone (very special in my case :-)).

P.S.

You don’t have to have G-Suite to have the Google App Script functionality. On the other hand, if you do want to sign-up for G-Suite ping me on twitter @IlyaReshet to get 20% off your first year of G Suite Basic or G Suite Business

The post Colour code Google calendar events automatically using Google Apps Script appeared first on ISbyR.

]]>
https://isbyr.com/colour-code-google-calendar-events-automatically-using-google-apps-script/feed/ 6