Browse Source

New functions to export tables to csv and tsv.

The new functionas are orgtbl-to-csv and orgtbl-to-tsv.
Carsten Dominik 17 years ago
parent
commit
9c8aabbf81
3 changed files with 22 additions and 0 deletions
  1. 4 0
      lisp/ChangeLog
  2. 12 0
      lisp/org-table.el
  3. 6 0
      lisp/org.el

+ 4 - 0
lisp/ChangeLog

@@ -1,5 +1,9 @@
 2008-05-10  Carsten Dominik  <dominik@science.uva.nl>
 2008-05-10  Carsten Dominik  <dominik@science.uva.nl>
 
 
+	* 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
 	* org-table.el (org-table-export-default-format): Remove :splice
 	from default format, we get the same effect by not specifying
 	from default format, we get the same effect by not specifying
 	:tstart and :tend.
 	:tstart and :tend.

+ 12 - 0
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
 first checks if there is an export format specified in a TABLE_EXPORT_FORMAT
 property, locally or anywhere up in the hierarchy."
 property, locally or anywhere up in the hierarchy."
   (interactive)
   (interactive)
+  (unless (org-at-table-p)
+    (error "No table at point"))
   (require 'org-exp)
   (require 'org-exp)
   (org-table-align) ;; make sure we have everything we need
   (org-table-align) ;; make sure we have everything we need
   (let* ((beg (org-table-begin))
   (let* ((beg (org-table-begin))
@@ -3818,6 +3820,16 @@ directly by `orgtbl-send-table'.  See manual."
 				       (remq nil *orgtbl-rtn*)
 				       (remq nil *orgtbl-rtn*)
 				     *orgtbl-rtn*)) "\n")))
 				     *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)
 (defun orgtbl-to-latex (table params)
   "Convert the orgtbl-mode TABLE to LaTeX.
   "Convert the orgtbl-mode TABLE to LaTeX.
 TABLE is a list, each entry either the symbol `hline' for a horizontal
 TABLE is a list, each entry either the symbol `hline' for a horizontal

+ 6 - 0
lisp/org.el

@@ -12920,6 +12920,12 @@ With optional NODE, go directly to that node."
       (display-buffer buf)
       (display-buffer buf)
       (sit-for 0))))
       (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)
 (defun org-plist-delete (plist property)
   "Delete PROPERTY from PLIST.
   "Delete PROPERTY from PLIST.
 This is in contrast to merely setting it to 0."
 This is in contrast to merely setting it to 0."