Skip to content
This repository has been archived by the owner on Apr 14, 2023. It is now read-only.

Export CSV function does not escape carriage returns #13

Open
corbers opened this issue Sep 13, 2017 · 1 comment
Open

Export CSV function does not escape carriage returns #13

corbers opened this issue Sep 13, 2017 · 1 comment
Assignees
Labels

Comments

@corbers
Copy link

corbers commented Sep 13, 2017

When a report is generated by GRASSMARLIN, e.g. Logical Nodes Report, some of the data can contain carriage returns. A common example of this is the operating system field which often contains multiple entries separated by a carriage return. When the data is exported to a CSV file the carriage returns are included which causes formatting issues when viewing in a spreadsheet package such as Excel. To fix the file the CSV file has to be manually edited to either remove the carriage returns or to escape the data by surrounding the data with quotes.

Suggest that the static method fieldFromString in Csv.java is updated to escape a field that contains carriage returns.

Thanks for a great application.

@iadgovuser5
Copy link
Contributor

iadgovuser5 commented Sep 14, 2017

Thank you very much for pointing this out. It turns out we had a little bit of a CM failure; the code that was posted as 3.2.1 does not match the master copy that was supposed to be posted. There were some issues with migrating development environments between 3.2.0 and 3.2.1 and, apparently, we missed at least one. We're starting a full audit of the differences, but that will take time. While we work on that, the correct version of the fieldFromString method is as follows:

public static String fieldFromString(String text) {
     if(text.startsWith("\") || text.contains(",") || text.contains("\r") || text.contains("\n")) {
          return "\"" + text.replace("\"", "\"\"").replaceAll("(?<!\\r)\\n", "\r\n") + "\"";
    } else {
          return text;
     }
}

Strictly speaking, replacing LF with CRLF isn't necessary, but CSV (IETF RFC4180) calls for CRLF as the line ending and so any tool working with CSV should handle CRLF in the strings. The exportTableToFile method, similarly, should be inserting CRLF sequences instead of calling writer.newLine().

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

5 participants