Last active
August 15, 2025 13:47
-
-
Save ynrng/6ebecd5ac60309fd997051b5e4ea6431 to your computer and use it in GitHub Desktop.
automatic google calendar event for pleasance gym booking when receive confirmation email
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
| function processBookingEmails() { | |
| // 1. Search for unread booking emails | |
| var threads = GmailApp.search('from:[email protected] is:unread'); // replace the sender email | |
| threads.forEach(function(thread) { | |
| var messages = thread.getMessages(); | |
| messages.forEach(function(message) { | |
| var body = message.getBody(); | |
| // 2. Find first link starting with http://www.sport.ed.ac.uk | |
| // https://www.sport.ed.ac.uk/online-booking-payment/Receipt?TransactionID=x&BasketId=x | |
| var match = body.match(/https:\/\/www\.sport\.ed\.ac\.uk[^\s"]+/); | |
| if (!match) return; | |
| // https://www.sport.ed.ac.uk/online-booking-payment/Receipt/Print?TransactionID=x&BasketId=x&VatReceipt=False | |
| var bookingUrl = match[0]; | |
| bookingUrl = bookingUrl.replace("Receipt?",'Receipt/Print?') | |
| bookingUrl = bookingUrl.replace("&",'&') | |
| bookingUrl += '&VatReceipt=False'; | |
| Logger.log("Found booking link: " + bookingUrl); | |
| // 3. Fetch the booking page | |
| var html = UrlFetchApp.fetch(bookingUrl).getContentText() | |
| // Logger.log(html) | |
| // Extract event name | |
| var eventNameMatch = html.match(/<br \/>\s*(.*?)\s*<br \/>\s*(.*?)\s*<br \/>/); | |
| var eventName = eventNameMatch ? eventNameMatch[1].trim() : "Unknown Event"; | |
| var room = (eventNameMatch && eventNameMatch.length>2) ? eventNameMatch[2].trim() : "Unknown Event"; | |
| // Extract date | |
| var dateMatch = html.match(/<div class="Date"><p>(.*?)<\/p>/); | |
| var eventDate = dateMatch ? dateMatch[1] : "Unknown Date"; | |
| var eventDates =eventDate.split('/') | |
| // Extract time | |
| var timeMatch = html.match(/<div class="Time"><p>(.*?)<\/p>/) | |
| var eventTime = timeMatch ? timeMatch[1] : "Unknown Time"; | |
| var eventTimes =eventTime.split(':') | |
| // duration time <div class="Duration"><p>60 minutes</p></div> | |
| var durMatch = html.match(/<div class="Duration"><p>(\d+)(.*?)<\/p>/) | |
| var dur = durMatch ? durMatch[1] : 60; // default 60 min | |
| if (!dateMatch || !timeMatch) { | |
| Logger.log("Could not parse date/time from booking page."); | |
| return; | |
| } | |
| // 5. Convert to Date object (local time) | |
| var eventDateTime = new Date(eventDates[2],eventDates[1]-1,eventDates[0],eventTimes[0],eventTimes[1],); | |
| var startTime = new Date(eventDateTime); | |
| var endTime = new Date(eventDateTime); | |
| endTime.setMinutes(endTime.getMinutes() + parseInt(dur)); | |
| // 6. Add to Google Calendar | |
| const calendar = CalendarApp.getDefaultCalendar(); | |
| var event = calendar.createEvent(eventName, startTime, endTime, { | |
| location: room, | |
| description: "Imported automatically from booking email: " + bookingUrl | |
| }); | |
| // 7. Add 30-min reminder | |
| event.addPopupReminder(30); | |
| Logger.log("Event created: " + event.getTitle()); | |
| }); | |
| // Mark thread as read to avoid reprocessing | |
| thread.markRead(); | |
| }); | |
| } |
Author
ynrng
commented
Aug 15, 2025
- Go to Google Apps Script.
- Create a new project and paste in the code above.
- In Services, enable:
- Gmail API
- Calendar API
- Save the project.
- Click Triggers → Add Trigger:
- Function: processBookingEmails
- Event Source: Time-driven
- Interval: Every 5–15 minutes (your choice)
- Authorise the script when prompted.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment