如何仅用Apps Script获取谷歌日历事件完整ID以生成跳转链接
Absolutely—you don’t need to enable the advanced Calendar API just to grab a full event ID for that direct link! The built-in CalendarApp service in Apps Script is more than enough for this, and it’s way simpler to set up. Here’s how to do it:
Step 1: Fetch the Event with Basic Search Criteria
You can target your event using details like its title, date range, or location. The CalendarApp service lets you pull events without any advanced setup—just standard authorization when you first run the script.
Here’s a practical example script:
function generateDirectEventLink() { // 1. Replace with your public calendar's ID (find this in Calendar settings > Integrate calendar) const targetCalendarId = "your-public-calendar-id@group.calendar.google.com"; const calendar = CalendarApp.getCalendarById(targetCalendarId); // 2. Define criteria to find your specific event (adjust these values!) const eventTitle = "Your Public Event Title"; const startDate = new Date("2024-06-01"); // Start of date range to search const endDate = new Date("2024-06-30"); // End of date range // 3. Fetch matching events const matchingEvents = calendar.getEvents(startDate, endDate, { title: eventTitle }); if (matchingEvents.length > 0) { // Grab the full event ID directly from the event object const fullEventId = matchingEvents[0].getId(); // Use your script's timezone (or hardcode one like "America/New_York") const timezone = Session.getScriptTimeZone(); // Build the direct link const directLink = `https://www.google.com/calendar/event?eid=${encodeURIComponent(fullEventId)}&ctz=${encodeURIComponent(timezone)}`; Logger.log("Direct Event Link: " + directLink); // You could also return this link or insert it into a Google Sheet/Doc } else { Logger.log("No matching events found—double-check your title and date range!"); } }
Key Notes:
- Full Event ID: The
getId()method returns the exact string you need for theeidparameter—no extra parsing required. It includes both the unique event identifier and the calendar ID, which Google Calendar uses to locate the event. - Repeat Events: If your event is recurring,
getEvents()will return all instances in the date range. UsegetSeries()on an instance to get the master event ID if you want to link to the entire series instead of a single occurrence. - Default Calendar: If you’re targeting the current user’s default calendar, skip the
getCalendarById()call and useCalendarApp.getDefaultCalendar()instead.
This approach avoids the hassle of enabling advanced services or dealing with the full Calendar API—just basic Apps Script functionality that’s quick to set up and authorize.
内容的提问来源于stack exchange,提问作者carlton riffel

