我正在使用谷歌应用程序脚本来处理日历事件, 根据文件:https://developers.google.com/apps-script/reference/calendar/calendar-app#getCalendarById%28String%29
返回值为给定ID的日历,如果日历不存在或用户无法访问,则返回null 假设在一个组织 windows.com 中进行了设置,其中该组织中的所有 id 都可以访问,
var cal = CalendarApp.getCalendarById('<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0b666a684b7c62656f647c7825686466" rel="noreferrer noopener nofollow">[email protected]</a>');
Logger.log(cal);
如果我运行该函数并且手动订阅 [email protected] ,它会给出 null
到授权脚本运行程序的日历然后它就可以正常工作,
有没有一种方法可以让我们在运行脚本时获取组织订阅的id,以便动态输入id。
组织 Google Business 应用中的日历设置为:默认与组织 windows.com 中的每个人共享此日历。
最佳答案
我在最近的项目中遇到了同样的问题,并发现用户必须显式订阅日历才能使 GetCalendarById 函数可靠地工作。我使用以下代码来处理此问题,如果 getCalendarById 第一次尝试返回失败,它会尝试订阅用户。
我还将日历设置为隐藏和未选择,因此订阅用户不会扰乱他们的标准日历 View 。
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)
关于javascript - 如果设置公开并且组织中的所有 ID 均可访问,则 getCalendarById(id) 如何工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26645439/