Recent

Author Topic: TStringGrid.SaveToCSVFile does not save all Fixedrows  (Read 12812 times)

jesusr

  • Sr. Member
  • ****
  • Posts: 484
Re: TStringGrid.SaveToCSVFile does not save all Fixedrows
« Reply #15 on: April 25, 2016, 05:25:11 pm »
Bart: Good idea, I would like to have implemented the CSV feature that way from start, now with most of the code for loading the CSV data outside of the grids unit, this is not too important yet I wouldn't say it's locked. I wouldn't mind if the LoadFrom/SaveToCSV feature is left in the grid with just a call to another LCL or LazUtils unit. I like also the idea of CSVoptions because in order to have finer control more arguments are needed.

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: TStringGrid.SaveToCSVFile does not save all Fixedrows
« Reply #16 on: April 25, 2016, 06:02:55 pm »
Let's wait for wp to fix this issue (he volunteered).
If that is done I can start refactoring.

The class definition of the import and export baseclass must be in grids.pas.
The ExportToStream, ExportToFile etc functions ATM make sense for TStringGrid, but should they be based in TCustomGrid (maybe as virtual abstract method)?

The import/exportclasses must themselves handle the situation where the grid parameter is of an unsupported type (e.g. not a TStringGrid but a TDrawGrid).

The Import/Export options must not necessarily be a property of a Grid.
It already has enough properties.

If we want to make this as much a plugin feature as possible, then I think we should have two overloads of each ExportToXXX methods:
Code: [Select]
TCsutomGrid.ExportToStream(AExporterClass: TGridExporterClass; AStream: TStream; AOptions: TGridExportOptions);
TCsutomGrid.ExportToStream(AExporter: TCustomGridExporter; AStream: TStream; AOptions: TGridExportOptions);

The first one ony requires the class AExporterClass exists, and requires no preparation of the user.
The second one gives users the possibility to set properties of the exporter (e.g. a HTML exporter could set a several optional HTML and CSS properties on the created table).

Bart

wp

  • Hero Member
  • *****
  • Posts: 11858
Re: TStringGrid.SaveToCSVFile does not save all Fixedrows
« Reply #17 on: April 26, 2016, 11:26:05 pm »
Here's an initial version of a rather general exporter for all grid-like components and various data formats.

The exporter comes in two pieces:
  • the Exporter itself which resembles something like the file format. I implemented CSV, HTML and spreadsheet formats (using fpspreadsheet). In addition to Bart's suggestions above it is very general, it does not even know the grid
  • the ExporterLink which links the exporter to the object to be exported. In this way, any grid-like object can be exported, such as TCustomDrawGrid descendants or TKGrid or even TListView. For each one of them a descendant of TLazExporterLink must be provided which implements its abstract methods to iterate through the "grid" and to pass the cell values to the exporter.
Unit LazExporter contains the abstract classes, TLazExporter and TLazExporterLink, as well as the TCSVExporter (which creates a csv file/stream) and the THTMLExporter (which creates a HTML file/stream). They offer their own set of properties to control the output. THTMLExporter, for example, has a stringlist "CSS" for formatting. TCSVExporter takes advantage of fpc's unit csvreadwrite, a part of the old CSVDocument (in memoriam of Rainier).

Unit GridExporter implements the ExporterLink for TCustomDrawGrid descendents which is controlled by a set of parameters for enabling export of FixedCols, FixedRows, or VisibleColumnsOnly. Since TCustomDrawGrid does not expose string content I added a virtual method TCustomDrawGrid.GetCellText which queries the cell text by means of an event OnGetCellText; TStringGrid, of course, returns the Cells[col,row] here. I did this because "virtual grids" might be constructed from TCustomDrawGrid in such an event-driven way. For testing, you must replace your grids.pas unit by the one in the attachment (there is also a patch to see the changes).

Unit SpreadsheetExporter implements the TSpreadsheetExporter which writes to the spreadsheet formats provided by fpspreadsheet.

The attachment contains also a little demo to see things at work.

Please note that this work is not yet completed, I am posting it in this early stage to see if I am on the right way. Some control properties of the TCSVExporter provided by csvreadwrite are still missing. And I would also like to implement a basic TListviewExporterLink to demonstrate the interface to a non-Grid component.

Once finished, the GridExporter could be used to remove all hardcoded csv-writing from grids.pas (SaveToCSV, CopyToClipboard).
« Last Edit: April 26, 2016, 11:28:21 pm by wp »

wp

  • Hero Member
  • *****
  • Posts: 11858
Re: TStringGrid.SaveToCSVFile does not save all Fixedrows
« Reply #18 on: April 27, 2016, 02:17:59 pm »
First problem: csvreadwrite requires at least fpc3.0, fpc 2.6.4 does not have it yet  :(

 

TinyPortal © 2005-2018