8

In org-mode is it possible to generate a table, where there is a column which contains a value computed from values in the other columns and which contains the sum of these values?

More specifically, at the moment I have a table with seven columns, where the last two columns contain values computed from values in the other columns using the following formula:

#+TBLFM: $6=$4-$3;t::$7=$4-$3-$2;t

I would like to include the sum of the last column. Is this possible? If yes, how can this be achieved? When I tried to do this I always got zero. Ideally I would like to have the sum somewhere outside of the table or in the last row (even if I add new rows).

2 Answers 2

8

You should use cell formulas (prefixed by := instead of =) for your results.

The example below gives the sum of the last column both at the first and the last rows.

|      |      | 8   |
| Col1 | Col2 | Sum |
|------+------+-----|
| 1    | 3    | 4   |
| 2    | 8    | 10  |
| 3    | -9   | -6  |
|------+------+-----|
|      |      | 8   |
#+TBLFM: $3=$1+$2::@1$3=vsum(@I..@II)::@6$3=vsum(@I..@II)

vsum sums a vector of numbers; @I and @II refer to the first and second hlines.

4
  • I still get zero as a result. Is it possible that the reason is the ;t part which I need since I am dealing with times. Adding this also "breaks" your example.
    – Christian
    Nov 11, 2016 at 14:52
  • Tested my example with ;T mode on all formulas and times for all cells, and it works OK. Note the uppercase T (lowercase t gives results in decimal hours instead of H:M:S).
    – Juancho
    Nov 11, 2016 at 15:46
  • Thanks, I found the problem: I started the table with |-----.... and then it does not work but without this line it does. (By the way, I really want ;t instead of ;T since I want to the total time in decimal hours).
    – Christian
    Nov 11, 2016 at 16:03
  • For general number (no times), it should be vsum(@I..II). See documentation.
    – stevosn
    Aug 28, 2019 at 14:23
2

You may use the orgtbl-aggregate package available on http://melpa.org

The sum of Col1 and Col2 is given in the aggregated table below.

#+name: cc
| Col1 | Col2 | Sum8 |
|------+------+------|
|    1 |    3 |    4 |
|    2 |    8 |   10 |
|    3 |   -9 |   -6 |
#+TBLFM: $3=$1+$2

#+BEGIN: aggregate :table "cc" :cols "vsum(Col1+Col2)"
| vsum(Col1+Col2) |
|-----------------|
|               8 |
#+END:

Documentation here: https://github.com/tbanel/orgaggregate

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.