Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
(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
(mapc
(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)
@ironchicken

This comment has been minimized.

Copy link

@ironchicken ironchicken commented Feb 28, 2019

This used to work for me when I was using org-mode 9.1.14. I'm now using 9.2.1 and it seems to have stopped working. This is the stack trace I get when I call it:

Debugger entered--Lisp error: (wrong-type-argument number-or-marker-p nil)
  /(nil 60.0)
  (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))
  (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)))
  (while --dolist-tail-- (setq entry (car --dolist-tail--)) (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))) (setq --dolist-tail-- (cdr --dolist-tail--)))
  (let ((--dolist-tail-- (nth 2 clock-data)) entry) (while --dolist-tail-- (setq entry (car --dolist-tail--)) (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))) (setq --dolist-tail-- (cdr --dolist-tail--))))
  (progn (setq total (+ total (nth 1 clock-data))) (insert (format "| | File *%s* | %.2f |\n" (file-name-nondirectory file) (/ (nth 1 clock-data) 60.0))) (let ((--dolist-tail-- (nth 2 clock-data)) entry) (while --dolist-tail-- (setq entry (car --dolist-tail--)) (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))) (setq --dolist-tail-- (cdr --dolist-tail--)))))
  (if (> (nth 1 clock-data) 0) (progn (setq total (+ total (nth 1 clock-data))) (insert (format "| | File *%s* | %.2f |\n" (file-name-nondirectory file) (/ (nth 1 clock-data) 60.0))) (let ((--dolist-tail-- (nth 2 clock-data)) entry) (while --dolist-tail-- (setq entry (car --dolist-tail--)) (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))) (setq --dolist-tail-- (cdr --dolist-tail--))))))
  (let ((clock-data (save-current-buffer (set-buffer (find-file-noselect file)) (org-clock-get-table-data (buffer-name) params)))) (if (> (nth 1 clock-data) 0) (progn (setq total (+ total (nth 1 clock-data))) (insert (format "| | File *%s* | %.2f |\n" (file-name-nondirectory file) (/ (nth 1 clock-data) 60.0))) (let ((--dolist-tail-- (nth 2 clock-data)) entry) (while --dolist-tail-- (setq entry (car --dolist-tail--)) (insert (format "| | . %s%s | %s %.2f |\n" (org-clocktable-indent-string ...) (nth 1 entry) (clocktable-by-tag/shift-cell ...) (/ ... 60.0))) (setq --dolist-tail-- (cdr --dolist-tail--)))))))
  (lambda (file) (let ((clock-data (save-current-buffer (set-buffer (find-file-noselect file)) (org-clock-get-table-data (buffer-name) params)))) (if (> (nth 1 clock-data) 0) (progn (setq total (+ total (nth 1 clock-data))) (insert (format "| | File *%s* | %.2f |\n" (file-name-nondirectory file) (/ (nth 1 clock-data) 60.0))) (let ((--dolist-tail-- (nth 2 clock-data)) entry) (while --dolist-tail-- (setq entry (car --dolist-tail--)) (insert (format "| | . %s%s | %s %.2f |\n" ... ... ... ...)) (setq --dolist-tail-- (cdr --dolist-tail--))))))))("/home/user/org/notes.org")
  mapcar((lambda (file) (let ((clock-data (save-current-buffer (set-buffer (find-file-noselect file)) (org-clock-get-table-data (buffer-name) params)))) (if (> (nth 1 clock-data) 0) (progn (setq total (+ total (nth 1 clock-data))) (insert (format "| | File *%s* | %.2f |\n" (file-name-nondirectory file) (/ (nth 1 clock-data) 60.0))) (let ((--dolist-tail-- (nth 2 clock-data)) entry) (while --dolist-tail-- (setq entry (car --dolist-tail--)) (insert (format "| | . %s%s | %s %.2f |\n" ... ... ... ...)) (setq --dolist-tail-- (cdr --dolist-tail--)))))))) ("/home/user/org/recur.org" "/home/user/org/sprints/BE-003.org"  ...))
  (let ((total 0)) (mapcar (function (lambda (file) (let ((clock-data (save-current-buffer ... ...))) (if (> (nth 1 clock-data) 0) (progn (setq total ...) (insert ...) (let ... ...)))))) (org-agenda-files)) (save-excursion (re-search-backward "*Tag time*") (org-table-next-field) (org-table-blank-field) (insert (format "*%.2f*" (/ total 60.0)))))
  (let ((tag (plist-get params :tags))) (insert "|--\n") (insert (format "| %s | *Tag time* |\n" tag)) (let ((total 0)) (mapcar (function (lambda (file) (let ((clock-data ...)) (if (> ... 0) (progn ... ... ...))))) (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))
  clocktable-by-tag/insert-tag((:name "clocktable-by-tag" :tags "WS" :maxlevel 2 :block "2019-02-28" :indentation-column 0 :content ""))
  (lambda (tag) (setq params (plist-put params :tags tag)) (clocktable-by-tag/insert-tag params))("WS")
  mapcar((lambda (tag) (setq params (plist-put params :tags tag)) (clocktable-by-tag/insert-tag params)) ("WS" "RD"))
  (let ((tags (plist-get params :tags))) (mapcar (function (lambda (tag) (setq params (plist-put params :tags tag)) (clocktable-by-tag/insert-tag params))) tags))
  org-dblock-write:clocktable-by-tag((:name "clocktable-by-tag" :tags "WS" :maxlevel 2 :block "2019-02-28" :indentation-column 0 :content ""))
  org-update-dblock()
  org-ctrl-c-ctrl-c(nil)
  funcall-interactively(org-ctrl-c-ctrl-c nil)
  call-interactively(org-ctrl-c-ctrl-c nil nil)
  command-execute(org-ctrl-c-ctrl-c)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment