0

I have a bunch of recipes under different headline, and each have an ingredient list specified as a table.

I would like to access that table as an list in a src block using the headline id, same as if I where to use the name table name (which is very easy)

Truth to be told I have a usecase where I'd like to do the same for lists.

* Recepies
** Killer Sandwich
   :PROPERTIES:
   :ID: 0123
   :END:
   A really good sandwich!
   #+name: sandwich_recipie
   | 1   | piece | bread |
   | 100 | g     | cheese |
   | 100 | g     | butter |

#+begin_src elisp :var data=sandwich_recipie headline_id=0123 :results output
(= sandwich_data (magic-org-function-to-get-first-table-as-list headline_id))
#+end_src

So far I've been looking into org-element, but it seems like I'm not smart enough to use that... sorry

1

1 Answer 1

1

It seems you have figured out almost everything. Using :var data=heading and #+name: heading already does the conversion of a table into a list for you and binds it to a variable by that name. Here's a full example:

* Recipes
** Killer Sandwich
   :PROPERTIES:
   :ID: 0123
   :END:
   A really good sandwich!
   #+name: sandwich_recipe
   | 1   | piece | bread |
   | 100 | g     | cheese |
   | 100 | g     | butter |

#+begin_src elisp :var data=sandwich_recipe headline-id="0123" :results output
(require 'seq)
(princ (format "Recipe: %s\n" headline-id))
(dolist (line data)
  (seq-let (amount unit ingredient) line
    (princ (format "%s%s of %s\n"
                   (if (= amount 1) "one " amount)
                   unit
                   ingredient))))
#+end_src

#+RESULTS:
: Recipe: 0123
: one piece of bread
: 100g of cheese
: 100g of butter

Here's a simpler source block that shows just the conversions:

#+begin_src elisp :var data=sandwich_recipe :results value drawer
data
#+end_src

#+RESULTS:
:results: 
((1 piece bread) (100 g cheese) (100 g butter))
:end:
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.