Guillaume MULLER <[email protected]> writes:

> I know CSV files are very badly defined. However, I think there is a bug in 
> org-table-import. Indeed, if cell content is protected by quotations (""), 
> everything inside those marks should be ignored. But when some cell entries 
> contain "\n", org-table-import goes berzerck.
>
> See the difference in resulting imports (in attached "test.org") for 
> (attached) "test1.csv" & "test2.csv"
>
> * test1.csv
> | ID | Title       |
> |  1 | My ,Title 1 |
> |  2 | My Title 2  |

Import works just fine for me on the latest main for this table.

> * test2.csv
> | "ID", | "Title", | "Text" |        |    |
> | "1",  | "Title   | 1",    | "Text, |    |
> | 1"    |          |        |        |    |
> | "2",  | "Title   | 2",    | "Text, | 2" |
>
> "ID", "Title", "Text"
> "1", "Title 1", "Text,
> 1"
> "2", "Title 2", "Text, 2"

This indeed does not work, because our csv parser refuses to parse
newlines inside values.

I think newlines can be handled if we convert them to spaces, like in
the attached tentative patch.

However, you need to force csv by C-u argument to org-table-import,
because our heuristics to determine the separator cannot catch that we
are looking at a csv table (it sees a line without commas and falls back
to generic table parser).

>From 2c5cd25c6c4de279d8c114247b764a2124e4fbfa Mon Sep 17 00:00:00 2001
Message-ID: <2c5cd25c6c4de279d8c114247b764a2124e4fbfa.1764017230.git.yanta...@posteo.net>
From: Ihor Radchenko <[email protected]>
Date: Mon, 24 Nov 2025 21:45:43 +0100
Subject: [PATCH] org-table-convert-region: Convert newlines to spaces in csv
 tables

* lisp/org-table.el (org-table-convert-region): Consider newlines
inside "..." in csv field.  When newlines do happen, replace them with
spaces as we cannot have newlines in Org table cells.

Reported-by: Guillaume MULLER <[email protected]>
Link: https://orgmode.org/list/[email protected]
---
 lisp/org-table.el | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/lisp/org-table.el b/lisp/org-table.el
index 551e18e9f..bbacf01c4 100644
--- a/lisp/org-table.el
+++ b/lisp/org-table.el
@@ -919,8 +919,8 @@ (64)    Prompt for a regular expression as field separator
 	  (cond
 	   ((looking-at "^") (insert "| "))
 	   ((looking-at "[ \t]*$") (replace-match " |") (forward-line 1))
-	   ((looking-at "[ \t]*\"\\([^\"\n]*\\)\"")
-	    (replace-match "\\1")
+	   ((looking-at "[ \t]*\"\\([^\"]*\\)\"")
+	    (replace-match (replace-regexp-in-string "\n" " " (match-string 1)) t t)
 	    (if (looking-at "\"") (insert "\"")))
 	   ((looking-at "[^,\n]+") (goto-char (match-end 0)))
 	   ((looking-at "[ \t]*,") (replace-match " | "))
-- 
2.50.1

-- 
Ihor Radchenko // yantar92,
Org mode maintainer,
Learn more about Org mode at <https://orgmode.org/>.
Support Org development at <https://liberapay.com/org-mode>,
or support my work at <https://liberapay.com/yantar92>

Reply via email to