How can you sort an Org clock table? - emacs

Here's an example Org clock table in Emacs:
#+BEGIN: clocktable :maxlevel 1 :scope file :tcolumns 1 :formula %
#+CAPTION: Clock summary at [2014-03-30 Sun 22:47]
| Headline | Time | % |
|--------------+--------+-------|
| *Total time* | *4:31* | 100.0 |
|--------------+--------+-------|
| Item A | 1:07 | 24.7 |
| Item B | 1:06 | 24.4 |
| Item C | 2:18 | 50.9 |
#+TBLFM: $3='(org-clock-time% #2$2 $2..$2);%.1f
#+END:
I want to sort the table by the % column. Is this possible?
Here's a more complex example:
#+BEGIN: clocktable :maxlevel 2 :scope file :tcolumns 1 :formula %
#+CAPTION: Clock summary at [2014-03-30 Sun 22:48]
| Headline | Time | % |
|--------------------+--------+-------|
| *Total time* | *4:31* | 100.0 |
|--------------------+--------+-------|
| Item A | 1:07 | 24.7 |
| \__ Item A1 | 0:07 | 2.6 |
| \__ Item A2 | 1:00 | 22.1 |
| Item B | 1:06 | 24.4 |
| \__ Item B1 | 1:06 | 24.4 |
| Item C | 2:18 | 50.9 |
| \__ Item C1 | 2:18 | 50.9 |
#+TBLFM: $3='(org-clock-time% #2$2 $2..$2);%.1f
#+END:
In this instance, the top level items should be sorted by their % values, but within each subtree, the secondary level items should also be sorted by their % values.

The time clocking code for Org-mode org-clock.el doesn't support any sorting facilities. The function org-clocktable-write-default creates tables by very rigid algorithm with limited variants of tuning, all of them are in the variable org-clocktable-defaults. Thus, dynamic block clocktable generates org tables "as is", http://orgmode.org/manual/The-clock-table.html contains exhaustive list of settings.
You can sort first table manually by placing the cursor in the 3 column and executing M-x org-table-sort-lines [n]umeric.
As to sorting second table with "knowledge about structure of levels" - it seems to be impossible without profound changes in the function org-clocktable-write-default.

The built-in formatter org-clocktable-write-default sorts the entries just row by row with no idea of levels. I was looking for the same functionality and ended up with writing my own formatter (not a formatter really) that sorts the entries with respect to levels. It has a limitation though that it can only sort on name or time, since the result of sorting on time is essentially the same as sorting on percentage, this's acceptable in your case.

Related

Generating a markdown table with key bindings in Spacemacs

What is the best way to generate a markdown table with key bindings in Spacemacs (evil mode)?
Update: To clarify, this question is not about editing markdown, but automatically generating the table content for a large number of key bindings.
This could be an elisp function iterating through the possible single keystrokes (letters, numbers, punctuation, and possibly space and some control characters, with and without modifier keys), seeing what function each key is bound to (if any), and getting the description of the function.
You can do that manually using SPC h d k, but it would be handy to generate a table, given the number of possible key bindings and the way they can depend on the buffer mode and the state.
The table should show single keystrokes (letters, numbers, punctuation) with and without modifiers, the function bound to them, and the first line of the function description.
The result should look something like this:
https://github.com/cjolowicz/howto/blob/master/spacemacs.md
| Key | Mnemonic | Description | Function |
| ------ | -------- | --------------------------------------------------------------- | ------------------------ |
| a | *append* | Switch to Insert state just after point. | `evil-append` |
| b | *backward* | Move the cursor to the beginning of the COUNT-th previous word. | `evil-backward-word-begin` |
| c | *change* | Change text from BEG to END with TYPE. | `evil-change` |
| d | *delete* | Delete text from BEG to END with TYPE. | `evil-delete` |
| e | *end* | Move the cursor to the end of the COUNT-th next word. | `evil-forward-word-end` |
| f | *find* | Move to the next COUNT’th occurrence of CHAR. | `evil-find-char` |
| g | *goto* | (prefix) | |
| h | | Move cursor to the left by COUNT characters. | `evil-backward-char` |
| i | *insert* | Switch to Insert state just before point. | `evil-insert` |
| j | | Move the cursor COUNT lines down. | `evil-next-line` |
| k | | Move the cursor COUNT lines up. | `evil-previous-line` |
| l | | Move cursor to the right by COUNT characters. | `evil-forward-char` |
| m | *mark* | Set the marker denoted by CHAR to position POS. | `evil-set-marker` |
| n | *next* | Goes to the next occurrence. | `evil-ex-search-next` |
| o | *open* | Insert a new line below point and switch to Insert state. | `evil-open-below` |
| p | *paste* | Disable paste transient state if there is more than 1 cursor. | `evil-mc-paste-after` |
| q | | Record a keyboard macro into REGISTER. | `evil-record-macro` |
| r | *replace* | Replace text from BEG to END with CHAR. | `evil-replace` |
| s | *substitute* | Change a character. | `evil-substitute` |
| t | *to* | Move before the next COUNT’th occurrence of CHAR. | `evil-find-char-to` |
| u | *undo* | Undo changes. | `evil-tree-undo` |
| v | *visual* | Characterwise selection. | `evil-visual-char` |
| w | *word* | Move the cursor to the beginning of the COUNT-th next word. | `evil-forward-word-begin` |
| x | *cross* | Delete next character. | `evil-delete-char` |
| y | *yank* | Saves the characters in motion into the kill-ring. | `evil-yank` |
| z | *scroll* | (prefix) | |
(The Mnemonic column would of course be handcrafted.)
The orgtbl-mode minor mode that comes with Org (and therefore Emacs itself) should be able to help here. Activate it, then use Tab and Ret to navigate from cell to cell, letting orgtbl create and balance cells as you go. (Balancing happens when you navigate to a new cell, e.g. with Tab.)
You'll have to start the table yourself, e.g. with something like
| Key | Mnemonic | Description | Function |
|-
but from there orgtbl can take over. You can also use things like org-table-insert-column and org-table-move-row-down to make other kinds of tabular changes.
I'm not entirely sure how nicely this will play with evil-mode or what bindings it will use come out of the box, but it's worth a try.

