Org Tables Reference Sheet
Some helpful functions to deal with org tables. Some I wrote, some found on the internet.
(defun group-number (num &optional size char)
"Format NUM as string grouped to SIZE with CHAR."
;; Based on code for `math-group-float' in calc-ext.el
(let* ((size (or size 3))
(char (or char ","))
(str (if (stringp num)
num
(number-to-string num)))
;; omitting any trailing non-digit chars
;; NOTE: Calc supports BASE up to 36 (26 letters and 10 digits ;)
(pt (or (string-match "[^0-9a-zA-Z]" str) (length str))))
(while (> pt size)
(setq str (concat (substring str 0 (- pt size))
char
(substring str (- pt size)))
pt (- pt size)))
str))
(defun commas-numstring(x y)
;; x column number, number
;; y range of values for the column, list
;; use in an org table like this:
;; '(commas-numstring $1 '(@1$1..@>$1));N
;; depends on the following two functions, and group-number:
(defun elx(x)
;; gets element width after formatting and grouping numbers
(setq el-formatted (format "%10.2f" x))
(setq el-trimmed (string-trim-left el-formatted))
(setq el-grouped (group-number el-trimmed))
(setq el-length (length el-grouped))
)
(defun max-elx(y)
;; gets maximum col width after formatting and grouping numbers
(setq col-formatted (mapcar (lambda (z) (format "%10.2f" z)) y))
(setq col-trimmed (mapcar (lambda (z) (string-trim-left z)) col-formatted))
(setq col-grouped (mapcar (lambda (z) (group-number z)) col-trimmed))
(setq col-length (mapcar (lambda (z) (length z)) col-grouped))
(apply #'max col-length)
)
(setq elx-diff (- (max-elx y) (elx x)))
(concat "SAR " (make-string elx-diff #x0020) el-grouped ) ;; #x0020 is a space
)
(defun clean-numstring(x)
;; inverses the output of commas-numstring
(string-to-number (replace-regexp-in-string "," "" (car (last (split-string x " ")))))
)
(defun viz-col(x y)
;; visualizes a column with single character. You can change the resolution
;; use in an org table like this:
;; '(viz-col $1 '(@1$1..@>$1));N
(setq maxval (apply #'calcFunc-vsum y))
(setq progr (ceiling (* 100 (/ (float x) maxval))))
(make-string progr ?x)
)
(defun get-percent(x y)
;; to get the percentage of each row from the total rows
;; use in an org table like this
;; '(get-percent $1 '(@1$1..@>$1));N
(setq maxval (apply #'calcFunc-vsum y))
(concat "%" (format "%3d" (* 100 (/ (float x) maxval))))
)