I have a very basic problem within my whole code which became a head-ache for a couple of days. I have made an extensive research on this issue however couldn't be able to find an exact solution (or I have missed the one's that I might have found). Here it is:
I have a spreadsheet, where I log in Google Calendar events with their properties including the id of the event. Sometimes, I clean up the Google Calendar manually and just want to run a code which checks the events existence in the calendar and if not delete the row. My code is:
function cleanCal(calid, eventid) {
var calid = 'my calendar id';
var eventid = 'event id';
var event = CalendarApp.getOwnedCalendarById(calid).getEventById(eventid);
if (event) {
event.deleteEvent();
// clean-up sheet
} else {
// clean-up sheet
}
}
Basically if the event is in the calendar, the code shall delete it first and then clean the sheet, if it's not there, it should clean the sheet only. However, when the code is executed, though the calendar event is not there, the if statement returns true and raises an error when trying to delete the event as it's actually not there. I haven't been able to find out the reason, how and why the event object return true though the event is not existing. Where am I doing wrong?? Thanks for replying and any help is really appreciated.
[EDIT]
This is the code that I use to check events existence with Calendar API v3
function verifyCalendarEvent_OLD(calid, eventid) {
var cal = CalendarApp.getCalendarById(calid)
var exists = true;
var response = Calendar.Events.list(
calid, {
showDeleted: true,
fields: "items(id,status,summary)"
}
);
for (var i = 0; i < response.items.length; i++) {
if (response.items[i].id == eventid.split("@")[0] && response.items[i].status == "cancelled") {
exists = false;
break;
}
}
return exists;
}
How about this answer?
Modification points :
For example, if there is no event of the event ID, CalendarApp.getOwnedCalendarById(calid).getEventById(eventid)
returns null
. When this is evaluated by if
, that is used as false
.
So I thought that you might try to retrieve the event which has already been removed. Because at Google Calendar, even if the event is removed, the event ID is remained. So although there is no event for the event ID, if (event) {}
in your script returns true
. I confirmed that CalendarApp.getOwnedCalendarById(calid).getEventById(eventid)
retrieves the removed events. For this situation, you can know whether the event is removed by confirming the event status.
- When the event status is confirmed, it indicates that the event is still not removed.
- When the event status is cancelled, it indicates that the event has already been removed.
Preparation to use this modified sample script :
When you use this modified script, please enable Calendar API at Advanced Google Services and API console.
Enable Calendar API v3 at Advanced Google Services
- On script editor
- Resources -> Advanced Google Services
- Turn on Calendar API v3
Enable Calendar API at API console
- On script editor
- Resources -> Cloud Platform project
- View API console
- At Getting started, click Enable APIs and get credentials like keys.
- At left side, click Library.
- At Search for APIs & services, input "Calendar". And click Calendar API.
- Click Enable button.
- If API has already been enabled, please don't turn off.
- When you run this script, if an error occurs, you might be required to wait few minutes until the API is enabled.
Modified script :
function cleanCal(calid, eventid) {
var calid = 'my calendar id';
var eventid = 'event id';
var status = Calendar.Events.get(calid, eventid).status; // Added
if (status == "confirmed") { // Modified
var event = CalendarApp.getOwnedCalendarById(calid).getEventById(eventid); // Added
event.deleteEvent();
// clean-up sheet
} else {
// clean-up sheet
}
}
If I misunderstand your question, I'm sorry.
Edit :
In my environment, the events removed by manual and script can be retrieved as status=cancelled
. Since I didn't know your situation, I prepared a sample script. This sample script is a simple flow.
- Create new event.
- Delete the created event.
- Confirm the deleted event.
- Here, your additional script was used.
Sample script :
function deleteeventa() {
// Create new event
var calid = 'my calendar id';
var c = CalendarApp.getCalendarById(calid);
var r = c.createEvent("sample event for deleting", new Date(2018,0,11,00,00), new Date(2018,0,11,01,00));
// Delete the created new event
var eventid = r.getId().split('@')[0];
var event = c.getEventById(eventid);
event.deleteEvent();
// Your additional script
var exists = true;
var response = Calendar.Events.list(
calid, {
showDeleted: true,
fields: "items(id,status,summary)"
}
);
for (var i = 0; i < response.items.length; i++) {
if (response.items[i].id == eventid && response.items[i].status == "cancelled") {
exists = false;
break;
}
}
Logger.log("%s, %s, %s", r.getTitle(), r.getId(), exists)
}
Result :
sample event for deleting, #####@google.com, false
Following Tanaike's great help, I have come up with this code snippet which - as far as I have tested - works fine for now. Hope it might be helpful for other users. Here is the code fragment:
function verifyCalendarEvent(calid, eventid) {
var cal = CalendarApp.getCalendarById(calid)
eventid = eventid.split("@")[0];
var exists = true;
var eventIds = [];
var eventStats = [];
var response = Calendar.Events.list(
calid, {
showDeleted: true,
fields: "items(id,status,summary)"
}
);
for (var i = 0; i < response.items.length; i++) {
eventIds.push(response.items[i].id);
}
for (var i = 0; i < response.items.length; i++) {
eventStats.push(response.items[i].status);
}
if (eventIds.indexOf(eventid) > 0) {
for (var i = 0; i < eventIds.length; i++) {
if (eventIds[i] == eventid && eventStats[i] == "cancelled") {
exists = false;
}
}
} else {
exists = false;
}
Logger.log("Calendar Event ["+eventid+"] exists? >> "+exists);
return exists;
}