Org Mode Insert Table Reference
Emacs Org-Mode tables are very powerful. The formula editor can execute elisp commands which makes for a really powerful spreadsheet editor (especially when combined with org-sbe
). One big limitation I've faced is multi-line content.
To solve this I've created a simple function to wrap around results of some elisp block to always return the most useful data.
(org-table-create-and-insert-reference "hello")
-> "hello"
(org-table-create-and-insert-reference "")
-> "nil"
(org-table-create-and-insert-reference nil)
-> "nil"
(org-table-create-and-insert-reference "hello\nworld")
-> [[table-results-nnnnn]]
+ an inserted named block at the bottom of the buffer
(defun org-table-create-and-insert-reference (results)
(let* ((return "")
(temp-name (make-temp-name "table-result-"))
(block-template "\n\n#+name: %s\n#+begin_quote\n%s\n#+end_quote")
)
(cond ((or (string= results "") (not results))
(setq return "nil"))
((= 1 (length (string-lines results)))
(setq return results))
(t
(progn
(save-excursion
(goto-char (point-max))
(insert (format block-template temp-name results))
)
(setq return (format "[[%s]]" temp-name))))
)
return)
)