基于菜单触发发送谷歌日历邀请,需先审核表单提交
修正后的代码实现(审核后批量添加日历参会者)
需求回顾
先审核Google Form提交内容,再从独立的Invites工作表(A列姓名、B列邮箱、C列会话标题、D列eventID)提取信息,给学生发送谷歌日历邀请。
原代码问题说明
- 数组索引错误:D列(eventID)对应数组索引应为
r[3](数组从0开始计数),原代码误用了r[4](对应第5列) - 无效变量定义:
attendeeEmail = [2]、eventId = [4]属于错误写法,无实际作用 - 日历ID冲突:代码中出现两个不同的日历ID,需统一为目标日历ID
修正后的代码
function addAttendeeToEvent() { // 目标工作表:Invites const ss = SpreadsheetApp.getActive().getSheetByName('Invites'); if (!ss) { console.log('未找到名为"Invites"的工作表'); return; } // 获取表头+数据行,跳过表头 const [header, ...rows] = ss.getDataRange().getValues(); // 目标日历ID(替换为你的实际日历ID) const targetCalendarId = 'bd8tmudge9f6ti9vgtm7kkagss@group.calendar.google.com'; const cal = CalendarApp.getCalendarById(targetCalendarId); if (!cal) { console.log('无法访问指定的日历,请检查日历ID是否正确'); return; } // 遍历每一行参会者信息 rows.forEach((row, index) => { const attendeeEmail = row[1]; // B列:邮箱 const eventId = row[3]; // D列:eventID // 跳过邮箱或eventID为空的行 if (!attendeeEmail || !eventId) { console.log(`第${index+2}行:邮箱或eventID为空,跳过`); return; } try { // 获取目标日历事件 const event = cal.getEventById(eventId); if (event) { // 添加参会者 event.addGuest(attendeeEmail); console.log(`已成功为${attendeeEmail}添加到事件:${event.getTitle()}`); } else { console.log(`第${index+2}行:未找到ID为${eventId}的事件`); } } catch (error) { console.log(`第${index+2}行处理失败:${error.message}`); } }); }
使用说明
- 确保
Invites工作表的列顺序正确:A=姓名、B=邮箱、C=会话标题、D=eventID - 替换代码中的
targetCalendarId为你的实际目标日历ID - 运行前先手动审核
Invites表中的内容,确认参会者信息和eventID无误 - 运行代码后,可通过「查看>日志」查看执行结果
内容的提问来源于stack exchange,提问作者pres10
相关产品推荐
相关产品推荐

