emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Elisp macro for working with org tables


From: Ihor Radchenko
Subject: Re: Elisp macro for working with org tables
Date: Wed, 03 Aug 2022 18:13:43 +0800

Mark Clements <mark.clements@ki.se> writes:

> In response to a question on Stackoverflow 
> (https://stackoverflow.com/questions/50921305/org-table-and-named-columns-for-babel-processing),
>  I have written an elisp macro to work with org tables:
>
> #+NAME: test-table
> | a | b |
> |---+---|
> | 1 | 2 |
> | 3 | 4 |
> ....
> Comments are welcome -- and I hope that this is useful.

Usually, we use table formulas in such situations.
Table formulas support named columns, lines, cells and constants.

> #+NAME: test-table-script
> #+BEGIN_SRC emacs-lisp :var table=test-table :colnames no
>   (require 'cl)
>   (defmacro with-table (table expression)
>     "Given an org table with colnames, evaluate an expression using the 
> colnames.
> Note: use ':colnames no' in the header -- this imports the names but does not 
> export them."
>     `(let* ((table-with-colnames (org-babel-get-colnames ,table))
>             (names (loop for name in (cdr table-with-colnames)
>                          collect (if (stringp name) (intern name) name))))
>        (funcall
>         `(lambda (data)
>                    (mapcar (lambda (row)
>                              (cl-destructuring-bind ,names row ,',expression))
>                            data))

Note that NAMES may contain forbidden chars for Elisp symbols.

Best,
Ihor



reply via email to

[Prev in Thread] Current Thread [Next in Thread]