Cross tab with a list of values instead of summation

I want a Cross tab that lists field values and counts them instead of just giving a count for the summation. I know I could make this with groups but I cant list the values vertically that way. From my research I believe I have to use a Display String Formula.
SQL Field Data
-------------------------------------------------
| Play # | Formation |Back Set | R/P | PLAY |
-------------------------------------------------
| 1 | TREY | FG | R | TRUCK |
-------------------------------------------------
| 2 | T | FG | R | RHINO |
-------------------------------------------------
| 3 | D | FG | P | 5 STEP |
-------------------------------------------------
| 4 | D | FG | P | 5 STEP |
-------------------------------------------------
| 5 | K JET | NG | R | DOG |
-------------------------------------------------
Desired report structure:
-----------------------------------------------------------
| Backet & Formation | Run | Pass |
-----------------------------------------------------------
| NG K JET | BULLA 1 | |
| | HELL 3 | |
-----------------------------------------------------------
| FG D | | 5 STEP 2 |
-----------------------------------------------------------
| NG K JET | DOG | |
-----------------------------------------------------------
| FG T | RHINO | |
-----------------------------------------------------------
Don't see why a Crosstab is necessary for this - especially if the entire body of the report is just that table.
Group your records by Bracket and Formation - If that's not
something natively configured in your table, make a new Formula field
and group on that.
Drop the 3 relevant fields into whichever section you need to display. (It might be a Footer, based on whether or not you want repeats
Write a formula to determine whether or not Run or Pass are displayed, and place it in their suppression field. (Good luck getting a Crosstab to do that for you! It tends to prefer 0s over blanks.)
If there's more to the report than just this table, you can cheat the system by placing your "table" into a subreport. And of course you can stretch Line objects across the sections and it will stretch to form the table outlines

In emacs org mode, is there a formula I can use to count the number of cells?

I'm new to emacs (spacemacs, actually) and org-mode. I'd like to have a final line in my table that shows how many rows there are in the table. Is there a formula function for that?
This formula should work: vlen(#I..#II).
Example:
| Title |
|-------|
| one |
| two |
| three |
| four |
|-------|
| 4 |
#+Tblfm: #6$1=vlen(#I..#II)
the example above is specified as "#6" which requires you to know hte sum in advance. The following seems to work:
| Title |
|-------|
| one |
| two |
| three |
| four |
|-------|
| 4 |
#+Tblfm: #II$1=vlen(#I..#II)

Error in org-table-sum org-mode?

I am just getting started with Emacs org-mode and I am already getting really confused about a simple column sum (org-table-sum). I start with
| date | sum |
|------+-------|
| | 16.2 |
| | 6.16 |
| | 6.16 |
| | |
When I hit C-c + (org-table-sum) below the second column I get the correct sum 28.52. If I add another line to make it
| date | sum |
|------+-------|
| | 16.2 |
| | 6.16 |
| | 6.16 |
| | 13.11 |
| | |
C-c + gives me 41.629999999999995. ???
If I change the last line from 13.11to 13.12, C-c +will give me (the correct) 41.64.
WTF?
Any explanation appreciated! Thanks!
Most decimal numbers cannot be represented exactly in binary floating point encoding (either single or double precision).
Test 13.11 here, to see that after conversion to double precision, the nearest number represented is 13.109999656677246.
This problem is not emacs related, but is a fundamental issue when working with floating point representation in a different base (binary rather than decimal).
Using calc's vsum, the result is OK:
| date | sum |
|------+-------|
| | 16.2 |
| | 6.16 |
| | 6.16 |
| | 13.11 |
|------+-------|
| | 41.63 |
#+TBLFM: #6$2=vsum(#I..#II)
This works because calc works with arbitrary precision and will not encode the numbers in a binary floating point format.

Numbering the rows in reverse order in an Emacs Org Mode table

I'd like to do something like this:
How to achieve a row index column in Emacs Org Mode using a Calc column rule
but I'd like the rows to be numbered in reverse order. I suspect this should be very easy, and should have something to do with #>, but e.g. $1=#>-## doesn't work.
You can try this example
| row | data |
|-----+------|
| 8 | |
| 7 | |
|-----+------|
| 6 | |
| 5 | |
| 4 | |
| 3 | 5123 |
| 2 | |
| 1 | 4234 |
#+TBLFM: $1='(- (length org-table-dlines) ##)