Created
November 5, 2025 14:28
-
-
Save rkrevolution/4bccff70011b9d5f5324e0dbf98c38ae to your computer and use it in GitHub Desktop.
Google App Script Shift Events Back by One Day
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Shifts every event in a given Google Calendar back by exactly one day (24h). | |
| * - Automatically detects the true min/max date range of existing events. | |
| * - Includes a DRY_RUN mode so you can preview changes before applying. | |
| */ | |
| function shiftCalendarEventsBackOneDay_AutoRange() { | |
| /** | |
| * 1. REQUIRED: Replace this with the Calendar ID you want to modify. | |
| * You can find it under Google Calendar → Settings → "Calendar ID" | |
| * Example: "abc123@group.calendar.google.com" | |
| */ | |
| const CALENDAR_ID = "YOUR_CALENDAR_ID_HERE"; | |
| /** | |
| * 2. Safety toggle: | |
| * - true = only LOG what would change (does NOT edit events) | |
| * - false = actually modify the events | |
| * Always run at least once in DRY_RUN mode before setting to false. | |
| */ | |
| const DRY_RUN = true; | |
| // 3. Get the Calendar object by ID | |
| const cal = CalendarApp.getCalendarById(CALENDAR_ID); | |
| if (!cal) throw new Error("Calendar not found. Check CALENDAR_ID."); | |
| /** | |
| * 4. Fetch all events using a very wide date window. | |
| * We don't know the real range yet, so we grab everything between 2000–2100. | |
| * (This is safe—Google will only return real events that exist.) | |
| */ | |
| const FAR_PAST = new Date(2000, 0, 1); // Jan 1, 2000 | |
| const FAR_FUTURE = new Date(2100, 0, 1); // Jan 1, 2100 | |
| const events = cal.getEvents(FAR_PAST, FAR_FUTURE); | |
| // If no events exist, there's nothing to do | |
| if (events.length === 0) { | |
| Logger.log("No events found in the calendar."); | |
| return; | |
| } | |
| /** | |
| * 5. Find MIN start time and MAX end time in the calendar | |
| * This gives us the real date range that imported events cover. | |
| */ | |
| let minStart = events[0].getStartTime(); | |
| let maxEnd = events[0].getEndTime(); | |
| events.forEach(event => { | |
| const start = event.getStartTime(); | |
| const end = event.getEndTime(); | |
| if (start < minStart) minStart = start; | |
| if (end > maxEnd) maxEnd = end; | |
| }); | |
| // Log the detected date range so the user can confirm it looks correct | |
| Logger.log(`Detected calendar range: ${minStart} → ${maxEnd}`); | |
| Logger.log(`Total events detected: ${events.length}`); | |
| /** | |
| * 6. Define 1 day in milliseconds | |
| * (Google Calendar stores times internally as full datetime objects, | |
| * so we adjust via millisecond math!) | |
| */ | |
| const ONE_DAY_MS = 24 * 60 * 60 * 1000; | |
| // 7. Track how many events were processed (for reporting) | |
| let count = 0; | |
| /** | |
| * 8. Loop through every event and push it back by one day | |
| */ | |
| events.forEach(event => { | |
| // Original start/end times from calendar | |
| const oldStart = event.getStartTime(); | |
| const oldEnd = event.getEndTime(); | |
| // New shifted times | |
| const newStart = new Date(oldStart.getTime() - ONE_DAY_MS); | |
| const newEnd = new Date(oldEnd.getTime() - ONE_DAY_MS); | |
| if (DRY_RUN) { | |
| // Only log what WOULD happen — no changes made | |
| Logger.log(`[DRY RUN] "${event.getTitle()}" ${oldStart} → ${newStart}`); | |
| } else { | |
| // Actually update the event in Google Calendar | |
| event.setTime(newStart, newEnd); | |
| } | |
| count++; | |
| }); | |
| // 9. Final summary log | |
| Logger.log(`${DRY_RUN ? "Would shift" : "Shifted"} ${count} event(s) back by one day.`); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment