Function: org-table-sum

org-table-sum is an autoloaded, interactive and byte-compiled function defined in org-table.el.gz.

Signature

(org-table-sum &optional BEG END NLAST)

Documentation

Sum numbers in region of current table column.

The result will be displayed in the echo area, and will be available as kill to be inserted with C-y (yank).

If there is an active region, it is interpreted as a rectangle and all numbers in that rectangle will be summed. If there is no active region and point is located in a table column, sum all numbers in that column.

If at least one number looks like a time HH:MM or HH:MM:SS, all other numbers are assumed to be times as well (in decimal hours) and the numbers are added as such.

If NLAST is a number, only the NLAST fields will actually be summed.

Key Bindings

Source Code

;; Defined in /usr/src/emacs/lisp/org/org-table.el.gz
;;;###autoload
(defun org-table-sum (&optional beg end nlast)
  "Sum numbers in region of current table column.
The result will be displayed in the echo area, and will be available
as kill to be inserted with \\[yank].

If there is an active region, it is interpreted as a rectangle and all
numbers in that rectangle will be summed.  If there is no active
region and point is located in a table column, sum all numbers in that
column.

If at least one number looks like a time HH:MM or HH:MM:SS, all other
numbers are assumed to be times as well (in decimal hours) and the
numbers are added as such.

If NLAST is a number, only the NLAST fields will actually be summed."
  (interactive)
  (save-excursion
    (let (col (org-timecnt 0) diff h m s org-table-clip)
      (cond
       ((and beg end))			; beg and end given explicitly
       ((org-region-active-p)
	(setq beg (region-beginning) end (region-end)))
       (t
	(setq col (org-table-current-column))
	(goto-char (org-table-begin))
	(unless (re-search-forward "^[ \t]*|[^-]" nil t)
	  (user-error "No table data"))
	(org-table-goto-column col)
	(setq beg (point))
	(goto-char (org-table-end))
	(unless (re-search-backward "^[ \t]*|[^-]" nil t)
	  (user-error "No table data"))
	(org-table-goto-column col)
	(setq end (point))))
      (let* ((items (apply 'append (org-table-copy-region beg end)))
	     (items1 (cond ((not nlast) items)
			   ((>= nlast (length items)) items)
			   (t (setq items (reverse items))
			      (setcdr (nthcdr (1- nlast) items) nil)
			      (nreverse items))))
	     (numbers (delq nil (mapcar #'org-table--number-for-summing
					items1)))
	     (res (apply '+ numbers))
	     (sres (if (= org-timecnt 0)
		       (number-to-string res)
		     (setq diff (* 3600 res)
			   h (floor diff 3600) diff (mod diff 3600)
			   m (floor diff 60) diff (mod diff 60)
			   s diff)
		     (format "%.0f:%02.0f:%02.0f" h m s))))
	(kill-new sres)
	(when (called-interactively-p 'interactive)
	  (message (substitute-command-keys
		    (format "Sum of %d items: %-20s     \
\(\\[yank] will insert result into buffer)"
			    (length numbers)
			    sres))))
	sres))))