* * *

Author Topic: CSVDocument: proposed update  (Read 1007 times)

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
CSVDocument: proposed update
« on: October 24, 2014, 02:31:55 pm »
(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
Code: [Select]
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:
Code: [Select]
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:
Code: [Select]
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!
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

mijen67

  • Newbie
  • Posts: 1
  • It's hard to beat the bandwidth of a flying DVD
Re: CSVDocument: proposed update
« Reply #1 on: August 05, 2017, 01:14:27 pm »
Great!

And IMHO the testing code is so much easier to read as a first intro to csvdocument than the example on http://wiki.lazarus.freepascal.org/CsvDocument

May I suggest that someone adds the above example to the documentation?

Kind regards
Michael

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus