MagicMirror² v2.7.0 is available! For more information about this release, check out this topic.

Getting Google Contacts birthdays into Calendar



  • Hi,

    For those interested in below issue solving :

    • Google Contact birth calendar doesn’t offer .ics so it’s impossible to get it automatically into MM calendar

    • I’ve wrote a quick google script that you can use as below :

    1/ Create an empty personnal “birthday calendar” into Google Agenda (remind the ID of the calendar into calendar parameters for later)

    2/ Create a new google script which you can store into your Google drive

    3/ Paste below code into the script :

    function Sync_Birth_Cal() {
    
      // Calendars adress (ID)
      
      var calendarSource = CalendarApp.getCalendarById("THE ID OF YOUR CONTACT CALENDAR");
      var calendarDestination = CalendarApp.getCalendarById("THE ID OF THE CREATED CALENDAR");
      
      // Start and End Date definition
      
      var Today = new Date();
      var StartDeleteDate = new Date();
      var EndDeleteDate = new Date();
      var StartCopyDate = new Date();
      var EndCopyDate = new Date();
      
      StartDeleteDate.setDate(Today.getDate()-400); 
      EndDeleteDate.setDate(Today.getDate()+400);
      StartCopyDate.setDate(Today.getDate()-360);
      EndCopyDate.setDate(Today.getDate()+360);
     
      
      // first deletes all datas in calendar
      
      
      var eventToDelete = calendarDestination.getEvents(StartDeleteDate, EndDeleteDate);
      
       for (var i = 0; i < eventToDelete.length; i++) {  
        eventToDelete[i].deleteEvent();
        }  
    
    
      // then copy everything again
      
      var eventToCopy = calendarSource.getEvents(StartCopyDate, EndCopyDate);
    
      for (var t in eventToCopy){
        var newEvent = calendarDestination.createEvent(eventToCopy[t].getTitle(), eventToCopy[t].getStartTime(), eventToCopy[t].getEndTime());
      }
      
    }
    

    4/ Into Google script, Edit, Add trigger (choose the one you want, I personally choose to launch that one every 2 hours, but you can do whatever you want)

    5/ You can know use the .ics path of the newly created calendar into your MM calendar

    NOTE : it’s the first time I write that kind of stuff. It works for me until now, but there could be some bugs also. Let’s see.

    NOTE 2 : be careful… part of the script is deleting calendars events on purpose… So don’t mess up…If needed, back up before.

    Regards



  • @bolish said in Getting Google Contacts birthdays into Calendar:

    function Sync_Birth_Cal() {

    // Calendars adress (ID)

    var calendarSource = CalendarApp.getCalendarById(“THE ID OF YOUR CONTACT CALENDAR”);
    var calendarDestination = CalendarApp.getCalendarById(“THE ID OF THE CREATED CALENDAR”);

    // Start and End Date definition

    var Today = new Date();
    var StartDeleteDate = new Date();
    var EndDeleteDate = new Date();
    var StartCopyDate = new Date();
    var EndCopyDate = new Date();

    StartDeleteDate.setDate(Today.getDate()-400);
    EndDeleteDate.setDate(Today.getDate()+400);
    StartCopyDate.setDate(Today.getDate()-360);
    EndCopyDate.setDate(Today.getDate()+360);

    // first deletes all datas in calendar

    var eventToDelete = calendarDestination.getEvents(StartDeleteDate, EndDeleteDate);

    for (var i = 0; i < eventToDelete.length; i++) {
    eventToDelete[i].deleteEvent();
    }

    // then copy everything again

    var eventToCopy = calendarSource.getEvents(StartCopyDate, EndCopyDate);

    for (var t in eventToCopy){
    var newEvent = calendarDestination.createEvent(eventToCopy[t].getTitle(), eventToCopy[t].getStartTime(), eventToCopy[t].getEndTime());
    }

    }

    I get this error when I try to run the script:

    TypeError: Cannot call method “getEvents” of null. (line 25, file “Code”)

    I got that sorted, forgot the 2 IDs.
    Now I get:
    Service invoked too many times in a short time: calendar. Try Utilities.sleep(1000) between calls. (line 37, file “Code”)

    MW



  • Hi @MwMagicMirror ,

    I didn’t got this specific error on my side but I got another one :

    • Mine was “too much invokes for one day” : I reduce the “every 2 hours triggers” to once a day + modified the time range from +/-400 to something less (copying before today date was useless)

    For your error, indeed, I’ve read somewhere on google that implementing a “sleep timer” between invokes could solve it.

    I just tried that and it works on my side (I don’t know if it will solve your issue as I don’t got the same) :

    function Sync_Birth_Cal() {
    
      // Calendars adress (ID)
      
      var calendarSource = CalendarApp.getCalendarById("ID1");
      var calendarDestination = CalendarApp.getCalendarById("ID2");
      
      // Start and End Date definition
      
      var Today = new Date();
      var StartDeleteDate = new Date();
      var EndDeleteDate = new Date();
      var StartCopyDate = new Date();
      var EndCopyDate = new Date();
      
      StartDeleteDate.setDate(Today.getDate()-10);
      EndDeleteDate.setDate(Today.getDate()+70);
      StartCopyDate.setDate(Today.getDate()-2);
      EndCopyDate.setDate(Today.getDate()+60);
     
      
      // first deletes all datas in calendar
      
      
      var eventToDelete = calendarDestination.getEvents(StartDeleteDate, EndDeleteDate);
      
       for (var i = 0; i < eventToDelete.length; i++) {  
        eventToDelete[i].deleteEvent();
         Utilities.sleep(1000); /// add a timer
        }  
    
    
      // then copy everything again
      
      var eventToCopy = calendarSource.getEvents(StartCopyDate, EndCopyDate);
    
      for (var t in eventToCopy){
        var newEvent = calendarDestination.createEvent(eventToCopy[t].getTitle(), eventToCopy[t].getStartTime(), eventToCopy[t].getEndTime());
        Utilities.sleep(1000); // add a timer
      }
      
    }
    


  • @bolish
    That got it for me, you did have the eventToDelete[i].deleteEvent(); outside the for loop, anyway its working like it should now.

    tyvm

    MW



  • @MwMagicMirror Happy it helped someone!!
    What do you mean by “was outside the loop”? Can you please show me the mistake?
    Tx



  • for (var i = 0; i < eventToDelete.length; i++) {
    }
    eventToDelete[i].deleteEvent();

    Thats what I noticed, anyway its all good, dont see that issue in any code now.
    Also I noticed that my birthday calendar has all my appointments and such. I see the logic is basically copying my main calendar into the birthday calendar. I manually delete everything out of my birthday calendar except for the birthdays, all is good…until…the script runs again then I back to having everything in my birthday calendar.

    MW