From: Alan Schmitt <alan.schmitt@polytechnique.org>
To: Heikki Lehvaslaiho <heikki.lehvaslaiho@gmail.com>
Cc: emacs-orgmode@gnu.org, Thorsten Jolitz <tjolitz@gmail.com>
Subject: Re: manipulate org tables using emacs-lisp
Date: Mon, 03 Oct 2016 14:58:52 +0200 [thread overview]
Message-ID: <m2vax937xv.fsf@charm-ecran.irisa.fr> (raw)
In-Reply-To: <CAK=ukcZSwjfU8Xf78mFOY7qWUsi7BbQzAEct4sGiNTL2vMV1Hw@mail.gmail.com> (Heikki Lehvaslaiho's message of "Sun, 2 Oct 2016 10:19:28 +0300")
[-- Attachment #1: Type: text/plain, Size: 2639 bytes --]
Thanks a lot for all the suggestions. For the moment I’ve put the table
at the beginning of the file, but I’ll probably tweak the following
functions to use Heikki’s trick of using a regexp to find the named
table.
I don’t think I want to convert the table to a lisp structure, work on
it, and output back the table as I want to preserve the formulas at the
end of the table. So what I do instead is convert it to a lisp structure
to find the row and column numbers I’m interested in, then use org-table
functions to directly change the table.
Here is the code, if it’s helpful to others.
Thanks again,
Alan
#+BEGIN_SRC emacs-lisp
(defun as/get-row-by-name (name)
"This assumes the table is at the beginning of the file"
(save-excursion
(goto-char (point-min))
(let ((tb (org-table-to-lisp))
(row 1)
(res))
(while (and (not res) tb)
(unless (equal (car tb) 'hline)
(if (equal (caar tb) name)
(setq res row)
(setq row (+ row 1))))
(setq tb (cdr tb)))
res)))
(defun as/get-column-by-name (name)
"This assumes the table is at the beginning of the file"
(save-excursion
(goto-char (point-min))
(let ((first-row (car (org-table-to-lisp)))
(col 1)
(res))
(while (and (not res) first-row)
(if (equal (car first-row) name)
(setq res col)
(setq col (+ col 1)))
(setq first-row (cdr first-row)))
res)))
(defun as/get-clean-value (rowname colname)
"This assumes the table is at the start of the file"
(save-excursion
(goto-char (point-min))
(let ((row (as/get-row-by-name rowname))
(col (as/get-column-by-name colname)))
(unless (and row col) (error "row name or column name not found"))
(let ((res (org-trim (org-table-get row col))))
(set-text-properties 0 (length res) nil res)
res))))
(defun as/set-table-value (rowname colname value)
"This assumes the table is at the start of the file"
(save-excursion
(goto-char (point-min))
(let ((row (as/get-row-by-name rowname))
(col (as/get-column-by-name colname)))
(unless (and row col) (error "row name or column name not found"))
(org-table-goto-line row)
(org-table-get-field col value)
(org-table-align))))
#+END_SRC
--
OpenPGP Key ID : 040D0A3B4ED2E5C7
Monthly Athmospheric CO₂, Mauna Loa Obs. 2016-08: 402.25, 2015-08: 398.93
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 454 bytes --]
prev parent reply other threads:[~2016-10-03 12:59 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-09-30 6:27 manipulate org tables using emacs-lisp Alan Schmitt
2016-09-30 20:52 ` Thorsten Jolitz
2016-10-01 8:44 ` Alan Schmitt
2016-10-01 16:32 ` Thorsten Jolitz
2016-10-02 7:19 ` Heikki Lehvaslaiho
2016-10-03 12:58 ` Alan Schmitt [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=m2vax937xv.fsf@charm-ecran.irisa.fr \
--to=alan.schmitt@polytechnique.org \
--cc=emacs-orgmode@gnu.org \
--cc=heikki.lehvaslaiho@gmail.com \
--cc=tjolitz@gmail.com \
/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).