3

I've this table:

|H1    |H2    |H3    |
|------+------+------|
|text11|text12|text13|
|text21|text22|text23|
|...   |...   |...   |
|textN1|textN2|textN3|
|--------------------|

and i want this list:

* text11
- H1: text11
- H2: text12
- H3: text13

* text21
- H1: text21
- H2: text22
- H3: text23

...

* textN1
- H1: textN1
- H2: textN2
- H3: textN3

How do I do that?

4

EDIT: Updated code because I had misunderstood what the headings should be, and to switch to s-join because apparently @lap's version of Emacs doesn't have string-join from subr-x (s-join and string-join take their arguments in opposite order!).

EDIT: Last fix: OP is using an older version of Org Mode and the parsed table includes the symbol hline to indicate the position of horizontal lines, so we remq them away.

You can name the table and then use a :var declaration in a source block and Org Mode will parse the table for you!

#+NAME: laps-table
|H1    |H2    |H3    |
|------+------+------|
|text11|text12|text13|
|text21|text22|text23|
|...   |...   |...   |
|textN1|textN2|textN3|
|--------------------|

#+BEGIN_SRC emacs-lisp :var tbl=laps-table
  (require 's)
  (let ((headings (car tbl)))
    (s-join
     "\n"
     (mapcar
      (lambda (row)
        (concat
         (format "* %s\n- " (car row))
         (s-join
          "\n- "
          (cl-map 'list 
                  (lambda (hd x) (concat hd ": " x))
                  headings row))))
      (remq 'hline (cdr tbl)))))
#+END_SRC

Running the source block results in the following:

#+RESULTS:
#+begin_example
* text11
- H1: text11
- H2: text12
- H3: text13
* text21
- H1: text21
- H2: text22
- H3: text23
* ...
- H1: ...
- H2: ...
- H3: ...
* textN1
- H1: textN1
- H2: textN2
- H3: textN3
#+end_example
  • I've this error with your code: Symbol's function definition is void: string-join – lap Sep 26 '16 at 12:10
  • ...not... text1, text2, etc... i want to insert text11, text21, etc. IN MY table... thanks! – lap Sep 26 '16 at 12:12
  • string-join is relatively new in Emacs, if you are using an old version of Emacs you can install a library called s and replace string-join with s-join. – Omar Sep 26 '16 at 16:30
  • I loaded the library s.el and replaced string-join with s-join but running the code still get errors (wrong type argument: sequencep, hline) – lap Sep 26 '16 at 17:11
  • What version of Emacs and what version of Org Mode are you using, @lap? – Omar Sep 26 '16 at 17:56

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.