emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Mark Clements <mark.clements@ki.se>
To: "emacs-orgmode@gnu.org" <emacs-orgmode@gnu.org>
Subject: Elisp macro for working with org tables
Date: Wed, 3 Aug 2022 08:33:59 +0000	[thread overview]
Message-ID: <GV3P280MB00019F1DDCF8BD0764CECDA3F09C9@GV3P280MB0001.SWEP280.PROD.OUTLOOK.COM> (raw)

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>.

             reply	other threads:[~2022-08-03  9:51 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-03  8:33 Mark Clements [this message]
2022-08-03 10:13 ` Ihor Radchenko

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=GV3P280MB00019F1DDCF8BD0764CECDA3F09C9@GV3P280MB0001.SWEP280.PROD.OUTLOOK.COM \
    --to=mark.clements@ki.se \
    --cc=emacs-orgmode@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).