使用Google Calendar API删除事件时遇语法错误求助
Hey there! Let's break down what's causing that syntax error and get your code working right away.
The Root Cause
The error Missing name after . operator pops up because delete is a reserved keyword in JavaScript. When you try to call calendar.events.delete(params, ...), the script interpreter gets confused—reserved words can't be used directly as property names with dot notation.
The Fix
Swap out the dot notation for bracket notation when accessing the delete method. Here's how to adjust that problematic line:
calendar.events['delete'](params, function(err) { if (err) { console.log('The API returned an error: ' + err); return; } // Replace ... with your actual success logic here });
Bonus: Small Optimization
You're fetching calendarId = sheet.getRange('E2').getValue() inside your loop, but this value doesn't change between iterations. Move it outside the loop to make your code run more efficiently:
function deleteEvent() { // Get calendar ID once, outside the loop var calendarId = sheet.getRange('E2').getValue(); for (var i = 1; i < datalastRow; i++) { var delete_eventid = sheet.getRange(i + 1, 3).getValue(); var params = { calendarId: calendarId, eventId: delete_eventid, }; calendar.events['delete'](params, function(err) { if (err) { console.log('The API returned an error: ' + err); return; } // Add your success handling code here }); } }
Quick Checks
- Double-check that
datalastRowandsheetare properly defined elsewhere in your script (they aren't included in your snippet, but we'll assume they're set up correctly). - Don't forget to replace the
...in your callback with the actual actions you want to run when an event is deleted successfully.
内容的提问来源于stack exchange,提问作者Aaron

