GAS-ICS-Sync
GAS-ICS-Sync copied to clipboard
Cancelled events on same day sometimes not being removed
First of all, this script is awesome and is working almost perfectly. I just had a follow-up question related to a thread that has already been resolved. When I first set this up, any events in the past were being removed from my Google Cal. I then read #109 and added this which then kept the old events. However, as mentioned in the thread, sometimes when something is adjusted or deleted on the same day, it does not delete from the Google Cal. Sometimes it does, sometimes not. Is there any way for this to be fixed so that even if something is deleted on the same day, the change reflects in the Google Cal?
Could you give more details on when this happens? Maybe it's always all-day events or instances of a recurring event series?
These events start in the future, are ongoing or already ended when the script is supposed to remove them?
Yes for sure, thanks for your reply. This is not only all-day events, or recurring events. It seems to happen more when the time is approaching, so for example if there is an event in 1 hours time, but that event is removed from the source, it seems to find it difficult to remove it. Almost as if that event is already 'saved' after using #109.
Below I have attached a screenshot of the calendar with an example. The calendar events in question are in orange. So you can see that all past events are being saved which is fantastic. However, on Saturday 4th, the event that says James Hazel, this is saved however it was removed from the source an hour or 2 hours before it happened. However, it has still saved.
It always works correctly unless its on the day of, then it seems to sometimes have this issue. Any potential fix would be awesome.

Thanks!
A quick rundown of what i suspect is the reason for this: (Code as in #109)
calEventEnd = new ICAL.Time.fromString(calendarEvents[i].end.dateTime);
calendarEvents[i].end.dateTime is a string like "2022-06-09T01:30:00+02:00"
however,
ICAL.Time.fromString() does not honor the UTC offset at the end of the String
therefore
calEventEnd ends up as 9th June, 01:30 UTC / +00:00
which finally leads to the following check returning true even though the event has not ended yet
calEventEnd.compare(startUpdateTime) < 0
tl;dr: the event does not get removed because the script falsely thinks the event has ended.
Solution:
in code.gs line 171 and 177 add
, timeZone: "etc/GMT"
after
maxResults: 2500
Okay great I will give this a go, all makes sense. Out of interest, does it make a difference that the timezone I am using for the calendar is Pacific Time? Does that change the fix or I should still use the same as you said.
Also, here are the lines I see. Do these lines still correspond to where I add your fixes. 171 and 177?

Do these lines still correspond to where I add your fixes
Yes.
Does this now look correct? Sorry I am not very good at all this.
Also, the fix is the same considering I am in Canada on PT time not GMT?
Thanks!

Interestingly, I noticed after adding the extra code that it deleted the next two events that passed instead of saving them. Not sure if that is something I did wrong or just bad timing or something. I won't be able to see if it happens again until tomorrow.
The two in the trash were not deleted by me, and still show in the source calendar so have been removed automatically.

I did another test, with the additional code I added:
Solution: add , timeZone: "etc/GMT" after maxResults: 2500
It is now deleting every event once it passes it rather than keeping them. Any idea why? It does seem like it's fixed the original problem but now this new one.
Hmm it seems to be working just fine for me.
Let's try this: Revert the changes just made and use this instead of the one from #109
function processEventCleanup(){
for (var i = 0; i < calendarEvents.length; i++){
var currentID = calendarEventsIds[i];
var feedIndex = icsEventsIds.indexOf(currentID);
var calEventEnd;
if (calendarEvents[i].end.date){
calEventEnd = new ICAL.Time.fromString(calendarEvents[i].end.date);
}
else{
calEventEnd = new ICAL.Time.fromJSDate(new Date(calendarEvents[i].end.dateTime));
}
if(feedIndex == -1 && calendarEvents[i].recurringEventId == null && !(keepPastEvents && calEventEnd.compare(startUpdateTime) < 0)){
Logger.log("Deleting old event " + currentID);
try{
Calendar.Events.remove(targetCalendarId, calendarEvents[i].id);
}
catch (err){
Logger.log(err);
}
}
}
}
Okay so I followed your instructions and reverted the changes made to lines 171 and 177. I then added your code above in replacement to the code I added from #109.
This is now still removing events that have passed. See screenshot example below. I would be totally happy to send you the whole code or give you access if that is possible.

FYI the example that was removed was a booking called Linda Henderson Janet Burgess and it was for 1pm.
Another example, the next two events that you saw visible above, now gone after the start time has passed.

@jonas0b1011001 , just wanted to check in here to see if you might have a solution to the symptoms above? If there is no fix, not to worry, I will just move on with it how it was. I really appreciate your help!
I have not had time to look into this again, sorry.
As mentioned, the proposed changes fixed the error i was able to reproduce on my end. I will have to come up with a different solution to find/fix the error you are experiencing.
@jonas0b1011001, not a problem at all. I also tried starting fresh and following your steps and it for sure fixed the original issue about events not being removed on the same day. However, it still was removing each event after the start time passes rather than keeping them which for me is important. I am sure you are super busy but when you get the time it would be great to have a fix. I will keep checking here. Thanks again!
@jonas0b1011001 , just wanted to check in here in case it was one that had been forgotten about. Any update on a potential solution? I have been using this and its been great, just this one issue that would be amazing to fix. Thanks!
Since it seems like this is still happening (?) I'm going to transfer to a discussion (our new forum for support). If it's no longer happening, please close out.