如果设置已公开并且组织中的所有ID都可访问,则getCalendarByID(Id)的工作方式

人气:1,018 发布:2022-10-16 标签: javascript web calendar google-apps-script google-calendar-api

问题描述

我正在为日历事件使用Google应用程序脚本, 根据文档:https://developers.google.com/apps-script/reference/calendar/calendar-app#getCalendarById%28String%29

返回值为具有给定ID的日历,如果日历不存在或用户无法访问,则返回值为空 假设在组织Windows.com中,设置是在组织中的所有ID都可访问的位置进行的

var cal = CalendarApp.getCalendarById('mac@windows.com');
   Logger.log(cal);

它给出null如果我运行该函数,并且当我手动将Mac@Windows.com订阅到授权脚本运行器的日历时,它工作正常,

有没有办法在运行脚本时获取组织订阅的id,以便动态输入id。

Org Google业务应用程序中的日历设置为:默认情况下与组织Windows.com中的每个人共享此日历。

推荐答案

我在最近的一个项目中遇到了同样的问题,我发现用户必须显式订阅日历,GetCalendarByID函数才能可靠地工作。我使用了以下代码来处理这个问题,如果getCalendarById在第一次尝试时未能返回它,它将尝试订阅用户。

我还将日历设置为隐藏和取消选择,这样订阅用户就不会扰乱他们的标准日历视图。

  var calendar = CalendarApp.getCalendarById(calendar_id);
  if(calendar == null){
    //user may not have access, auto-subscribe them.
    calendar = CalendarApp.subscribeToCalendar(calendar_id,{hidden:true,selected:false});
  }

请参阅CalendarApp参考资料https://developers.google.com/apps-script/reference/calendar/calendar-app#subscribeToCalendar(String,Object)

677