emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Elisp macro for working with org tables
@ 2022-08-03  8:33 Mark Clements
  2022-08-03 10:13 ` Ihor Radchenko
  0 siblings, 1 reply; 2+ messages in thread
From: Mark Clements @ 2022-08-03  8:33 UTC (permalink / raw)
  To: emacs-orgmode@gnu.org

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 |

#+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))
        (car table-with-colnames))))
  ;; example to add a third column
  (cons '(a b c) (cons 'hline (with-table table (list a b (+ a b)))))
#+END_SRC

#+RESULTS: test-table-script
| a | b | c |
|---+---+---|
| 1 | 2 | 3 |
| 3 | 4 | 7 |

Comments are welcome -- and I hope that this is useful.

Sincerely, Mark.


När du skickar e-post till Karolinska Institutet (KI) innebär detta att KI kommer att behandla dina personuppgifter. Här finns information om hur KI behandlar personuppgifter<https://ki.se/medarbetare/integritetsskyddspolicy>.


Sending email to Karolinska Institutet (KI) will result in KI processing your personal data. You can read more about KI’s processing of personal data here<https://ki.se/en/staff/data-protection-policy>.

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Elisp macro for working with org tables
  2022-08-03  8:33 Elisp macro for working with org tables Mark Clements
@ 2022-08-03 10:13 ` Ihor Radchenko
  0 siblings, 0 replies; 2+ messages in thread
From: Ihor Radchenko @ 2022-08-03 10:13 UTC (permalink / raw)
  To: Mark Clements; +Cc: emacs-orgmode@gnu.org

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


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-08-03 10:14 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-08-03  8:33 Elisp macro for working with org tables Mark Clements
2022-08-03 10:13 ` Ihor Radchenko

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).