From mboxrd@z Thu Jan 1 00:00:00 1970 From: Alan Schmitt Subject: Re: manipulate org tables using emacs-lisp Date: Mon, 03 Oct 2016 14:58:52 +0200 Message-ID: References: <87k2dtm7or.fsf@gmail.com> Mime-Version: 1.0 Content-Type: multipart/signed; boundary="=-=-="; micalg=pgp-sha512; protocol="application/pgp-signature" Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:52356) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1br2pi-00046c-23 for emacs-orgmode@gnu.org; Mon, 03 Oct 2016 08:59:03 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1br2pd-0003tD-28 for emacs-orgmode@gnu.org; Mon, 03 Oct 2016 08:59:02 -0400 Received: from mail2-relais-roc.national.inria.fr ([192.134.164.83]:9300) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1br2pc-0003so-Rs for emacs-orgmode@gnu.org; Mon, 03 Oct 2016 08:58:56 -0400 In-Reply-To: (Heikki Lehvaslaiho's message of "Sun, 2 Oct 2016 10:19:28 +0300") List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: "Emacs-orgmode" To: Heikki Lehvaslaiho Cc: emacs-orgmode@gnu.org, Thorsten Jolitz --=-=-= Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Thanks a lot for all the suggestions. For the moment I=E2=80=99ve put the t= able at the beginning of the file, but I=E2=80=99ll probably tweak the following functions to use Heikki=E2=80=99s trick of using a regexp to find the named table. I don=E2=80=99t 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=E2=80=99m interested in, then use org-= table functions to directly change the table. Here is the code, if it=E2=80=99s 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 =2D-=20 OpenPGP Key ID : 040D0A3B4ED2E5C7 Monthly Athmospheric CO=E2=82=82, Mauna Loa Obs. 2016-08: 402.25, 2015-08: = 398.93 --=-=-= Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQEcBAEBCgAGBQJX8lYNAAoJEAQNCjtO0uXHBHoIAJamFkYMjPUPcC2R6ICeFnMY svkHLZeK0h0mc1zm+RNNMnCSr7eb1HV1TyziP93AbdNJK2N9/IOBAR/wzK7F+C0Y C0N+j16YjXkcXNL1AwPnL7etZt9a2/7Hcm9jpSVCODBb2xdlh0IFmSl3SCK7ILPy cuxQpRmCylcpcoGqRumcKfknkMoW6fwdWAtW4FfK2O0VjDRjmHLSH05XlsZG9LRc jpMrv6dSvoDL65rnqkBkcQLZPhxjFAAu5MqaQaTm+VGQEb1BYus2y4viALzI/6GM A/3Ygc2j/lJ3Y3PBOoVq+6VNo9Y9omyQJILkb0AsP3fAVn4Gf9knCUePO/TBvA4= =UKC4 -----END PGP SIGNATURE----- --=-=-=--