2

I want to copy the values of a shifted range of cells from a remote table.
Example:

#+NAME: TBL1
| 1 | 2 | 3 | 4 |
|   |   |   |   |

#+NAME: TBL2
|   | 5 | 6 | 7 | 8 |
|   |   |   |   |   |

The table TBL1 should look like this in the end:

#+NAME: TBL1
| 1 | 2 | 3 | 4 |
| 5 | 6 | 7 | 8 |

I'm currently doing this with a dedicated function for each of 15+ cells, which makes the #+TBLFM: line super long and is not maintainable. An efficient way to accomplish this is by using the lisp function identity:

#+NAME: TBL1
| 1 | 2 | 3 | 4 |
|   | 5 | 6 | 7 |
#+TBLFM: @2$1..@2$4='(identity remote(TBL2, @1$$#))

Problem is, that this function looks for values in the cells @1$1..@1$4. I want to shift the cells to look at to the right (@1$2..@1$5).
This has to be done via the REF value for the remote function, (@1$$#). If I understand the syntax correctly, $$# is pointing to the current column of the cell which is going to be filled with the remote value (e.g. for cell @2$1 in TBL1, $$# becomes $1). So this has to be shifted to the right with something like $$#+1.

What is the correct reference for my example?

2
  • Nice question!! Does replacing @1$$# with (format "@1$%d" (+ 1 $#)) work? I’m away from emacs at moment so I can’t test syntax.
    – Melioratus
    Jan 27, 2019 at 20:35
  • Thanks @Melioratus. Evaluating the replacement leaves cells of @2 of TBL1 blank. No error notification. I also checked (format "@1$%d" 2) which should format the string @1$2 and result in | 5 | 5 | 5 | 5 | for @2 of TBL1, but @2 stays also empty. Therefore using format might not work in general.
    – rrogg
    Jan 27, 2019 at 21:38

1 Answer 1

1

You can use the org-table-get-remote-range function with some string manipulation, although it's not pretty:

#+NAME: TBL1
| 1 | 2 | 3 | 4 |
| 5 | 6 | 7 | 8 |
#+TBLFM: @2='(org-table-get-remote-range "TBL2" (concat "@" "1$" (number-to-string (+ $# 1))))

Care must be taken to not write a reference literal in the lisp code to avoid immediate substitution of the table contents into the code before it is evaluated. For this reason the "@" is separate from the "1" in the concat function call.

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.