GRASSMARLIN icon indicating copy to clipboard operation
GRASSMARLIN copied to clipboard

Export CSV function does not escape carriage returns

Open corbers opened this issue 8 years ago • 1 comments

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.

corbers avatar Sep 13 '17 10:09 corbers

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().

iadgovuser5 avatar Sep 14 '17 15:09 iadgovuser5