从Google表格同步事件时,CalendarApp创建全天事件仅显示“To:”
Let's break down why your script is creating events with that weird "To:" label and fix it step by step.
Core Issues in Your Original Code
Wrong Parameter Order for
createAllDayEvent
TheCalendarApp.createAllDayEvent()method expects the first argument to be the event title (string) and the second to be the start date (Date object). You passed a date as the first argument, which gets converted into an unreadable string for the title—hence the "To:" display bug.Empty Loop (No Data Processing)
Yourforloop has no code inside it, so it never actually iterates through the rows of data in your spreadsheet. You're only creating a single hardcoded event instead of using the sheet data.Not Using the Specified Calendar
You fetched the target calendar ID from cell D1, but then usedCalendarApp.getDefaultCalendar()to create events—meaning events were going to your default calendar instead of the one you intended.
Fixed Script with Explanations
Here's the corrected version that properly pulls event titles and dates from your sheet and adds them to the right calendar:
function importSheetEventsToCalendar() { // Grab the active spreadsheet tab var activeSheet = SpreadsheetApp.getActiveSheet(); // Get the target calendar ID from cell D1 var calendarID = activeSheet.getRange("D1").getValue(); // Access the specified calendar (fail early if ID is invalid) var targetCalendar = CalendarApp.getCalendarById(calendarID); // Validate calendar ID before proceeding if (!targetCalendar) { SpreadsheetApp.getUi().alert("Oops! The calendar ID in D1 is invalid. Please check it and try again."); return; } // Fetch rows A1:B11 (assuming A = event title, B = event date) var sheetData = activeSheet.getRange("A1:B11").getValues(); // Loop through each row to create events for (let rowIndex = 0; rowIndex < sheetData.length; rowIndex++) { var currentRow = sheetData[rowIndex]; var eventTitle = currentRow[0]; // Column A: Event name var eventDate = currentRow[1]; // Column B: Event date // Skip empty rows (no title or no date) if (!eventTitle || !eventDate) continue; // Convert date to a proper Date object if it's a string if (typeof eventDate === "string") { eventDate = new Date(eventDate); } // Create the all-day event in the target calendar targetCalendar.createAllDayEvent(eventTitle, eventDate); // Log progress for debugging (check in View > Logs) Logger.log(`Created event: ${eventTitle} on ${eventDate.toLocaleDateString()}`); } }
Key Fixes & Improvements
- Correct Parameter Order: Now we pass the event title first, then the date—so your calendar will show the proper event name instead of "To:".
- Functional Loop: The loop now processes every row in your sheet data, creating an event for each valid row.
- Target Calendar Usage: Events are added to the calendar you specified in D1, not your default calendar.
- Error Handling: The script checks if the calendar ID is valid and alerts you if not, plus skips empty rows to avoid errors.
- Date Safety: Converts string dates from the sheet into proper Date objects to ensure compatibility.
Quick Notes to Ensure Success
- If your sheet has a header row (e.g., A1 = "Event Title", B1 = "Date"), start the loop at
rowIndex = 1instead of 0 to skip the header. - Make sure the dates in column B are in a format JavaScript can parse (like
MM/dd/yyyyoryyyy-MM-dd). - The first time you run the script, you'll need to grant permissions for it to access your Google Sheets and Calendar—follow the on-screen prompts.
内容的提问来源于stack exchange,提问作者Петр Аношкин

