Skip to content

Instantly share code, notes, and snippets.

@florisvanvugt
Last active May 30, 2018 16:34
Show Gist options
  • Save florisvanvugt/c1c0e1c9a782b46430cf2854238a285f to your computer and use it in GitHub Desktop.
Save florisvanvugt/c1c0e1c9a782b46430cf2854238a285f to your computer and use it in GitHub Desktop.
Exporting Emacs Orgmode calendar to Google Calendar
;;; Taken pretty much verbatim from https://orgmode.org/worg/org-tutorials/org-google-sync.html
;;; Thanks to those developers!
;; https://stackoverflow.com/questions/23463962/emacs-export-calendar-bad-timezone-format-in-ics
(setq org-icalendar-timezone "America/New_York")
;;; define categories that should be excluded
(setq org-export-exclude-category (list "google" "private"))
;;; define filter. The filter is called on each entry in the agenda.
;;; It defines a regexp to search for two timestamps, gets the start
;;; and end point of the entry and does a regexp search. It also
;;; checks if the category of the entry is in an exclude list and
;;; returns either t or nil to skip or include the entry.
(defun org-mycal-export-limit ()
"Limit the export to items that have a date, time and a range. Also exclude certain categories."
(setq org-tst-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ... [0-9]\\{2\\}:[0-9]\\{2\\}[^\r\n>]*?\
\)>")
(setq org-tstr-regexp (concat org-tst-regexp "--?-?" org-tst-regexp))
(save-excursion
; get categories
(setq mycategory (org-get-category))
; get start and end of tree
(org-back-to-heading t)
(setq mystart (point))
(org-end-of-subtree)
(setq myend (point))
(goto-char mystart)
; search for timerange
(setq myresult (re-search-forward org-tstr-regexp myend t))
; search for categories to exclude
(setq mycatp (member mycategory org-export-exclude-category))
; return t if ok, nil when not ok
(if (and myresult (not mycatp)) t nil)))
;;; activate filter and call export function
;;(defun org-mycal-export ()
;; (let ((org-icalendar-verify-function 'org-mycal-export-limit))
;; (org-export-icalendar-combine-agenda-files)))
;; FVV updated 2018-05-29:
(defun org-mycal-export ()
(let ((org-icalendar-verify-function 'org-mycal-export-limit))
(org-icalendar-combine-agenda-files)))

The idea here is:

  • Use orgmode to export your agenda to an .ics file
  • Upload the .ics to a web server where you have access
  • Configure your Google Calendar to sync with this web server's .ics file

I know, it's roundabout, but it was the easiest way I could find.

To use this:

  • Replace /home/floris with your home direcvtory in export_org_calendar.sh
  • Replace YOURSERVER.COM and username and password with your access credentials to a server you have access to.
  • Make a cron job to run this periodically!

Enjoy!

#!/bin/sh
# What emacs will output to
export EMACS_ICS=/home/floris/org.ics # replace this with your own home directory obviously!
# What we will subsequently call the file
export ICS_FILE=exported.ics
cd ~/tmp # where we will put temporary files
echo =================================
echo Exporting the calendar from emacs
# First generate the calendar and export automatically to ~/org.ics
#emacsclient -e "(save-excursion (org-mycal-export))"
# (Previously, I did this with emacsclient, but I found it takes a while and it would therefore interrupt my current emacs work.)
# So now it creates a separate emacs process:
emacs --batch -l ~/.emacs --eval "( save-excursion ( org-mycal-export ) )"
echo "Moving $EMACS_ICS to $ICS_FILE"
cp -fv $EMACS_ICS $ICS_FILE
echo
# Upload to FTP server
echo ============================
echo Uploading ICS file to server
ftp -n <<EOF
open YOURSERVER.COM
user USERNAME PASSWORD
cd public_html
put $ICS_FILE
EOF
echo --- done
echo
echo Cleaning up...
echo "Calendar exported: $(date)" >> ~/lib/calendar_export_log.txt
# to run this as a cron schedule you can use:
## 5,20,35,50 * * * * /home/<USER>/lib/export_org_calendar.sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment