In the process of building an online appointment booking app and wanted to provide the user with the ability to add their confirm appointment to their calendar via an ‘Add to Calendar’ button.
After a discussion and some advice from Charles I have managed to implement this using the following technique so thought it would be a good thing to post in case anyone else wants to achieve this.
My approach is based on nwcell’s Github repository which allows for a fully JS approach. In BF I did the following:
-
Created an HTML element on my page with a button containing and @click=“namedAction(‘addToCalendar’,{})” attribute.
-
In Misc I added the addToCalendar namedAction with a function - Runs Javascript action.
-
In the function I loaded the JS from the Github example.html and then updated the cal_single.addEvent() function to use the required values from my BF model.
-
Added the cal_single.download() function which in example.html was part of the button below the JS and cal_single.addEvent() function.
Points of Interest:
-
Spent ages trying to get the ics DTSTART & DTEND values to a format that worked using moment.js. I tried to get them to the format show in the ics file which is YYYYMMDDTHHmmss. After much head scratching I managed to find the allowed format in the JS (step 3 above). For a date and time the format required is ddd MMM DD YYYY HH:mm:ss
-
I tweaked the JS (step 3 again) to modify the ics card to include a reminder 60 minutes before the start of the appointment by adding:
“BEGIN:VALARM”,
“ACTION:DISPLAY”,
“DESCRIPTION:REMINDER”,
“TRIGGER:-PT60M”,
“END:VALARM”,
before the final “END:VEVENT” tag. This could probably be added to the cal_single.addEvent() as an additional parameter by someone with better JS skills than I have!
- If you want to pass in a full LOCATION make sure the commas between the elements of the address are preceded by a backslash i.e. , as otherwise only the first line of the address is added.