User talk:Garydale

From Free Pascal wiki
Revision as of 05:23, 16 February 2020 by Trev (talk | contribs) (Fixed syntax highlighting)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

FPSpreadsheet code to open a file

Hello, there is already a method to read a file in fpspreadsheet regardless of the format: ReadFile

If you don't pass any file format it will attempt to auto-detect the format. At the moment it doesn't differentiate between various XLS formats, just supposes biff8, but it would be better to improve it to do this then to add new code to do the same thing is each app using fpspreadsheet. --Felipe Monteiro de Carvalho 11:13, 2 August 2011 (CEST)


Hello Felipe: Please feel free to use my code to improve the ReadFromFile method. I looked at the code and my routine should be almost a drop-in replacement. Here's my take at it:

procedure TsWorkbook.ReadFromFile(AFileName: string);
var SheetType: TsSpreadsheetFormat;
    valid: Boolean;

  procedure getFileType(const FileToUpdate: TFileName; var SheetType: TsSpreadsheetFormat; var valid: Boolean);
    var suffix: String;
    begin
        valid := True;
        suffix := ExtractFileExt(FileToUpdate);
        if suffix = STR_EXCEL_EXTENSION then
           SheetType := sfExcel8
        else if suffix = STR_OPENDOCUMENT_CALC_EXTENSION  then
           SheetType := sfOpenDocument
        else if suffix = STR_OOXML_EXCEL_EXTENSION then
           SheetType := sfOOXML
        else if suffix = '.csv' then
           SheetType := sfCSV
        else
           valid := False;
    end;

begin
    getFileType(AFileName, SheetType, valid);
    if valid then begin
       try
          ReadFromFile(AFileName, SheetType);
       except
           if SheetType = sfExcel8 then begin
             repeat
                try
                   SheetType := Pred(SheetType);
                   ReadFromFile(AFileName, SheetType);
                   valid := True;
                except
		   valid := False
                end;
              until valid or (SheetType = sfExcel2);
          end;
       end;
    end;
end;

NOTE: I prefer Wirth's later syntax enhancements to his original Algol/Pascal work. In particular, I like his switch to statement lists within closed constructs rather than begin/end blocks. As such, I tend to use begin/end even when they aren't needed, simply to allow statements to be properly closed.

Could you update the wiki for my commit of your patch? thanks --Felipe Monteiro de Carvalho 17:46, 11 August 2011 (CEST)


Waiting for you to post a working patch, as per my comments in the bug-tracker. Also, would like to see what you made of my thoughts on the getFileTypeFromName routine. This would have an impact on whatever changes I make to the wiki.

Looking forward to it.

Which comment? To which bug? There is no comment here: http://bugs.freepascal.org/view.php?id=14466 --Felipe Monteiro de Carvalho 13:16, 23 August 2011 (CEST)