1

[NOTE: updated to better reflect understanding of the issue]

I am trying to move all events from one Google calendar to another using Google Scripts.

I have written the following code:

function move_all_events()
{
  var fromYear = 2016;
  var toYear=2020
  var fromDate = new Date(fromYear,0,1,0,0,0); //Year, Month (where 0 = Jan, 1 = Feb...), Hour, Min, Sec, Millisecond
  var toDate = new Date(toYear,1,0,0,0);
  var fromCalendarName = 'Calendar1';
  var toCalendarName = 'Calendar2';

  // Move from start of fromYear to start of toYear (for month 0 = Jan, 1 = Feb...)

  var fromCalendar = CalendarApp.getCalendarsByName(fromCalendarName)[0];
  var toCalendar = CalendarApp.getCalendarsByName(toCalendarName)[0];

  var fromCalendarId = fromCalendar.getId();
  var toCalendarId = toCalendar.getId();
  Logger.log(fromCalendarId);
  Logger.log(toCalendarId);

  var events = fromCalendar.getEvents(fromDate, toDate);
  for(var i=0; i<events.length;i++){
    var evId = events[i].getId().replace("@google.com", "");
    Logger.log(evId); // show event Id in log
    Calendar.Events.move(fromCalendarId, evId, toCalendarId);  
  }
}

The above code WORKS fine when the events to-be-moved have been created manually from within Google Calendar.

However, the code FAILS when the events to-be-moved were created externally in an ical/ics file and then IMPORTED into Google calendar.

When I run the code, I get the following error:

API call to calendar.events.move failed with error: Not Found (line 86, file "Code")

where line 86 is the line with Calendar.Events.move.

Note the log shows:

[20-04-24 01:26:15:031 EDT] myname@gmail.com
[20-04-24 01:26:15:032 EDT] asfdsfdsfdsfekwlkkllkpauc700@group.calendar.google.com
[20-04-24 01:26:15:986 EDT] myeventidprefix-a3a597f53ff059959e950fc513df33af

All seems "correct" and indeed the event Id is exactly the UID that I created externally in the ics file that I then imported.

Yet somehow, it seems that Calendar.Events.move is not able to find the event itself in the fromCalendar.

Any idea what is going wrong here?

A SECOND problem that I noted is that even when the routine works (i.e. for events created within Google Calendar), the 'notifications' are not moved. I.e., if I create an event in fromCalendar with notifications, the moved events in toCalendar have lost the notification. Note that neither calendar has Event or 'All Day' notifications turned-on generally.

Interestingly, if I move that same event back to fromCalendar, the notifications magically reappear.

So any idea why the notifications are seemingly still internally intact but are not visible along with the other event data

puterboy
  • 11
  • 2
  • Is this thread useful for your situation? https://stackoverflow.com/q/48134986/7108653 – Tanaike Apr 24 '20 at 05:39
  • Unfortunately that doesn't help. My events don't have the extra "@google.com" text in them. So adding .replace("@google.com", "") doesn't do anything. – puterboy Apr 24 '20 at 06:30
  • Note that I just found that my code *does* work if I am moving from a *non-default* calendar. But it fails when the "from" calendar is the default calendar "myname@gmail.com" – puterboy Apr 24 '20 at 06:32
  • Thank you for replying. I deeply apologize that my comment was not useful for your situation. When the event IDs for the default calendar and additional calendars are retrieved by `getId()`, the value like `#####@google.com` is returned. So when `Calendar.Events.move(fromCalendarId, ev.getId(), toCalendarId);` is used, the error of your issue occurs. When `Calendar.Events.move(fromCalendarId, ev.getId().replace("@google.com", ""), toCalendarId);` is used, no error occurs for `fromCalendar` of both the default calendar and others. I apologize I couldn't replicate your issue. – Tanaike Apr 24 '20 at 06:44
  • So, can you provide the detail flow for replicating your current issue by reflecting my comment? By this, I would like to confirm it. – Tanaike Apr 24 '20 at 06:45
  • After some more troubleshooting, I see the issue is not whether the calendar is the default one or not. Rather, it works when I am moving events created with Google Calendar but the error I cited occurs when I try to move events that were imported ics (ical) files. So, the problem is with imported events only. Any idea why that should be so??? – puterboy Apr 24 '20 at 07:29
  • I also discovered that "moving" events *loses* their associated notifications -- whereas "copying" them via the standard calendar.google.com web interface does not lose the notification. (interestingly, the notifications are restored if I move the event back to the original calendar) Is there any way to make sure the notifications are also moved? If not is there a command analogous to Calendar.Events.move for *copying* so that i could copy each event to the new calendar and then go back and delete the original event? – puterboy Apr 24 '20 at 07:30
  • And to clarify, I did add the clause .replace("@google.com", ""). Note however that the @google.com" is only added for natively created google events. My imported events have the ID that I created for them in the imported ics file as the UID field. Perhaps, imported events need to be referenced differently? Do they have a separate unique Google ID that is not the imported UID? – puterboy Apr 24 '20 at 07:40
  • Thank you for replying. Unfortunately, I couldn't understand about your current situation. This is due to my poor English skill. I deeply apologize for this. So can you update your question by including the detail information? By this, I think that it will help users including me think of the solution. – Tanaike Apr 24 '20 at 11:02
  • Note that I updated the original post to make it clearer what the exact issues are. Sorry for any confusion. – puterboy Apr 24 '20 at 13:54
  • Thank you for replying. I'm glad your issue was resolved. – Tanaike Apr 24 '20 at 23:45

1 Answers1

0

OK - I solved the first part. The problem is that for imported iCal events, the iCal UID is not the same as the Google event ID. In particular, .getId() returns the iCal UID, while the function Calendar.Events.move needs the Google event ID which can be retrieved via: Calendar.Events.list(fromCalendarId).items

See: How can I find the Event Id of my Google Calendar event?

This of course is not an issue for native events since the ID's are the same which explains why my original code worked for native events.

Revised code is as follows:

 for(var i=0; i<events.length; i++){
    var evId = events[i].id;
    Logger.log(i + ": " + evId); // show event Id in log
    Calendar.Events.move(fromCalendarId, evId, toCalendarId);  //Note need to turn on Calendar API under Resources
  }

This however moves some but not all of the events which is weird... and I don't see any pattern... Indeed, among many similar events some are moved and others aren't.

(Also, as noted above, the notifications don't move over as mentioned before.)

puterboy
  • 11
  • 2
  • Note that it looks like I can get it all copied if I do some "throttling" on the event for-loop. For example: `if (i % 25 == 0) {Utilities.sleep(1000);} – puterboy Apr 24 '20 at 21:17