首页 > 解决方案 > 同步从 URL 创建的 Google 日历

问题描述

我已经使用 URL https://calendar.google.com/calendar/u/0/r/settings/addbyurl手动将我的健身房日历添加到我的谷歌日历中,并粘贴到健身房的 webcal url 中。这有效,新日历在谷歌日历中显示为“其他日历”。但是,当我预订更多的体育课时,它们并没有被谷歌同步。

如果我手动取消订阅“其他日历”并重新创建它,新的健身课程会出现在我的桌面和我的安卓手机上。这种娱乐迫使同步。

所以我尝试编写一些谷歌应用脚​​本来删除“其他日历”并重新创建它。然后通过基于时间的触发器运行应用脚本。

该脚本运行正常并且删除工作有效,但我找不到通过脚本重新创建日历的方法。所以我真的很感激一些帮助来完成这个编码或强制谷歌日历重新同步它的“其他日历”(无论如何它应该这样做)。

这是我的编码:

    myCalendar('My Bookings');

    function myCalendar(mycal) {

      var calendars;
      var pageToken;
      do {
        calendars = Calendar.CalendarList.list({
          maxResults: 100,
          pageToken: pageToken
        });
        if (calendars.items && calendars.items.length > 0) {
          for (var i = 0; i < calendars.items.length; i++) {
            var calendar = calendars.items[i];
            Logger.log('%s (ID: %s)', calendar.summary, calendar.id, calendar.description);
            if (calendar.summary == mycal) {
              // get works it's my gym calendar, but why doesn't it show the webcal url?
              var mycalendar = Calendar.CalendarList.get(calendar.id);
              Logger.log(mycalendar);
              // remove works
              Calendar.CalendarList.remove(calendar.id);
              Logger.log('removed');
              // The calendar has been removed
              // But how do I recreate the same calendar from a url?
            }
          }
        } else {
          Logger.log('No calendar found.');
        }
        pageToken = calendars.nextPageToken;
      } while (pageToken);
    }

标签: google-apps-scriptgoogle-calendar-api

解决方案


这段代码按你说的做。

function myFunction () {
  var id = 'nmkf37p4k6onnhsa8a770u0m8adqd3v2@import.calendar.google.com' 
  //change this id. 
  //You can find your calendar's id in Calendar Settings > Integrate Calendar
  
  var calendar = CalendarApp.getCalendarById(id)
  
  calendar.unsubscribeFromCalendar() // this line makes you unsubscribe
  CalendarApp.subscribeToCalendar(id) // this makes you resubscribe
}

但是请告诉我,您如何将新事件添加到日历中?例如,您通过calendar.google.com浏览器添加它们,但在 Android 手机上看不到它们。这是正在发生的事情吗?


推荐阅读