emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Ihor Radchenko <yantar92@gmail.com>
To: Mark Clements <mark.clements@ki.se>
Cc: "emacs-orgmode@gnu.org" <emacs-orgmode@gnu.org>
Subject: Re: Elisp macro for working with org tables
Date: Wed, 03 Aug 2022 18:13:43 +0800	[thread overview]
Message-ID: <87r11xiqi0.fsf@localhost> (raw)
In-Reply-To: <GV3P280MB00019F1DDCF8BD0764CECDA3F09C9@GV3P280MB0001.SWEP280.PROD.OUTLOOK.COM>

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	other threads:[~2022-08-03 10:14 UTC|newest]

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

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=87r11xiqi0.fsf@localhost \
    --to=yantar92@gmail.com \
    --cc=emacs-orgmode@gnu.org \
    --cc=mark.clements@ki.se \
    /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).