瀏覽代碼

New functions to export tables to csv and tsv.

The new functionas are orgtbl-to-csv and orgtbl-to-tsv.
Carsten Dominik 17 年之前
父節點
當前提交
9c8aabbf81
共有 3 個文件被更改,包括 22 次插入0 次删除
  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>
 
+	* 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.

+ 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
 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

+ 6 - 0
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."