【问题标题】:Org Table Formatting with Dollar Sign Columns带有美元符号列的组织表格式
【发布时间】:2025-12-06 11:15:01
【问题描述】:

我正在使用这种创建列的方法:
http://orgmode.org/worg/org-tutorials/org-spreadsheet-lisp-formulas.html

我有一个包含 9 列的电子表格,第 8 列包含带有美元符号的文件名,例如:
product_name $500.00.jpg

所以我希望第 9 列说明文件是否实际存在,所以我的组织 TBLFM 如下:
#+TBLFM: $9='(file-exists-p (concat "/import/" $8))

所以问题是,无论我是否在 TBLFM 末尾使用 ;L 标志,在应用公式时,我都会立即得到“无效字段说明符:“$500””,因为没有编号为 500 的列。

有什么想法可以让这个工作吗?我试过 $8 用引号而不是引号,有和没有文字标志,我试过在实际列中转义美元符号,但都没有运气。

编辑:请务必注意,将我的列值更改为:product_name $\ 500.00.jpg 确实有效,但返回的 file-exists-p 值显然不正确。

编辑:触发错误的示例组织模式表:

| foo | bar | /some/file with $500 in it.jpg |   | baz |
|     |     |                                |   |     |
#+TBLFM: $4='(or (file-exists-p $3) "f")

【问题讨论】:

  • 您能否提供一个最小表给出错误的示例?我无法重现您的错误,但也许我的示例不正确。
  • 当然,已编辑原始答案以包含一个示例,请尝试在 TBLFM 行通过C-c C-c 完成。
  • 好的,现在我可以确认了。我不记得我在使用什么,可能是 $ 后跟一个单词,而不是数字,但 org-mode 似乎足够聪明,不会将 $word 解释为字段说明符。我没有查看org-mode 代码,但可能应该向org-mode 邮件列表提出这个问题。

标签: emacs elisp org-mode


【解决方案1】:

这是Org代码的一部分,它处理公式的这种情况:

;; Insert the references to fields in same row
(while (string-match "\\$\\(\\([-+]\\)?[0-9]+\\)" form)
  (setq n (+ (string-to-number (match-string 1 form))
         (if (match-end 2) n0 0))
    x (nth (1- (if (= n 0) n0 (max n 1))) fields))
  (unless x (error "Invalid field specifier \"%s\""
           (match-string 0 form)))
  (setq form (replace-match
          (save-match-data
        (org-table-make-reference x nil numbers lispp))
          t t form)))

如您所见,您无法回避这一点,但是,您可以在该位置附近修补 org-table-eval-formula 以允许您使用某种转义序列来插入文字印记,我可以得到它远:

;; Insert the references to fields in same row
(while (string-match "\\(^\\|[^\\$]\\)\\$\\(\\([-+]\\)?[0-9]+\\)" form)
  (setq n (+ (string-to-number (match-string 2 form))
             (if (match-end 3) n0 0))
        x (nth (1- (if (= n 0) n0 (max n 1))) fields))
  (unless x (error "Invalid field specifier \"%s\""
                   (match-string 0 form)))
  (setq form (replace-match
              (save-match-data
                (org-table-make-reference x nil numbers lispp))
              t t form)))

这将跳过$$,不幸的是,此函数递归调用自身,直到所有$ 都被替换(我不知道)。你以后可以做什么:在 eLisp 代码中用单打替换双印记......这在我的情况下有效:

| foo | bar | /some/file with $$500 in it.jpg | f | /some/file with $500 in it.jpg |
|     |     |                                 | t |                                |
#+TBLFM: $4='(or (file-exists-p $3) "f")::$5='(format "%s" (replace-regexp-in-string "\\$\\$" "$" $3))

【讨论】:

    【解决方案2】:

    尝试用美元字符转义 $8,即 $$8。

    【讨论】: