Action
Import Calendar Events
Import Calendar Events (fixed)
I noticed that both the original Calendar action by Agile Tortoise and several others fail to import. I discovered a script by “unknown” that was last edited a in 2022 that was buggy. Among the various issues:
Time formatting: Missing padding for single-digit minutes, causing times to show as “10:0” instead of “10:00”.
AM/PM calculation logic:
The noon hour (12 PM) was being converted to “0:0 PM” because your code subtracted 12 when hours > 11
Midnight (12 AM) wasn’t being handled properly
Code correctness: The undefined check used the string “undefined” rather than the actual undefined value
Formatting inconsistencies: The all-day event formatting included a “/n” instead of a proper newline
All of this has been fixed. I hope someone can make use of this!
Steps
-
script
let calendars = Calendar.getAllCalendars(); let calendarNames = calendars.map(x => x.title); let p = Prompt.create(); p.title = "Import Calendar"; p.message = "Select calendar and date range to import events from the calendar as a Markdown list"; p.addSelect("calendar", "Calendar", calendarNames, [], false); p.addDatePicker("start","Start date", (1).years().ago(), {"mode":"date"}); p.addDatePicker("end","End date", Date.today(), {"mode":"date"}); p.addButton("Import"); if (p.show()) { let selected = p.fieldValues["calendar"]; let ix = calendarNames.findIndex(x => x == selected); let cal = calendars[ix]; let start = p.fieldValues["start"]; let end = Date.parse(p.fieldValues["end"]).addMilliseconds(86399999); if (cal) { if (!importCal(cal, start, end)) { context.fail(); } } } else { context.cancel(); } function importCal(cal, start, end) { let events = cal.events(start, end); if (events == undefined || events.length == 0) { alert('No events found to import'); return false; } let t = [] for(let event of events) { t.push("* " + event.title + " - "); if (event.isAllDay) { t.push("All Day"); } else { let hours = event.startDate.getHours(); let minutes = event.startDate.getMinutes(); let period = "AM"; if (hours >= 12) { period = "PM"; if (hours > 12) { hours -= 12; } } // Handle midnight case (0:00 AM should display as 12:00 AM) if (hours === 0) { hours = 12; } // Ensure minutes have leading zeros when needed let formattedMinutes = minutes < 10 ? "0" + minutes : minutes; t.push(hours + ":" + formattedMinutes + " " + period); } t.push('\n'); } draft.append(t.join("")); return true; }
Options
-
After Success Default Notification Info Log Level Info