Skip to content

Commit a3daffe

Browse files
Merge pull request #71 from contributorpw/edits
2 parents 96a8e98 + 51c1365 commit a3daffe

File tree

4 files changed

+49
-10
lines changed

4 files changed

+49
-10
lines changed
Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
{
22
"timeZone": "Europe/Moscow",
33
"exceptionLogging": "STACKDRIVER",
4-
"runtimeVersion": "V8"
4+
"runtimeVersion": "V8",
5+
"dependencies": {
6+
"enabledAdvancedServices": [
7+
{
8+
"userSymbol": "Calendar",
9+
"version": "v3",
10+
"serviceId": "calendar"
11+
}
12+
]
13+
}
514
}
Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,41 @@
11
/**
22
*
33
* Deletes only specific events from series that match by date and search term
4+
* Retuns affected events
45
*
5-
* @param {GoogleAppsScript.Calendar.Calendar} calendar
6+
* @param {string} calendarId
67
* @param {Date} start
78
* @param {Date} end
89
* @param {string} search
9-
* @returns {GoogleAppsScript.Calendar.CalendarEvent[]}
10+
* @returns {GoogleAppsScript.Calendar.Schema.Event[]}
1011
*/
11-
function deleteEventFromSeries(calendar, start, end, search) {
12-
const options = search ? {} : { search };
13-
const events = calendar.getEvents(start, end, options);
14-
return events.map(
15-
(event) => (event.getEventSeries() ? event.deleteEvent() : undefined, event)
16-
);
12+
function deleteEventFromSeries(calendarId, start, end, search) {
13+
const result = [];
14+
const timeMin = start.toISOString();
15+
const timeMax = end.toISOString();
16+
const events = Calendar.Events.list(calendarId, {
17+
timeMin,
18+
timeMax,
19+
q: search,
20+
singleEvents: false,
21+
fields: 'items',
22+
});
23+
if (events.items.length) {
24+
events.items
25+
.filter((event) => event.recurrence)
26+
.forEach((event) => {
27+
const instances = Calendar.Events.instances(calendarId, event.id, {
28+
timeMin,
29+
timeMax,
30+
});
31+
if (instances.items.length)
32+
instances.items.forEach((instance) => {
33+
instance.status = 'cancelled';
34+
result.push(
35+
Calendar.Events.patch(instance, calendarId, instance.id)
36+
);
37+
});
38+
});
39+
}
40+
return result;
1741
}

snippets/calendar/delete-one-of-series/readme.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ categories: ['snippets']
1010

1111
## Delete specific events from Google Calendar series
1212

13+
**Note:** For bulk operations please use Google Calendar API Batch Requests. _See:_ [Sending Batch Requests  |  Calendar API  |  Google Developers](https://developers.google.com/calendar/batch), [tanaikech/BatchRequest: This is a library for running Batch Requests using Google Apps Script (GAS).](https://github.com/tanaikech/BatchRequest)
14+
1315
### Snippet
1416

1517
- {{< externalLink >}}
@@ -21,3 +23,7 @@ categories: ['snippets']
2123
### Run it
2224

2325
{{< codeFromFile "run.js" >}}
26+
27+
### Manifest
28+
29+
{{< codeFromFile "appsscript.json" >}

snippets/calendar/delete-one-of-series/run.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@ function run() {
1414
const end = new Date(now);
1515
end.setHours(23, 59, 59, 999);
1616
end.setDate(end.getDate() + 2);
17-
console.log(deleteEventFromSeries(calendar, start, end, 'event'));
17+
console.log(deleteEventFromSeries(calendar.getId(), start, end, 'event'));
1818
}

0 commit comments

Comments
 (0)