Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Howto convert org-mode table to original tabbed format?

Tags:

emacs

org-mode

I am converting a region to a table by using C-c |.

Is there a way to reverse the process, say after converting do some editing and go back to original format (tab separated values will do)?

I know that I can do it via org-table-export but that is too cumbersome.

like image 382
ᐅdevrimbaris Avatar asked Jul 18 '13 07:07

ᐅdevrimbaris


2 Answers

I needed this too and just wrote the following based on org-table-export:

(defun org-table-transform-in-place ()
  "Just like `ORG-TABLE-EXPORT', but instead of exporting to a
  file, replace table with data formatted according to user's
  choice, where the format choices are the same as
  org-table-export."
  (interactive)
  (unless (org-at-table-p) (user-error "No table at point"))
  (org-table-align)
  (let* ((format
      (completing-read "Transform table function: "
               '("orgtbl-to-tsv" "orgtbl-to-csv" "orgtbl-to-latex"
                 "orgtbl-to-html" "orgtbl-to-generic"
                 "orgtbl-to-texinfo" "orgtbl-to-orgtbl"
                 "orgtbl-to-unicode")))
     (curr-point (point)))
    (if (string-match "\\([^ \t\r\n]+\\)\\( +.*\\)?" format)
    (let ((transform (intern (match-string 1 format)))
          (params (and (match-end 2)
               (read (concat "(" (match-string 2 format) ")"))))
          (table (org-table-to-lisp
              (buffer-substring-no-properties
               (org-table-begin) (org-table-end)))))
      (unless (fboundp transform)
        (user-error "No such transformation function %s" transform))
      (save-restriction
        (with-output-to-string
          (delete-region (org-table-begin) (org-table-end))
          (insert (funcall transform table params) "\n")))
      (goto-char curr-point)
      (beginning-of-line)
      (message "Tranformation done."))
      (user-error "Table export format invalid"))))

(define-key org-mode-map (kbd "\C-x |") 'org-table-transform-in-place)

It'd be great if this got added to org-mode proper as I think many would use it.

like image 99
Joe Avatar answered Oct 21 '22 04:10

Joe


Try orgtbl-to-tsv for tab-separated values.

There is also orgtbl-to-csv for comma-separated values.

Combining the table with a short code block to do the conversion is convenient. For example:

* Some heading
        
#+name: foo 
| a | b | c |
|---+---+---|
| 1 | 2 | 3 |
| 4 | 5 | 6 |
        

#+name: foo-csv
#+BEGIN_SRC elisp :var x=foo :wrap example
(orgtbl-to-csv x nil)
#+END_SRC

#+RESULTS: foo-csv
#+begin_example
1,2,3
4,5,6
#+end_example

C-c C-c on the code block will produce the results shown. Adding :colnames no as a header argument to the code block will also preserve the header line:

#+name: foo-csv
#+BEGIN_SRC elisp :var x=foo :wrap example :results raw :colnames no
(orgtbl-to-csv x nil)
#+END_SRC

#+RESULTS: foo-csv
#+begin_example
a,b,c
1,2,3
4,5,6
#+end_example
like image 33
NickD Avatar answered Oct 21 '22 04:10

NickD