通过Apps Script与Google Calendar API为Google Calendar活动添加多位参会者时的错误排查
The root cause of your GoogleJsonResponseException: Invalid attendee email error is how you're structuring the attendees array. Right now, you're passing entire arrays of emails and names to a single attendee object, but the Google Calendar API expects an array of individual attendee objects—each with a single string email and displayName.
Here's the breakdown of the mistake:
Your current code takes the split arrays and shoves them into one attendee entry:
var emails = attendeesEmail.split(","); var names = attendeesNames.split(","); const event = { // ... other properties 'attendees': [ { email: emails, displayName: names } // ❌ Passes arrays instead of individual strings ] }
The Fix:
You need to loop through the split email and name arrays, create a separate attendee object for each pair, and collect those into the attendees array. Also, make sure to trim any extra whitespace from split values (since your example has ", " between entries, which leaves a leading space on the second name/email).
Replace your problematic code block with this:
// Handle attendees: split and clean up values, then create individual attendee objects const attendees = []; if (attendeesEmail && attendeesNames) { const emails = attendeesEmail.split(",").map(email => email.trim()); const names = attendeesNames.split(",").map(name => name.trim()); // Match each email to its corresponding name (assumes arrays are same length) for (let j = 0; j < emails.length; j++) { attendees.push({ email: emails[j], displayName: names[j] || '' // Fallback to empty string if name is missing }); } } const event = { id: eventID, summary: eventsubject, location: location, description: description, 'start': { 'dateTime': startTime.toISOString(), 'timeZone': 'US/Central' }, 'end': { 'dateTime': endTime.toISOString(), 'timeZone': 'US/Central' }, colorId: color, attendees: attendees // ✅ Pass array of individual attendee objects }
Key Improvements:
- Uses
map().trim()to remove extra spaces from split values (critical for avoiding invalid emails with leading/trailing spaces) - Creates a dedicated
attendeesarray by pairing each email with its matching name - Adds a fallback for missing names to prevent undefined values
- Properly structures the attendees array to meet Calendar API requirements
Additional Checks:
- Ensure that the number of names matches the number of emails in your spreadsheet rows (the code assumes they're paired correctly)
- Test with both single and multiple attendee rows to confirm the fix works for both cases
内容的提问来源于stack exchange,提问作者Adam Schwartz

