5

My .org-file contains tasks tagged as: a, b, c, d, e, f, g. I want to create table of time distribution by tags a, b, c, d.

    | Tag | Time Spent |  % |
    |-----+------------+----|
    | a   |      00:30 | 25 |
    | b   |      00:30 | 25 |
    | c   |      00:30 | 25 |
    | d   |      00:30 | 25 |

What is the easiest way to do it?

1 Answer 1

3

Based on this Gist: https://gist.github.com/tsu-nera/d9ffa6a51a6e7bdb957b

Add it to init.el:

(require 'org-table)
(require 'org-clock)

(defun clocktable-by-tag/shift-cell (n)
  (let ((str ""))
    (dotimes (i n)
      (setq str (concat str "| ")))
    str))

(defun clocktable-by-tag/insert-tag (params)
  (let ((tag (plist-get params :tags)))
    (insert "|--\n")
    (insert (format "| %s | *Tag time* |\n" tag))
    (let ((total 0))
  (mapcar
       (lambda (file)
     (let ((clock-data (with-current-buffer (find-file-noselect file)
                 (org-clock-get-table-data (buffer-name) params))))
       (when (> (nth 1 clock-data) 0)
         (setq total (+ total (nth 1 clock-data)))
         (insert (format "| | File *%s* | %.2f |\n"
                 (file-name-nondirectory file)
                 (/ (nth 1 clock-data) 60.0)))
         (dolist (entry (nth 2 clock-data))
           (insert (format "| | . %s%s | %s %.2f |\n"
                   (org-clocktable-indent-string (nth 0 entry))
                   (nth 1 entry)
                   (clocktable-by-tag/shift-cell (nth 0 entry))
                   (/ (nth 3 entry) 60.0)))))))
       (org-agenda-files))
      (save-excursion
    (re-search-backward "*Tag time*")
    (org-table-next-field)
    (org-table-blank-field)
    (insert (format "*%.2f*" (/ total 60.0)))))
    (org-table-align)))

(defun org-dblock-write:clocktable-by-tag (params)
  (insert "| Tag | Headline | Time (h) |\n")
  (insert "|     |          | <r>  |\n")
  (let ((tags (plist-get params :tags)))
    (mapcar (lambda (tag)
          (setq params (plist-put params :tags tag))
          (clocktable-by-tag/insert-tag params))
        tags)))

(provide 'clocktable-by-tag)

And use it in your .org-file:

#+BEGIN: clocktable-by-tag :tags ("a" "b" "c" "d") :maxlevel 2
#+END:
3
  • 1
    This used to work for me using org-mode 9.1.14 but it's stopped working with org-mode 9.2.1. I have a stack trace I could share but it's pretty big. Commented Feb 28, 2019 at 18:03
  • 1
    The parameters for org-clock-get-table-data have changed. :tags is now just a flag that means include the headline's tags when non-nil. However, you can now filter by tags using :match. Commented Jan 24, 2020 at 23:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.