library of babel: support for reading/writing to/from Google docs

This commit is contained in:
Eric Schulte 2010-07-19 17:12:53 -07:00
parent 78ef1ed410
commit a618566cd6
1 changed files with 70 additions and 2 deletions

View File

@ -54,7 +54,7 @@ as a table in traditional Org-mode table syntax.
#+end_src
** remote files
**** json
Read local or remote file in [[http://www.json.org/][json]] format into emacs-lisp objects.
#+srcname: json
#+begin_src emacs-lisp :var file='() :var url='()
@ -72,13 +72,81 @@ Read local or remote file in [[http://www.json.org/][json]] format into emacs-li
(json-read))))
#+end_src
**** Google docs
The following code blocks make use of the [[http://code.google.com/p/googlecl/][googlecl]] Google command line
tool. This tool provides functionality for accessing Google services
from the command line, and the following code blocks use /googlecl/
for reading from and writing to Google docs with Org-mode code blocks.
****** read a document from Google docs
The =google= command seems to be throwing "Moved Temporarily" errors
when trying to download textual documents, but this is working fine
for spreadsheets.
#+source: gdoc-read
#+begin_src emacs-lisp :var title="example"
(let* ((format (if (member "vector" result-params) "csv" "txt"))
(file (concat title "." format))
(cmd (format "google docs get --format %S --title %S" format title)))
(message cmd) (message (shell-command-to-string cmd))
(prog1 (if (string= format "csv")
(with-temp-buffer
(org-table-import (shell-quote-argument file) nil)
(org-table-to-lisp))
(with-temp-buffer
(insert-file-contents (shell-quote-argument file))
(buffer-string)))
(delete-file file)))
#+end_src
For example, a line like the following can be used to read the
contents of a spreadsheet named =num-cells= into a table.
#+begin_src org
,#+call: gdoc-read(title="num-cells"") :results vector
#+end_src
A line like the following can be used to read the contents of a
document as a string.
#+begin_src org
,#+call: gdoc-read(title="loremi") :results scalar
#+end_src
****** write a document to a Google docs
Write =data= to a google document named =title=. If =data= is tabular
it will be saved to a spreadsheet, otherwise it will be saved as a
normal document.
#+source: gdoc-write
#+begin_src emacs-lisp :var title="babel-upload" :var data=fibs(n=10) :results silent
(let* ((format (if (listp data) "csv" "txt"))
(tmp-file (make-temp-file "org-babel-google-doc" nil (concat "." format)))
(cmd (format "google docs upload --title %S %S" title tmp-file)))
(with-temp-file tmp-file
(insert
(if (listp data)
(orgtbl-to-csv
data '(:fmt (lambda (el) (if (stringp el) el (format "%S" el)))))
(if (stringp data) data (format "%S" data)))))
(message cmd)
(prog1 (shell-command-to-string cmd) (delete-file tmp-file)))
#+end_src
example usage
#+begin_src org
,#+source: fibs
,#+begin_src emacs-lisp :var n=8
, (flet ((fib (m) (if (< m 2) 1 (+ (fib (- m 1)) (fib (- m 2))))))
, (mapcar (lambda (el) (list el (fib el))) (number-sequence 0 (- n 1))))
,#+end_src
,#+call: gdoc-write(title="fibs", data=fibs(n=10))
#+end_src
* Plotting code
** R
Plot column 2 (y axis) against column 1 (x axis). Columns 3 and beyond, if present, are ignored.
#+srcname: R-plot(data=R-plot-example-data)
#+begin_src R :session *R*
#+begin_src R
plot(data)
#+end_src