【发布时间】:2022-08-09 05:58:05
【问题描述】:
我正在使用这个应用脚本Populate a team vacation calendar
我在 50 位用户的 Google 群组中收到以下错误,其中有 1 人。
\'检索 email@email.com 的事件时出错,假期: GoogleJsonResponseException:对 calendar.events.list 的 API 调用失败 错误:未找到;跳过\'
设置断点时,在此导入事件上失败。
function importEvent(username, event) { event.summary = \'[\' + username + \'] \' + event.summary; event.organizer = { id: TEAM_CALENDAR_ID, }; event.attendees = []; console.log(\'Importing: %s\', event.summary); try { Calendar.Events.import(event, TEAM_CALENDAR_ID); } catch (e) { console.error(\'Error attempting to import event: %s. Skipping.\', e.toString()); } } do { params.pageToken = pageToken; let response; try { response = Calendar.Events.list(user.getEmail(), params); } catch (e) { console.error(\'Error retriving events for %s, %s: %s; skipping\', user, keyword, e.toString()); continue; } events = events.concat(response.items.filter(function(item) { return shoudImportEvent(user, keyword, item); })); pageToken = response.nextPageToken; } while (pageToken); return events; }这是堆栈:
\"GoogleJsonResponseException:对 calendar.events.list 的 API 调用失败 错误:在代码:48:20 的 findEvents(代码:111:34)处未找到 Array.forEach () at Code:47:14 a...\"
这是完整的代码
// Set the ID of the team calendar to add events to. You can find the calendar\'s // ID on the settings page. let TEAM_CALENDAR_ID = \'CALENDAR ID\'; // Set the email address of the Google Group that contains everyone in the team. // Ensure the group has less than 500 members to avoid timeouts. let GROUP_EMAIL = \'GROUP EMAIL\'; let KEYWORDS = [\'vacation\', \'ooo\', \'out of office\', \'offline\']; let MONTHS_IN_ADVANCE = 3; /** * Sets up the script to run automatically every hour. */ function setup() { let triggers = ScriptApp.getProjectTriggers(); if (triggers.length > 0) { throw new Error(\'Triggers are already setup.\'); } ScriptApp.newTrigger(\'sync\').timeBased().everyHours(1).create(); // Runs the first sync immediately. sync(); } /** * Looks through the group members\' public calendars and adds any * \'vacation\' or \'out of office\' events to the team calendar. */ function sync() { // Defines the calendar event date range to search. let today = new Date(); let maxDate = new Date(); maxDate.setMonth(maxDate.getMonth() + MONTHS_IN_ADVANCE); // Determines the time the the script was last run. let lastRun = PropertiesService.getScriptProperties().getProperty(\'lastRun\'); lastRun = lastRun ? new Date(lastRun) : null; // Gets the list of users in the Google Group. let users = GroupsApp.getGroupByEmail(GROUP_EMAIL).getUsers(); // For each user, finds events having one or more of the keywords in the event // summary in the specified date range. Imports each of those to the team // calendar. let count = 0; users.forEach(function(user) { let username = user.getEmail().split(\'@\')[0]; KEYWORDS.forEach(function(keyword) { let events = findEvents(user, keyword, today, maxDate, lastRun); events.forEach(function(event) { importEvent(username, event); count++; }); // End foreach event. }); // End foreach keyword. }); // End foreach user. PropertiesService.getScriptProperties().setProperty(\'lastRun\', today); console.log(\'Imported \' + count + \' events\'); } /** * Imports the given event from the user\'s calendar into the shared team * calendar. * @param {string} username The team member that is attending the event. * @param {Calendar.Event} event The event to import. */ function importEvent(username, event) { event.summary = \'[\' + username + \'] \' + event.summary; event.organizer = { id: TEAM_CALENDAR_ID, }; event.attendees = []; console.log(\'Importing: %s\', event.summary); try { Calendar.Events.import(event, TEAM_CALENDAR_ID); } catch (e) { console.error(\'Error attempting to import event: %s. Skipping.\', e.toString()); } } /** * In a given user\'s calendar, looks for occurrences of the given keyword * in events within the specified date range and returns any such events * found. * @param {Session.User} user The user to retrieve events for. * @param {string} keyword The keyword to look for. * @param {Date} start The starting date of the range to examine. * @param {Date} end The ending date of the range to examine. * @param {Date} optSince A date indicating the last time this script was run. * @return {Calendar.Event[]} An array of calendar events. */ function findEvents(user, keyword, start, end, optSince) { let params = { q: keyword, timeMin: formatDateAsRFC3339(start), timeMax: formatDateAsRFC3339(end), showDeleted: true, }; if (optSince) { // This prevents the script from examining events that have not been // modified since the specified date (that is, the last time the // script was run). params.updatedMin = formatDateAsRFC3339(optSince); } let pageToken = null; let events = []; do { params.pageToken = pageToken; let response; try { response = Calendar.Events.list(user.getEmail(), params); } catch (e) { console.error(\'Error retriving events for %s, %s: %s; skipping\', user, keyword, e.toString()); continue; } events = events.concat(response.items.filter(function(item) { return shoudImportEvent(user, keyword, item); })); pageToken = response.nextPageToken; } while (pageToken); return events; } /** * Determines if the given event should be imported into the shared team * calendar. * @param {Session.User} user The user that is attending the event. * @param {string} keyword The keyword being searched for. * @param {Calendar.Event} event The event being considered. * @return {boolean} True if the event should be imported. */ function shoudImportEvent(user, keyword, event) { // Filters out events where the keyword did not appear in the summary // (that is, the keyword appeared in a different field, and are thus // is not likely to be relevant). if (event.summary.toLowerCase().indexOf(keyword.toLowerCase) < 0) { return false; } if (!event.organizer || event.organizer.email == user.getEmail()) { // If the user is the creator of the event, always imports it. return true; } // Only imports events the user has accepted. if (!event.attendees) return false; let matching = event.attendees.filter(function(attendee) { return attendee.self; }); return matching.length > 0 && matching[0].responseStatus == \'accepted\'; } /** * Returns an RFC3339 formated date String corresponding to the given * Date object. * @param {Date} date a Date. * @return {string} a formatted date string. */ function formatDateAsRFC3339(date) { return Utilities.formatDate(date, \'UTC\', \'yyyy-MM-dd\\\'T\\\'HH:mm:ssZ\'); }
-
虽然指向外部资源的链接可能会有所帮助,但问题应该是自包含的。请添加minimal reproducible example。
-
@Rubén 我用代码修改了我的问题。
-
代码不完整,无法成为minimal reproducible example,即未声明
params,并且缺少有关如何重现错误的详细信息。 -
我继续并在最后添加了整个代码。除了错误\'Error retriving events for email@email.com, holiday: GoogleJsonResponseException: API call to calendar.events.list failed with error: Not Found,没有什么好说的了;每次运行时都会发生跳过\'。对于 1 个特定用户。
-
这个问题有几个问题。我建议你从头开始。将新问题集中在一条错误消息中,并包含minimal reproducible example 以重现该特定错误。包括所有相关细节以允许其他人重现错误,例如用户是否仅属于一个域或多个域等。
标签: google-apps-script google-calendar-api