From 9c8aabbf81288f222343e958e0225f0c79fa9129 Mon Sep 17 00:00:00 2001 From: Carsten Dominik Date: Sat, 10 May 2008 13:40:25 +0200 Subject: [PATCH] New functions to export tables to csv and tsv. The new functionas are orgtbl-to-csv and orgtbl-to-tsv. --- lisp/ChangeLog | 4 ++++ lisp/org-table.el | 12 ++++++++++++ lisp/org.el | 6 ++++++ 3 files changed, 22 insertions(+) diff --git a/lisp/ChangeLog b/lisp/ChangeLog index 3ecad1fce..42382aa5e 100644 --- a/lisp/ChangeLog +++ b/lisp/ChangeLog @@ -1,5 +1,9 @@ 2008-05-10 Carsten Dominik + * org-table.el (orgtbl-to-tsv, orgtbl-to-csv): New functions. + + * org.el (org-quote-csv-field): New functions. + * org-table.el (org-table-export-default-format): Remove :splice from default format, we get the same effect by not specifying :tstart and :tend. diff --git a/lisp/org-table.el b/lisp/org-table.el index 86e5f09ba..009c2eed1 100644 --- a/lisp/org-table.el +++ b/lisp/org-table.el @@ -438,6 +438,8 @@ be found in the variable `org-table-export-default-format', but the function first checks if there is an export format specified in a TABLE_EXPORT_FORMAT property, locally or anywhere up in the hierarchy." (interactive) + (unless (org-at-table-p) + (error "No table at point")) (require 'org-exp) (org-table-align) ;; make sure we have everything we need (let* ((beg (org-table-begin)) @@ -3818,6 +3820,16 @@ directly by `orgtbl-send-table'. See manual." (remq nil *orgtbl-rtn*) *orgtbl-rtn*)) "\n"))) +(defun orgtbl-to-tsv (table params) + "Convert the orgtbl-mode table to TAB separated material." + (orgtbl-to-generic table (org-combine-plists '(:sep "\t") params))) +(defun orgtbl-to-csv (table params) + "Convert the orgtbl-mode table to CSV material. +This does take care of the proper quoting of fields with comma or quotes." + (orgtbl-to-generic table (org-combine-plists + '(:sep "," :fmt org-quote-csv-field) + params))) + (defun orgtbl-to-latex (table params) "Convert the orgtbl-mode TABLE to LaTeX. TABLE is a list, each entry either the symbol `hline' for a horizontal diff --git a/lisp/org.el b/lisp/org.el index cf98ed232..37dd8815a 100644 --- a/lisp/org.el +++ b/lisp/org.el @@ -12920,6 +12920,12 @@ With optional NODE, go directly to that node." (display-buffer buf) (sit-for 0)))) +(defun org-quote-csv-field (s) + "Quote field for inclusion in CSV material." + (if (string-match "[\",]" s) + (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"") + s)) + (defun org-plist-delete (plist property) "Delete PROPERTY from PLIST. This is in contrast to merely setting it to 0." -- 2.11.4.GIT