0

Cell fields do automatic calculation by default. For example

|-------+----------|
| Test  | Value    |
|-------+----------|
| Stone | 5.4+12.3 |
|-------+----------|
#+TBLFM: @>$>=@>$>

The cell formula refers to itself, the cell content is the calculation '5.4+12.3'. Placing cursor on #+TBLFM line and pressing C-c C-c for re-applying the formulas to the entire table transforms cell content into the result '17.7'.

|-------+-------|
| Test  | Value |
|-------+-------|
| Stone |  17.7 |
|-------+-------|
[pointer]#+TBLFM: @>$>=@>$>

How awesome is that?! The question now is: Can a #+TBLFM line with elisp code do the same?

Here are my attempts to reproduce it - in vain, all of which placing cursor on #+TBLFM line and pressing C-c C-c for re-applying the formulas to the entire table does NOT transform cell content into the result '17.7'.

#1 - with a bit transformation ahead ( ',' > '.'):

|-------+----------|
| Test  | Value    |
|-------+----------|
| Stone | 5,4+12,3 |
|-------+----------|
#+TBLFM: @>$>='(string-to-number (replace-regexp-in-string "," "." @>$>))

New cell content: 5.4

#2 - most basic approach:

|-------+----------|
| Test  | Value    |
|-------+----------|
| Stone | 5.4+12.3 |
|-------+----------|
#+TBLFM: @>$>='(string-to-number @>$>)

New cell content: 5.4

#3 - the sparking idea, that quick-calc must be behind it somehow:

3.1

|-------+----------|
| Test  | Value    |
|-------+----------|
| Stone | 5.4+12.3 |
|-------+----------|
#+TBLFM: @>$>='(quick-calc @>$>)

New cell content: #ERROR

3.2

|-------+----------|
| Test  | Value    |
|-------+----------|
| Stone | 5.4+12.3 |
|-------+----------|
#+TBLFM: @>$>='(calc-do-quick-calc @>$>)

New cell content: #ERROR

What does the trick? Does anyone know?

1 Answer 1

2

Try:

|-------+-------|
| Test  | Value |
|-------+-------|
| Stone |  17.7 |
|-------+-------|
#+TBLFM: @>$>='(calc-eval @>$>)

The point is that @>$> is (or is converted to) a string, so you need a function that takes a string and evaluates it, producing another string that can be assigned to the contents of the cell on the LHS.

calc-eval fits the bill: doing C-h f calc-eval says:

(calc-eval STR &optional SEPARATOR &rest ARGS)

Do a quick calculation and return the result as a string. Return value will either be the formatted result in string form, or a list containing a character position and an error message in string form.

The quick-calc idea was close, but quick-calc does not take a string as input (it reads it from the minibuffer) and it does not produce a string as its value (it prints it to the echo area). Note that calc-eval is part of the Calc package which is what the Org mode table code uses to evaluate formulas ("normal" formulas, not elisp ones). Also note that there is no function named calc-do-quick-calc (try doing C-h f calc-do-quick-calc, so there was no hope that that would work.

2
  • SWEET! Works like a charm. Thank you very much, NickD, for giving both, the correct answer to the core question, and the fundamental background related to the core question. Couldn't have been answered better.
    – starquake
    Jul 3, 2023 at 17:53
  • You are welcome!
    – NickD
    Jul 3, 2023 at 23:32

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.