1

Here is a table where scores are calculated at last row with a formula. How to generate a new table TotalResults with 1 and last columns? Can we even sort the last column in the process?

#+TBLNAME: ScoreCard
| team | 1st round | 2nd round | total |
|------+-----------+-----------+-------|
| A    |         7 |         9 |    16 |
| B    |         3 |         5 |    8 |
#+TBLFM: $4=vsum($2..$3)

Like this one

#+TBLNAME: ScoreCard
| team | total |
|------+-------|
| B    |     8 |
| A    |    16 |
1

Hope this function will do the trick

(defun generate-a-new-table-out-of-old-one-with-sorting (ref)
  " Inserts an Org table at point copied from an existing table REF.
   only first and last colums are copied and the table is sorted by following the last column    "
  (interactive (list (read-from-minibuffer "Table name to proceed :" "ScoreCard")))
  (let ((fline (concat  "|"
                        (org-table-get-remote-range ref "@<$<") "|"
                        (org-table-get-remote-range ref "@<$>")  "|\n"))
        (fcol (org-table-get-remote-range ref "@<<$<..@>$<") )
        (lcol (org-table-get-remote-range ref "@<<$>..@>$>"))
        )
    (insert "\n#+TBLNAME: TotalResults\n")
    (insert fline)  
    (seq-mapn (lambda(x y) (insert "|" x "|" y "|\n"))fcol lcol)
    (org-table-goto-line 1)
    (org-table-insert-hline)
    (org-table-analyze)
    (org-table-goto-field "@2$2")
    (org-table-sort-lines nil ?n)
    (orgtbl-ctrl-c-ctrl-c nil)))

You can sort the results in descending order by replacing ?n by ?N in the sort command. For my own that is what i would done.

PS:

To activate this function, first evaluate it (The simplest is C-x C-e after the last parenthesis, but you can evaluate it in a source block like you did it).

Next evaluate it like any elisp function (the resulting table will be inserted at point)

M-x  generate-a-new-table-out-of-old-one-with-sorting

Obviously you can change the name of the function for a shorter.

If you find it handy and use it frequently, you can save my code in your init file and (eventually) bind it to some keystrokes.

ps 2:

If you prefer the code to be evaluated in an org-mode block I had adapted it :

#+begin_src emacs-lisp  :var ref="ScoreCard" :results value raw
  (let ((fline (concat  "|"
                        (org-table-get-remote-range ref "@<$<") "|"
                        (org-table-get-remote-range ref "@<$>")  "|\n"))
        (fcol (org-table-get-remote-range ref "@<<$<..@>$<") )
        (lcol (org-table-get-remote-range ref "@<<$>..@>$>"))
        )
    (with-temp-buffer
      (org-mode)
      (insert "\n#+TBLNAME: TotalResults\n")
      (insert fline)  
      (seq-mapn (lambda(x y) (insert "|" x "|" y "|\n"))fcol lcol)
      (org-table-goto-line 1)
      (org-table-insert-hline)
      (org-table-analyze)
      (org-table-goto-field "@2$2")
      (org-table-sort-lines nil ?n)
      (orgtbl-ctrl-c-ctrl-c nil)
      (buffer-string))
    )

#+end_src
3
  • I have placed above code in #+begin_src lisp tag, but evaluating it doesnt produce the table. Please help this newbie here. Mar 24 '21 at 6:46
  • 1
    I added some explain for how to use it. If not enough please ask for more. I can change the code for it to be used in a source block, but that way it can be used only once time. I don't find it so handy.
    – gigiair
    Mar 24 '21 at 7:30
  • 1
    I added an elisp block code to do the job the way you first did it.
    – gigiair
    Mar 24 '21 at 9:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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