(I'll also get in touch with the csvdocument author)
Please find below a proposed new version of csvdocument.
Changes since latest svn version:
- commented procedures/functions/properties
- fixed TrimEmptyCells[1]
Symptom: when there is (an) empty cell(s) between non-empty cell(s) in a row, it/they are also deleted, changing the order of the cells.
run test program
program trimtest;
{$mode objfpc}{$H+}
uses
{$IFDEF UNIX}{$IFDEF UseCThreads}
cthreads,
{$ENDIF}{$ENDIF}
Sysutils, Classes, CSVDocument;
var
Doc:TCSVDocument;
procedure DumpDocument;
var
col,row:integer;
begin
writeln('Dump document - cells enclosed by *:');
for row:=0 to Doc.RowCount-1 do
begin
writeln('Row: '+inttostr(Row));
col:=0;
while Col>=0 do
begin
if Doc.HasCell(col,row) then
begin
writeln('*'+Doc.Cells[col,row]+'*');
col:=col+1;
end
else
begin
Col:=-1; //next row
end;
end;
writeln(''); //end of row
end;
writeln('');
end;
begin
Doc:=TCSVDocument.Create;
try
Doc.Delimiter:=';';
Doc.LineEnding:=LineEnding;
Doc.EqualColCountPerRow:=true;
Doc.QuoteChar:='"';
{
Doc.AddCell(0,'Jim');
Doc.AddCell(0,'Beam');
Doc.AddCell(0,'47');
Doc.AddCell(0,'male');
Doc.AddRow('EmptyEnd');
Doc.AddCell(1,'');
Doc.AddCell(1,'20');
Doc.AddCell(1,'');
Doc.AddRow('EmptyMiddle');
Doc.AddCell(2,'');
Doc.AddCell(2,'');
Doc.AddCell(2,'male');
Doc.AddRow('Quoted');
Doc.AddCell(3,'"The Butcher" Foley');
Doc.AddCell(3,'38');
Doc.AddCell(3,'male');
}
Doc.CSVText:=
'Jim;Beam;47;male'+LineEnding+
'Null;;20;unknown'+LineEnding+
'Arnold;;"male"'+LineEnding+
'Soy;Hon;;'+LineEnding+
'Bob;"""The Hammer"" Williams";;male';
DumpDocument;
writeln('csvtext: ');
//writing out csv text with quoted stuff works.
writeln(Doc.CSVText);
writeln('');
// Test bug fix for RemoveTrailingEmptyCells
writeln('RemoveTrailingEmptyCells:');
Doc.RemoveTrailingEmptyCells;
writeln('csvtext: ');
writeln(Doc.CSVText);
writeln('');
finally
Doc.Free;
end;
end.
Before fix - see record starting with Null for symptom:
csvtext:
Jim;Beam;47;male
Null;;20;unknown
Arnold;;male;
Soy;Hon;;
Bob;"""The Hammer"" Williams";;male
RemoveTrailingEmptyCells:
csvtext:
Jim;Beam;47;male
Null;20;unknown;
Arnold;male;;
Soy;Hon;;
Bob;"""The Hammer"" Williams";male;
After fix:
csvtext:
Jim;Beam;47;male
Null;;20;unknown
Arnold;;male;
Soy;Hon;;
Bob;"""The Hammer"" Williams";;male
RemoveTrailingEmptyCells:
csvtext:
Jim;Beam;47;male
Null;;20;unknown
Arnold;;male;
Soy;Hon;;
Bob;"""The Hammer"" Williams";;male
As usual, comments/suggestions are more than welcome!