复制 org table 中的日期

Copy the date in org table

假设组织中有这样一个电子表格 table

|------------+-------+------------+--------+--------+------------|
| Date       | Items | Unit Price | Amount | Amount | Categories |
|------------+-------+------------+--------+--------+------------|
| 2019/09/17 | A     |       2.64 |      1 |   2.64 | materials  |
|            | B     |      52.67 |      2 | 105.34 | diagnosis  |
|            | C     |       3.08 |      1 |   3.08 | materials  |
|            | D     |       3.85 |      2 |    7.7 | materials  |
|            | E     |      33.66 |      2 |  67.32 | materials  |
|            | F     |         40 |      1 |     40 | treatments |
|            | G     |       16.5 |      1 |   16.5 | materials  |
|            | H     |          4 |      3 |     12 | treatments |
|            | I     |         40 |      1 |     40 | bed        |
|            | M     |       6    |     13 |     78 | treatments |
|------------+-------+------------+--------+--------+------------|
#+TBLFM: =*

如何将日期 2019/09.17 复制到数据列的底部?

@manandearth 在评论中发布的 link 描述了如何复制(可能稍作修改)列中的条目。简而言之,在一个单元格中按 S-RET 会从上面的单元格复制其内容(如果它不为空)——如果该单元格已满而下一个单元格为空,则它将完整单元格复制到空单元格。如果内容是数字,那么 "duplication" 会稍作修改:它将值增加 1。日期也是如此:它将日期增加到第二天(但日期的格式必须符合Org 模式识别:活动日期 <YYYY-MM-DD> 或非活动数据 [YYYY-MM-DD])。在这些情况下,增量默认为 1,但可以通过将变量 org-table-copy-increment 设置为不同的值来设置为其他值。这就是我在评论中提到的 "interactive" 案例。

在 table 中填充列的另一种方法是使用公式。例如,这是一个用列中第一个条目的副本填充第一列的公式:

#+TBLFM: @3..@> = @2

这表示:Set all rows from row 3 (@3) to the last row (@>) of column 1 () to the value of the cell in row 2 (@2), column 1 ()。请注意,第 1 行是 header。在上面的 table 公式行上按 C-c C-c 然后...等等,发生了什么事?

|------------+-------+------------+--------+--------+------------|
|       Date | Items | Unit Price | Amount | Amount | Categories |
|------------+-------+------------+--------+--------+------------|
| 2019/09/17 | A     |       2.64 |      1 |   2.64 | materials  |
|  13.196078 | B     |      52.67 |      2 | 105.34 | diagnosis  |
|  13.196078 | C     |       3.08 |      1 |   3.08 | materials  |
|  13.196078 | D     |       3.85 |      2 |    7.7 | materials  |
|  13.196078 | E     |      33.66 |      2 |  67.32 | materials  |
|  13.196078 | F     |         40 |      1 |     40 | treatments |
|  13.196078 | G     |       16.5 |      1 |   16.5 | materials  |
|  13.196078 | H     |          4 |      3 |     12 | treatments |
|  13.196078 | I     |         40 |      1 |     40 | bed        |
|  13.196078 | M     |          6 |     13 |     78 | treatments |
|------------+-------+------------+--------+--------+------------|
#+TBLFM: @3..@> = @2

由于技术原因,在这种情况下它不太有效:Org 模式在 table 公式计算中使用 Calc,Calc 查看 2019/09/17 并说:"Aha, I have to divide 2019 by 9 and then divide the result by 17",然后填充除法结果列的其余部分:13.196078。你可能认为 2019/09/17 是一个日期,但 Org 模式并不知道这一点:它把它交给 Calc,Calc 将它解释为一个算术表达式。这里的解决方案与链接答案中的解决方案相同:通过将其设置为活动日期:<2019-09-17> 或非活动日期:[2019-09-17]:[=,使 Org 模式意识到它是 date 31=]

|------------------+-------+------------+--------+--------+------------|
| Date             | Items | Unit Price | Amount | Amount | Categories |
|------------------+-------+------------+--------+--------+------------|
| [2019-09-17]     | A     |       2.64 |      1 |   2.64 | materials  |
| [2019-09-17 Tue] | B     |      52.67 |      2 | 105.34 | diagnosis  |
| [2019-09-17 Tue] | C     |       3.08 |      1 |   3.08 | materials  |
| [2019-09-17 Tue] | D     |       3.85 |      2 |    7.7 | materials  |
| [2019-09-17 Tue] | E     |      33.66 |      2 |  67.32 | materials  |
| [2019-09-17 Tue] | F     |         40 |      1 |     40 | treatments |
| [2019-09-17 Tue] | G     |       16.5 |      1 |   16.5 | materials  |
| [2019-09-17 Tue] | H     |          4 |      3 |     12 | treatments |
| [2019-09-17 Tue] | I     |         40 |      1 |     40 | bed        |
| [2019-09-17 Tue] | M     |          6 |     13 |     78 | treatments |
|------------------+-------+------------+--------+--------+------------|
#+TBLFM: @3..@> = @2

这不会进行自动递增,但如果这是您想要的,则很容易实现:Calc 可以对日期进行计算,因此我们可以通过在每一行的日期中添加行号减去 2(例如,第 3 行将获得 3 - 2 = 1 的增量,第 4 行将获得 4 - 2 = 2,等等)。要做到这一点,你必须得到the row number of the current row:成语是@#。那么公式就变成了:

#+TBLFM: @3..@> = @2 + @# - 2

并且 table 变为:

|------------------+-------+------------+--------+--------+------------|
| Date             | Items | Unit Price | Amount | Amount | Categories |
|------------------+-------+------------+--------+--------+------------|
| [2019-09-17]     | A     |       2.64 |      1 |   2.64 | materials  |
| [2019-09-18 Wed] | B     |      52.67 |      2 | 105.34 | diagnosis  |
| [2019-09-19 Thu] | C     |       3.08 |      1 |   3.08 | materials  |
| [2019-09-20 Fri] | D     |       3.85 |      2 |    7.7 | materials  |
| [2019-09-21 Sat] | E     |      33.66 |      2 |  67.32 | materials  |
| [2019-09-22 Sun] | F     |         40 |      1 |     40 | treatments |
| [2019-09-23 Mon] | G     |       16.5 |      1 |   16.5 | materials  |
| [2019-09-24 Tue] | H     |          4 |      3 |     12 | treatments |
| [2019-09-25 Wed] | I     |         40 |      1 |     40 | bed        |
| [2019-09-26 Thu] | M     |          6 |     13 |     78 | treatments |
|------------------+-------+------------+--------+--------+------------|
#+TBLFM: @3..@> = @2+ @# - 2

日期显示的各种异常(我们包括星期几吗?我们包括时间吗?)可以使用 org-time-stamp-custom-formats 解决,但这让我们陷入了我没有的水域探索。