I have a big org file with lot of topics, and every topic have a books subheading. I would like write a function that can generate a list of all books in org-table. I was trying with (org-map-entries) but no luck so far ;)
1 Answer
Something like this. Call it with M-x my-org-books-to-table RET while the org buffer with your books is active to insert the table at point:
(defun my-org-books-to-table ()
"Generate a list of books and insert as org-table."
(interactive)
(let ((item "books")
items)
;; Map through all headings with item (text of the headline) "books".
(org-map-entries
(lambda ()
(let ((lvl (number-to-string (1+ (org-outline-level)))))
;; Map through children of heading found. Add to books unless its
;; heading text matches "books".
(org-map-entries
(lambda ()
(let* ((olp (org-get-outline-path t t))
(txt (car (last olp)))
(title (nth 0 olp)))
(push (list title txt) items)))
(concat "LEVEL=" lvl "+ITEM<>\"" item "\"") 'tree)))
(concat "ITEM=\"" item "\""))
(setq items (nreverse items))
(setq items (append (list '(title topic) 'hline) items))
(insert (concat (orgtbl-to-orgtbl items nil) "\n"))))
-
-
@jagrg It's not needed, thx. Was a leftover from another go. And also forgot to turn on caching for org-get-outline-path. Modified my answer.– HubisanCommented Oct 26, 2019 at 20:48
-
@slk500 updated answer to depend on heading text only and not heading text + level. This still assumes that the title is found at level 1.– HubisanCommented Oct 28, 2019 at 9:28