MagicMirror Forum
    • Recent
    • Tags
    • Unsolved
    • Solved
    • MagicMirror² Repository
    • Documentation
    • 3rd-Party-Modules
    • Donate
    • Discord
    • Register
    • Login
    A New Chapter for MagicMirror: The Community Takes the Lead
    Read the statement by Michael Teeuw here.

    Getting Google Contacts birthdays into Calendar

    Scheduled Pinned Locked Moved Development
    6 Posts 2 Posters 2.6k Views 3 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • M Offline
      MwMagicMirror
      last edited by MwMagicMirror

      @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

      1 Reply Last reply Reply Quote 0
      • B Offline
        bolish
        last edited by

        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
          }
          
        }
        
        M 1 Reply Last reply Reply Quote 0
        • M Offline
          MwMagicMirror @bolish
          last edited by

          @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

          1 Reply Last reply Reply Quote 0
          • B Offline
            bolish
            last edited by

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

            1 Reply Last reply Reply Quote 0
            • M Offline
              MwMagicMirror
              last edited by

              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

              1 Reply Last reply Reply Quote 0
              • 1 / 1
              • First post
                Last post
              Enjoying MagicMirror? Please consider a donation!
              MagicMirror created by Michael Teeuw.
              Forum managed by Sam, technical setup by Karsten.
              This forum is using NodeBB as its core | Contributors
              Contact | Privacy Policy