2

I want to have tables that I can easily export to files. In this regard I want to associate a file path and a format to tables. This page in the docs is most relevant: https://orgmode.org/manual/Built_002din-table-editor.html

I tried this to set the file path:

#+TABLE_EXPORT_FILE: 'data/observations.csv'
| day              | observation |
|------------------+-------------|
| <2019-11-14 Thu> | cold        |
| <2019-11-14 Thu> | warm        |

And then running org-table-export but I still get the prompt asking for a file.

I am fine with a per-file setting of the file format, i.e.:

# Local Variables:
# mode: org
# org-table-export-default-format: orgtbl-to-csv
# End:

But it would be nice to set that as well on a per table basis.

I also would like a way to run this for all tables at once (ideally in combination with org-babel-tangle). I'll ask another Q for that if you want the points :)

  • 1
    I just need tangling tables for myself. Added an alternative answer doing that. – Tobias Feb 26 at 2:43
  • Awesome, I'm changing the accepted answer to that one since it is exactly what the question asks. – salotz Feb 26 at 18:07
1

The following Elisp code couples Org table export to csv/tsv with org-babel-tangle.

Tables with the following affiliated attribute are exported to data files on org-babel-tangle:
#+ATTR_TANGLE: DATAFILE

You can also specify the export type:
#+ATTR_TANGLE: ("DATAFILE" "TYPE")

(defun org+-tangle-table-at-point ()
  "Tangle Org table at point.
The table is ignored if it is not preceeded by a line like:
#+ATTR_TANGLE: DATAFILE
or
#+ATTR_TANGLE: (\"DATAFILE\" \"TYPE\")"
  (interactive)
  (unless (org-at-table-p)
    (user-error "Not at Org table"))
  (let* (type
     (data (org-element-context))
     (parent (org-element-property :parent data))
     file)
    (while parent
      (setq data parent)
      (setq parent (org-element-property :parent data)))
    (when (and (setq prop (org-element-property :attr_tangle data))
           (setq file (read prop)))
      (cond
       ((symbolp file)
    (setq file (symbol-name file)))
       ((stringp file)) ;; keep it that way
       ((consp file)
    (setq type (nth 1 file)
          file (car file)))
       (t
    (user-error "Unexpected format of table to be tangled")))
      (org-table-export file (or type org-table-export-default-format))
      t)))

(defun org+-tangle-tables (fun &optional arg target-file lang)
  "Tangle Org tables in current buffer.
For around advice with `org-babel-tangle' as FUN.
See `org-babel-tangle' for the args ARG, TARGET-FILE and LANG.

Currently, only one table per file is possible."
  (cond
   ((equal arg '(4)) ;; at point
    (if (org-at-table-p)
    (org+-tangle-table-at-point)
      (funcall fun arg target-file lang)))
   ((equal arg '(16))
    (funcall fun arg target-file lang))
   (t
    (funcall fun arg target-file lang)
    (let ((table-count 0))
      (org-with-wide-buffer
       (goto-char (point-min))
       (while (re-search-forward "^[ \t\r]*|" nil t)
     (goto-char (match-beginning 0))
     (when (org+-tangle-table-at-point)
       (cl-incf table-count))
     (goto-char (org-table-end)))
       )
      (message "Tangled %d tables from %s." table-count (buffer-name))))))

(advice-add 'org-babel-tangle :around #'org+-tangle-tables)

Example Org source file with two exported tables:

#+ATTR_TANGLE: /tmp/test.dat
#+NAME: myTable
| test   | table |
| first  | row   |
| second | row   |
| third  | row   |

Some intermediate text.

Some src block:
#+BEGIN_SRC emacs-lisp :tangle /tmp/test.el
"Here I am."
#+END_SRC

#+ATTR_TANGLE: ("/tmp/test1.dat" "orgtbl-to-csv")
| another | table |
|---------+-------|
|      11 |    12 |
|      21 |    22 |
| improve this answer | |
1

The doc you linked says:

You may also use properties TABLE_EXPORT_FILE and TABLE_EXPORT_FORMAT to specify the file name and the format for table export in a subtree.

They are properties and not keywords. So you set them in the properties drawer of the subtree.

Example:

* Section with table
:PROPERTIES:
:TABLE_EXPORT_FILE: data/observations.csv
:TABLE_EXPORT_FORMAT: orgtbl-to-csv
:END:

| day              | observation |
|------------------+-------------|
| <2019-11-14 Thu> | cold        |
| <2019-11-14 Thu> | warm        |
| improve this answer | |
  • This works. Seems counterintuitive though imo. It doesn't export the header row though... – salotz Nov 15 '19 at 17:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.