1

I have an org table:

| name      | band     | group # |
|-----------+----------+---------|
| lennon    | beatles  |         |
| mccartney | beatles  |         |
| harrison  | beatles  |         |
| starr     | beatles  |         |
|-----------+----------+---------|
| page      | zeppelin |         |
| plant     | zeppelin |         |
| jones     | zeppelin |         |
| bonham    | zeppelin |         |
|-----------+----------+---------|
| mason     | floyd    |         |
| waters    | floyd    |         |
| wright    | floyd    |         |
| barrett   | floyd    |         |
| gilmour   | floyd    |         |

I'd like to fill the last column with the "row group number":

| name      | band     | group # |
|-----------+----------+---------|
| lennon    | beatles  |       1 |
| mccartney | beatles  |       1 |
| harrison  | beatles  |       1 |
| starr     | beatles  |       1 |
|-----------+----------+---------|
| page      | zeppelin |       2 |
| plant     | zeppelin |       2 |
| jones     | zeppelin |       2 |
| bonham    | zeppelin |       2 |
|-----------+----------+---------|
| mason     | floyd    |       3 |
| waters    | floyd    |       3 |
| wright    | floyd    |       3 |
| barrett   | floyd    |       3 |
| gilmour   | floyd    |       3 |

I'd like to do this in a way that would allow me to add more groups to the table in the future. Is this possible in org-mode?

  • Do you pick or assign the group a number or is the group number assigned by group order in the table? Will the beatles always be group number 1 even if it was the bottom of the table? – Melioratus Sep 11 '17 at 1:59
  • 1
    The beatles are group #1 because they are the first group on the list. The group number is assigned by group order in the table. – Brian Fitzpatrick Sep 11 '17 at 2:04
  • Thanks for quick response! I had a simple solution if you manually assigned number to group,#+TBLFM: $3=@-I$3, but it does not work if you wanted group number to be equal to group order. I'll keep looking! – Melioratus Sep 11 '17 at 2:09
  • @Melioratus Thanks! For now I'm inputting these by hand in a much larger org document. Hopefully I can automate this in the future! – Brian Fitzpatrick Sep 11 '17 at 2:29
  • Did my answer help? The example code will automatically assign the group number in column 3 according the order the band in column 2 of your table. If you move a band up or down in the table, the group number for that band will updated when the #+TBLFM: is executed again using C-u C-c * or C-u C-u C-c *. Thanks for asking your question! – Melioratus Jun 24 '18 at 17:40
1

Try This

  1. Add a #+NAME: to your org table, e.g. bands-table

    #+NAME: bands-table
    | name      | band     | group # |
    |-----------+----------+---------|
    | lennon    | beatles  |         |
    | mccartney | beatles  |         |
    | harrison  | beatles  |         |
    | starr     | beatles  |         |
    |-----------+----------+---------|
    | page      | zeppelin |         |
    | plant     | zeppelin |         |
    | jones     | zeppelin |         |
    | bonham    | zeppelin |         |
    |-----------+----------+---------|
    | mason     | floyd    |         |
    | waters    | floyd    |         |
    | wright    | floyd    |         |
    | barrett   | floyd    |         |
    | gilmour   | floyd    |         |
    
  2. Add the code block below into your file.

      #+NAME: my/group-number 
      #+BEGIN_SRC elisp :var data=bands-table[,1,] :var band_name=""
        (setq bands (delete-dups data))
        (seq-position bands band_name)
      #+END_SRC
    
  3. Add #+TBLFM: to bottom of your org table.

    #+NAME: bands-table
    | name      | band     | group # |
    |-----------+----------+---------|
    | lennon    | beatles  |         |
    | mccartney | beatles  |         |
    | harrison  | beatles  |         |
    | starr     | beatles  |         |
    |-----------+----------+---------|
    | page      | zeppelin |         |
    | plant     | zeppelin |         |
    | jones     | zeppelin |         |
    | bonham    | zeppelin |         |
    |-----------+----------+---------|
    | mason     | floyd    |         |
    | waters    | floyd    |         |
    | wright    | floyd    |         |
    | barrett   | floyd    |         |
    | gilmour   | floyd    |         |
    #+TBLFM: $3='(org-sbe my/group-number (band_name $$2)) 
    
  4. Place cursor on #+TBLFM: and press C-c C-c to populate group number column. If prompted answer yes.

    #+NAME: bands-table
    | name      | band     | group # |
    |-----------+----------+---------|
    | lennon    | beatles  |       1 |
    | mccartney | beatles  |       1 |
    | harrison  | beatles  |       1 |
    | starr     | beatles  |       1 |
    |-----------+----------+---------|
    | page      | zeppelin |       2 |
    | plant     | zeppelin |       2 |
    | jones     | zeppelin |       2 |
    | bonham    | zeppelin |       2 |
    |-----------+----------+---------|
    | mason     | floyd    |       3 |
    | waters    | floyd    |       3 |
    | wright    | floyd    |       3 |
    | barrett   | floyd    |       3 |
    | gilmour   | floyd    |       3 |
    #+TBLFM: $3='(org-sbe my/group-number (band_name $$2))  
    

Thanks for asking a fun question!
Hope that helped!


This code was tested using:
GNU Emacs 25.3.1
Org mode version 9.1.2

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.