Forum > FPSpreadsheet

[SOLVED] Do not use QuoteChar when save csv

(1/2) > >>

totya:
Hi Master! :)

If I load and save same csv file, the result is different, see:

input:


--- Quote ---a   b   c
öüó   őúé    áűí
123    456   789

--- End quote ---

output: (save)

--- Quote ---a   b   c
öüó   "őúé "   áűí
"123 "   456   789

--- End quote ---

But I must do not need QuoteChar when I save csv file, can I do it anyhow? Thank you!

Code:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TForm1.MenuItem1Click(Sender: TObject);begin  CSVParams.Encoding:='UTF-8';  CSVParams.Delimiter := #9; //CSVParams.QuoteChar :=  ???   sWorksheetGrid1.LoadFromSpreadsheetFile('Src\test.csv', sfCSV);  sWorksheetGrid1.SaveToSpreadsheetFile('Src\test_OUT.csv', sfCSV);end;       

wp:
No "master", please...

It seems that quotes are added when a cell text ends (or begins) with a space. Looking into the csvdocument sources (which are used by fpspreadsheet) I see that there is a boolean option "QuoteOuterWhiteSpace" which is active by default. When you open file fpscsv.pas, find procedure TsCSVWriter.WriteSheet, add the line "FCSVBuilder.QuoteOuterWhiteSpace := CSVParams.QuoteOuterWhiteSpace;" after the "try", and add a "CSVParams.QuoteOuterWhiteSpace :=false;" to your project code the quotes will be gone.

I committed an updated version of fpspreadsheet in which the TCSVParams record is extended by the following fields:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---  TsCSVParams = record   // W = writing, R = reading, RW = reading/writing    ...    QuoteOuterWhiteSpace: Boolean;   // W: Quote when cell content begins/ends with white space.    IgnoreOuterWhiteSpace: Boolean;  // R: Ignores white space before/after cell content.    ....  end;
As you can see you can also set CSVParams.IgnoreOuterWhiteSpace to true to get rid of the leading/trailing spaces already at reading - then these cells will not be quoted either.

But note that the white-space options do not work for numerical cells since the string-to-float conversion occurs via the standard function TryStrToFloat which does a "trim" (white-space removal) before the conversion.

Thaddy:
What is so difficult in understanding escaped chars?
https://en.wikipedia.org/wiki/Escape_character

To be honest @wp gave you a much more elaborate answer than the question deserved.
He has more patience...

totya:

--- Quote from: wp on February 07, 2023, 05:57:30 pm ---No "master", please...
--- End quote ---

Hi!

I call you "master" long time ago (years), because this is the biggest component what I seen ever on the world, and this component helped me a lot. But okay!

Your completely understand my problem (not like others) thank you!

I uninstall fps comoonent via online package manager. First uninstall nothing happen, but the second uninstall is success. Next I install svn version, success.

Unfortunately,  CSVParams.QuoteOuterWhiteSpace := False; doesn't work.

Code:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TForm1.LoadCSVMenuItemClick(Sender: TObject);begin  CSVParams.Encoding := 'UTF8';  CSVParams.Delimiter := #9;  CSVParams.QuoteOuterWhiteSpace := False;   Memo.Lines.Add('Loading...');  Application.ProcessMessages;   sWorksheetGrid1.LoadFromSpreadsheetFile('Src\test.csv', sfCSV);  sWorksheetGrid1.SaveToSpreadsheetFile('Src\test_OUT.csv', sfCSV);end;         
input:

--- Quote ---a   b   c
öüó   őúé    áűí
123    456   789
--- End quote ---

output:

--- Quote ---a   b   c
öüó   "őúé "   áűí
"123 "   456   789
--- End quote ---

wp:
Oh, there's a typo in the writer code - fixed. Please get yourself the latest commit.

Navigation

[0] Message Index

[#] Next page

Go to full version