0

Let's say I have the following table :

   |--------+--------|
   | Letter | Number |
   |--------+--------|
   | A      | 1      |
   | B      | 2      |
   | C      | 3      |
   |--------+--------|

I'd like to only export some part of it. Let's say:

   |--------+--------|
   | Letter | Number |
   |--------+--------|
   | A      | 1      |
   | C      | 3      |
   |--------+--------|

Is it possible?

1

One way is to use the original table as input to a source block that produces the table you want to export. Then it's just a matter of not exporting what you don't want:


* Original table                                      :noexport:
 #+name: orig
 |--------+--------|
 | Letter | Number |
 |--------+--------|
 | A      |      1 |
 | B      |      2 |
 | C      |      3 |
 |--------+--------|


* New table
#+begin_src elisp :var tbl=orig :exports results
  (progn
    (setf (nth 2 tbl) nil)
    (delete nil (append (list 'hline (car tbl) 'hline) (cdr tbl))))
#+end_src
2
  • Thanks. A bit laborious, but I guess there is no simple way to do it...
    – crocefisso
    Dec 16 '21 at 16:55
  • 1
    You can just copy the original table into the New table section and just delete the row. That's equivalent to what the source block does and might be less work.
    – NickD
    Dec 16 '21 at 18:54
1

You can modify the org-table-export function to add a filtering effect. The following excludes rows whose first column matches "B" (exact match). The added code is after ;; start of custom filter, but the rest is identical to org-table-export.

Working example:

(defun org-table-export-filtered (&optional file format)
  "org-table-export with an added filter based on values in the first column"
  (interactive)
  (unless (org-at-table-p) (user-error "No table at point"))
  (org-table-align)        ; Make sure we have everything we need.
  (let ((file (or file (org-entry-get (point) "TABLE_EXPORT_FILE" t))))
    (unless file
      (setq file (read-file-name "Export table to: "))
      (unless (or (not (file-exists-p file))
                      (y-or-n-p (format "Overwrite file %s? " file)))
          (user-error "File not written")))
    (when (file-directory-p file)
      (user-error "This is a directory path, not a file"))
    (when (and (buffer-file-name (buffer-base-buffer))
                 (file-equal-p
                    (file-truename file)
                    (file-truename (buffer-file-name (buffer-base-buffer)))))
      (user-error "Please specify a file name that is different from current"))
    (let ((fileext (concat (file-name-extension file) "$"))
            (format (or format (org-entry-get (point) "TABLE_EXPORT_FORMAT" t))))
      (unless format
          (let* ((formats '("orgtbl-to-tsv" "orgtbl-to-csv" "orgtbl-to-latex"
                                "orgtbl-to-html" "orgtbl-to-generic"
                                "orgtbl-to-texinfo" "orgtbl-to-orgtbl"
                                "orgtbl-to-unicode"))
                 (deffmt-readable
                     (replace-regexp-in-string
                      "\t" "\\t"
                      (replace-regexp-in-string
                       "\n" "\\n"
                       (or (car (delq nil
                                          (mapcar
                                           (lambda (f)
                                             (and (string-match-p fileext f) f))
                                           formats)))
                           org-table-export-default-format)
                       t t)
                      t t)))
            (setq format
                    (org-completing-read
                     "Format: " formats nil nil deffmt-readable))))
      (if (string-match "\\([^ \t\r\n]+\\)\\( +.*\\)?" format)
            (let ((transform (intern (match-string 1 format)))
                    (params (and (match-end 2)
                                   (read (concat "(" (match-string 2 format) ")"))))
                    (table (org-table-to-lisp)))
              (unless (fboundp transform)
                (user-error "No such transformation function %s" transform))

            ;; start of custom filter
            (let* ((org-table-temp (org-table-to-lisp))
                   (new-org-table-temp '()))
              (while org-table-temp
                (setq row (car-safe org-table-temp))
                (setq row-text (format "%s" (car-safe row)))
                (if (bound-and-true-p row-text)
                    ;; change "B" below to filter on different value
                    (if (string= row-text "B") nil (push row new-org-table-temp))) ;; appends if not matching
                (setq org-table-temp (cdr org-table-temp)))
              (setq new-org-table-temp (nreverse new-org-table-temp))
              (setq table new-org-table-temp)
              )
            ;; end of custom filter

              (let (buf)
                (with-current-buffer (find-file-noselect file)
                    (setq buf (current-buffer))
                    (erase-buffer)
                    (fundamental-mode)
                    (insert (funcall transform table params) "\n")
                    (save-buffer))
                (kill-buffer buf))
              (message "Export done."))
          (user-error "TABLE_EXPORT_FORMAT invalid")))))

This works by taking original table produced when executing org-table-export, checking value in first column of each row for "B", and if it does not match appends row to a new filtered table.

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.