reddit / emacs

Mapping over table rows in org-mode

susam · 2022-06-27 21:58
github-alphapapa · 2022-06-28 00:03
Nicely explained. BTW, you might mention that `-map` is the same as the built-in `mapcar`.
HumanBrainMapper · 2022-06-28 03:39
very cool! Could you show a mail merge example as well?
ffellirm · 2022-06-28 09:03
> Org-mode does have some special syntax with bunch of cool features for doing arithmetic on the cells (just like Excel formulas) ... Yes, and you can also use elisp as part of that syntax which can be helpful if you want to have the interactivity of the spreadsheet combined with some elisp "one-liners". For example: | first name | last name | yearly-income | regency commentary | |-------------+-----------+---------------+------------------------------------------------------| | Mr | Bennett | 2000 | Mr. Bennett is a man of small fortune - 2000 a year. | | Fitzwilliam | Darcy | 10000 | Mr. Darcy is a man of large fortune - 10000 a year. | | Charles | Bingley | 5000 | Mr. Bingley is a man of large fortune - 5000 a year. | #+TBLFM: $4='(concat "Mr. $2 is a man of " (if (< $3 2500) "small" "large") " fortune - $3 a year.");L Sometimes this can to be a useful way of dealing with strings it org-table spreadsheets. And you can use `calc` functions from elisp, like: | first name | last name | yearly-income | regency commentary | |-------------+-----------+---------------+--------------------------------------------------------------| | Mr | Bennett | 2000 | Mr. Bennett is a man of below average fortune - 2000 a year. | | Fitzwilliam | Darcy | 10000 | Mr. Darcy is a man of above average fortune - 10000 a year. | | Charles | Bingley | 5000 | Mr. Bingley is a man of below average fortune - 5000 a year. | #+TBLFM: $4='(concat "Mr. $2 is a man of " (if (< $3 (string-to-number (calc-eval "vmean([@I$3..@>$3])"))) "below average" "above average") " fortune - $3 a year.");L Of course, you can define elisp functions and then use them in spreadsheets. #+begin_src elisp (defun regency-commentary (last-name yearly-income) "create commentary about income" (let ((income-size (if (< (string-to-number yearly-income) 2500) "small" "large"))) (format "Mr. %s is a man of %s fortune - %s a year." last-name income-size yearly-income))) #+end_src | first name | last name | yearly-income | regency commentary | |-------------+-----------+---------------+------------------------------------------------------| | Mr | Bennett | 2000 | Mr. Bennett is a man of small fortune - 2000 a year. | | Fitzwilliam | Darcy | 10000 | Mr. Darcy is a man of large fortune - 10000 a year. | | Charles | Bingley | 5000 | Mr. Bingley is a man of large fortune - 5000 a year. | #+TBLFM: $4='(regency-commentary $2 $3) (I know the author is aware of this, but just mentioning for general interest / related stuff / alternative approaches )