script not working to bulk-edit the colour of CAlendar events

59 views
Skip to first unread message

Russ De

unread,
Feb 10, 2025, 3:52:47 AM2/10/25
to Google Apps Script Community
Hello, 

I asked ChatGPT to create a script to bulk-edit the colour of specified calendar events, but when it runs, nothing seems to happen. Can anyone help? (The remarks show what it's supposed to do at each step). 

Here's the script:

function updateEventColors() {
  // Get your default calendar (or use CalendarApp.getCalendarById() for a specific one)
  var calendar = CalendarApp.getDefaultCalendar();
 
  // Define a time range (adjust these dates as needed)
  var startTime = new Date("2025-02-09");
  var endTime = new Date("2025-12-29");
 
  // Retrieve events in that range
  var events = calendar.getEvents(startTime, endTime);
 
  // Loop over events
  for (var i = 0; i < events.length; i++) {
    var event = events[i];
    // Check if the event title contains "SchoolTerm" (case-insensitive)
    if (event.getTitle().toLowerCase().indexOf("SchoolTerm") !== -1) {
      // Change the color (choose from the predefined constants, e.g., RED, BLUE, GREEN, etc.)
      event.setColor(CalendarApp.EventColor.GREEN);
    }
  }
 
  Logger.log("Color update complete.");
}



Many thanks!
Russell

Lapiaute

unread,
Feb 10, 2025, 10:04:06 AM2/10/25
to Google Apps Script Community
Les 2 formats de dates sont differents
ils faut que le format de date soit cohérent avec le pays ou du moins identique

var startTime = nouvelle Date ( "09/02/2025" );
  var endTime = nouvelle Date ( "2025-12-29" );

Russ De

unread,
Feb 11, 2025, 6:47:24 AM2/11/25
to Google Apps Script Community
Merci Lapiaute,
J'ai essayé de changer la date pour être cohérent avec mon paye (l'Australie), comme :
  var startTime = new Date("2025/02/09");
  var endTime = new Date("2025/12/29");
mais ca ne marche encore pas. 
Merci beaucoup!

Russ De

unread,
Feb 11, 2025, 3:55:15 PM2/11/25
to Google Apps Script Community
So changing the date format as above doesn't see to work. Any other ideas welcome. 

Russ De

unread,
Feb 11, 2025, 3:56:04 PM2/11/25
to Google Apps Script Community
*seem

Russ De

unread,
Feb 11, 2025, 4:44:21 PM2/11/25
to Google Apps Script Community
It now works: 
I just conversed again with ChatGPT and found the issue it was me having changed the search term to include upper case (ther search term does have upper case, but it needs to be lower case in the "indexOf" command. Also, I introduced a universal date format to be sure. The codes now as follows, and it works. 
Thanks again Lapiaute for taking the time and getting me to think!


function updateEventColors() {
  // Get your default calendar (or use CalendarApp.getCalendarById() for a specific one)
  var calendar = CalendarApp.getDefaultCalendar();
 
  // Define a time range.
  // For example, if you intended February 16-18, 2025, use month 1 for February (0 is January).
  var startTime = new Date(2025, 1, 16); // February 16, 2025
  var endTime = new Date(2025, 1, 18);   // February 18, 2025
 
  // Retrieve events in that range.
  var events = calendar.getEvents(startTime, endTime);
  Logger.log("Found " + events.length + " events.");
 
  // Loop over events.
  for (var i = 0; i < events.length; i++) {
    var event = events[i];
    // Convert the title to lowercase and check for the substring "schoolterm".
    if (event.getTitle().toLowerCase().indexOf("schoolterm") !== -1) {
      Logger.log("Updating event: " + event.getTitle());
      event.setColor(CalendarApp.EventColor.GREEN);
    }
  }
 
  Logger.log("Color update complete.");
}
Reply all
Reply to author
Forward
0 new messages