How can I get the most recent Google Calendar events when using the "get many" option without getting every single event throughout history?

Using the Calendar Node --> Resource: Event --> Operation: Get Many gives old data.

I want to “get” all calendar events due in the current day (24 hours).
I tried doing this with the Get Many option and limiting it to 50 then sorting by date. But all of the events were over a year old…

It looks like the Get Many option with a limit of 50 provides the OLDEST 50 events.

I do NOT want to get all the data and parse it after the fact because that would take a long time (there are many thousands of events since the beginning of the calendar and trying to put this into a code node throws an Error).

Error message

Problem running workflow
Request failed with status code 413

How could I get the calendar events due to arise for a given date?

The official google API docs have timeMax and timeMin arguments. Are we able to use these?

Hi @hdotking, the Google calendar node provides options that you can use. E.g. you might wanna use the Order by option to sort your events from newest to oldes or use the Start/End time option. Have tried to use any of those?
image

Hi @Niklas_Hatje

Yes i’ve tried playing with all of the existing options.

When I try setting Start Time to last week it still shows me emails from 2021. End Time does the same. Using them both returns no events. (see image below for Start Time)

Please advise.

The start/end options are the wrong way around and should be renamed. This is what they actually do:

Start time → Before (at least some part of the event must be before this time)
End time → After (at least some part of the event must be after this time)

Note that if you have recurring events, they will be returned but with the date of the original occurrence, not the occurrence that happens within your window.

Here’s a workflow that fixes this, and also corrects for changes in daylight savings time. (It may not work on n8n versions earlier than 0.216 because of some internal changes we made)

2 Likes

@sirdavidoff Thank you! This works for me and is awesome!

I’m a little wary of using code with comments like this in production:

  • // What we used to do, but not needed any more?

Please please please let me know the timeline of when a refactored, tested and production ready workaround is made available! :smiley:

I’m happy to make edits myself and share the node if y’all can tell me what needs editing/fixing/removing from this code node.

This code was written a while ago but comes from a workflow I use every day. So please ignore that comment

Sure thing. PS: for some reason your code snippet resets the timezone in the timestamp returned to -05.

I fixed it by hardcoding my desired timeZone and using it in the setCurrentDate function.

Here’s the updated code that respects the user’s timezone. It turns out I just needed to add this line of code before .toISO
newDate = newDate.setZone(zoneName)

// Recurring meetings seem to give the original event's start/end, not the current event's
// We get around this by setting the start/end date to the current date (but keeping the original time)
// Obviously this is a bit of a hack and would be better handled in the Google Calendar node itself
// We also need to account for differences in whether we're in DST between the original event and the current time

function setCurrentDate(date) {
  /*var newDate = new Date(date);
  newDate.setDate(new Date().getDate());
  newDate.setMonth(new Date().getMonth());
  newDate.setFullYear(new Date().getFullYear());*/
  var zoneName = "Europe/London"
  var newDate = DateTime.fromISO(date);
  
  newDate = newDate.set({
    day: $now.day,
    month: $now.month,
    year: $now.year
  });
  newDate = newDate.setZone(zoneName)
  return newDate.toISO();
}

// If we're currently in DST and this date isn't, add an hour to it
// If we're not currently in DST and this date is, subtract an hour from it
// To calculate DST we use the timezone that the event is in
function normaliseDST(date, zoneName) {
  var newDate = DateTime.fromISO(date).setZone(zoneName);
  var now = DateTime.now().setZone(zoneName);

  console.log("start", newDate, newDate.isInDST);
  console.log("now", now, now.isInDST);
  
  if(newDate.isInDST && !now.isInDST) {
    console.log("Normalising +1")
    newDate = newDate.plus({hour: 1});
  } else if(!newDate.isInDST && now.isInDST) {
    console.log("Normalising -1")
    newDate = newDate.minus({hour: 1});
  }

  console.log("adjusted", newDate, newDate.isInDST);
  newDate = newDate.setZone(zoneName)
  return newDate.toISO();
}

// Check we haven't been passed an empty item
if (Object.keys(item).length == 0) return out;

// Get the current time in the time zone of the event
/*console.log("startString", item.start.dateTime);
var start = DateTime.fromISO(item.start.dateTime).setZone(item.start.timeZone);
console.log("start", start);
console.log("startisdst", start.isInDST);
console.log("zoneName", start.zoneName);
var now = $now.setZone(start.zoneName);
console.log("now", now);
console.log("nowisdst", now.isInDST);*/

if('start' in item) {
  item.calcStartNormalised = setCurrentDate(normaliseDST(item.start.dateTime, item.start.timeZone)); // What we used to do, but not needed any more?
  item.calcStart = setCurrentDate(item.start.dateTime, item.start.timeZone);
  //item.calcEnd = setCurrentDate(item.end.dateTime);
  console.log(item.start.timeZone)
}

return item;

Good to hear! Thanks for sharing.

By the way -5 is the default timezone for n8n. So another way of fixing this would be to change n8n’s time zone to your desired one.

1 Like

I have just popped in a PR to address this which can be found below, Assuming it is reviewed today / tomorrow it should make it into the next release.

1 Like

@Jon You’re a legend :100:

Can’t wait to use this! I’ll experiment with the new functionality ASAP :smiley:

1 Like

New version [email protected] got released which includes the GitHub PR 5529.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.