1

I have a very simple table that looks like this:

| Fabricant | Catégorie | Animal                 | Nombre |
|-----------+-----------+------------------------+--------|
| Pearson   | Camp      | Bœuf                   |      1 |
| Pearson   |           | Castor                 |      1 |
| Pearson   |           | Chèvre                 |      2 |
| Pearson   |           | Cougar                 |      2 |

On which I apply this if formula as I have seen on the org mode documentation:

#+TBLFM: $5 = if($4 < 2, test, string(''))

But it throws an error:

| Fabricant | Catégorie | Animal                 | Nombre |        |
|-----------+-----------+------------------------+--------+--------|
| Pearson   | Camp      | Bœuf                   |      1 | #ERROR |
| Pearson   |           | Castor                 |      1 | #ERROR |
| Pearson   |           | Chèvre                 |      2 | #ERROR |
| Pearson   |           | Cougar                 |      2 | #ERROR |

I'm very new to Org mode, Emacs and spreadsheet formulas in general so I don't know what I'm doing wrong.

1 Answer 1

4

I don't know well the formula syntax for calc, but it seems that if you follow exactly the syntax of string constant that the documentation uses, it will work. Namely, replace '' by "":

#+TBLFM: $5 = if($4 < 2, test, string(""))

Can't explain why, though. There is also a formula syntax for Emacs Lisp that you might find more intuitive.

In your case, it translates to:

#+TBLFM: $5 = '(if (< (string-to-number $4) 2) "test" "")

which can be made shorter, as @gigiair pointed out, using N mode switch (all referenced elements will be numbers):

#+TBLFM: $5 = '(if (< $4 2) "test" ""); N

You might also find useful to toggle the formula debugger org-table-toggle-formula-debugger (C-c {) which would show you the expansion of the formula and an error message if applicable.

2
  • 1
    You can avoid the string-to-number function with the tag N : $5='(if (< $4 2) "test" "");N
    – gigiair
    Commented Jul 31, 2021 at 4:59
  • 1
    I suspect that calc takes its string definition from elisp, hence the double quotes. See the "Strings" section of the Calc manual: C-h i g (Calc) Strings.
    – NickD
    Commented Jul 31, 2021 at 14:23

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.