emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [O] manipulate org tables using emacs-lisp


From: Alan Schmitt
Subject: Re: [O] manipulate org tables using emacs-lisp
Date: Mon, 03 Oct 2016 14:58:52 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/25.1 (darwin)

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: signature.asc
Description: PGP signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]