org-modeorg-babel

How to access data from many tables in src blocks


In my org document, I have several tables named (with #+name:) t1, t2, etc. I want to pass all of the tables to some lisp code. This is what I have so far:

#+name: process-tables
#+header: :var t1=t1 t2=t2 t3=t3 t4=t4 t5=t5 t6=t6 t7=t7 t8=t8 t9=t9 t10=t10 t11=t11 t12=t12 t13=t13 t14=t14
#+BEGIN_SRC emacs-lisp
    (process (append t1 t2 t3 t4 t5 t6 t7 t8 t9 t10 t11 t12 t13 t14))
#+END_SRC

This seems very clumsy. Is there a better way? I do not want to merge the tables in the org document.


Solution

  • You can try the org-table-map-tables and org-table-to-lisp functions to create a list of all tables in the buffer. This avoids having to invoke table names individually.

    (defun org-tables-to-list ()
      (let (tbls)
        (org-table-map-tables
         (lambda ()
           (push (org-table-to-lisp) tbls))
         t)
        (apply #'append (nreverse tbls))))
    

    For example:

    #+name: t1
    | 0 | 1 |
    
    #+name: t2
    | 2 | 3 |
    
    #+name: process-tables
    #+BEGIN_SRC emacs-lisp :results value verbatim
    (org-tables-to-list)
    #+END_SRC
    
    #+RESULTS: process-tables
    : (("0" "1") ("2" "3"))