From 8c56dd2d66a0c8cee47477b2c1f38c0bf5bec6c8 Mon Sep 17 00:00:00 2001 From: wp_xxyyzz Date: Sun, 8 Jan 2017 10:53:23 +0000 Subject: [PATCH] fpspreadsheet: Fix loading error messages not being shown by TsWorkbookSource. git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@5606 8e941d3f-bd1b-0410-a28a-d453659cc2b4 --- .../source/common/fpspreadsheet.pas | 3 +- .../fpspreadsheet/source/common/xlsbiff2.pas | 10 ++- .../fpspreadsheet/source/common/xlsbiff8.pas | 3 +- .../source/visual/fpspreadsheetctrls.pas | 85 ++----------------- 4 files changed, 17 insertions(+), 84 deletions(-) diff --git a/components/fpspreadsheet/source/common/fpspreadsheet.pas b/components/fpspreadsheet/source/common/fpspreadsheet.pas index c109db970..cb85c1795 100644 --- a/components/fpspreadsheet/source/common/fpspreadsheet.pas +++ b/components/fpspreadsheet/source/common/fpspreadsheet.pas @@ -8058,13 +8058,14 @@ begin raise Exception.CreateFmt(rsReaderNotFound, [AFileName]); // Here is the trial-and-error loop checking for the various formats. + success := false; for i:=0 to High(fileformats) do begin try ReadFromFile(AFileName, fileformats[i], AParams); success := true; break; // Exit the loop if we reach this point successfully. except - success := false; + //success := false; end; end; diff --git a/components/fpspreadsheet/source/common/xlsbiff2.pas b/components/fpspreadsheet/source/common/xlsbiff2.pas index 58cc9e0d4..e4c4cd84b 100644 --- a/components/fpspreadsheet/source/common/xlsbiff2.pas +++ b/components/fpspreadsheet/source/common/xlsbiff2.pas @@ -574,6 +574,7 @@ var BIFF2EOF: Boolean; RecordType: Word; CurStreamPos: Int64; + BOFFound: Boolean; begin Unused(AParams); BIFF2EOF := False; @@ -582,6 +583,7 @@ begin FWorksheet := FWorkbook.AddWorksheet('Sheet', true); { Read all records in a loop } + BOFFound := false; while not BIFF2EOF do begin { Read the record header } @@ -592,7 +594,7 @@ begin case RecordType of INT_EXCEL_ID_BLANK : ReadBlank(AStream); - INT_EXCEL_ID_BOF : ; + INT_EXCEL_ID_BOF : BOFFound := true; INT_EXCEL_ID_BOOLERROR : ReadBool(AStream); INT_EXCEL_ID_BOTTOMMARGIN : ReadMargin(AStream, 3); INT_EXCEL_ID_CODEPAGE : ReadCodePage(AStream); @@ -630,7 +632,11 @@ begin // Make sure we are in the right position for the next record AStream.Seek(CurStreamPos + RecordSize, soFromBeginning); - if AStream.Position >= AStream.Size then BIFF2EOF := True; + if AStream.Position >= AStream.Size then + BIFF2EOF := True; + + if not BOFFound then + raise Exception.Create('BOF record not found.'); end; FixCols(FWorksheet); diff --git a/components/fpspreadsheet/source/common/xlsbiff8.pas b/components/fpspreadsheet/source/common/xlsbiff8.pas index 4af64b3d6..9151f86b8 100644 --- a/components/fpspreadsheet/source/common/xlsbiff8.pas +++ b/components/fpspreadsheet/source/common/xlsbiff8.pas @@ -957,11 +957,12 @@ begin try OLEDocument.Stream := OLEStream; OLEStorage.ReadOLEStream(AStream, OLEDocument, 'Workbook'); + InternalReadFromStream(OLEStream); finally OLEStorage.Free; end; - InternalReadFromStream(OLEStream); +// InternalReadFromStream(OLEStream); // wp: moved up finally OLEStream.Free; diff --git a/components/fpspreadsheet/source/visual/fpspreadsheetctrls.pas b/components/fpspreadsheet/source/visual/fpspreadsheetctrls.pas index 6921e0dae..ba1e2c5f0 100644 --- a/components/fpspreadsheet/source/visual/fpspreadsheetctrls.pas +++ b/components/fpspreadsheet/source/visual/fpspreadsheetctrls.pas @@ -971,43 +971,6 @@ end; procedure TsWorkbookSource.InternalLoadFromFile(AFileName: string; AAutoDetect: Boolean; AFormatID: TsSpreadFormatID; AWorksheetIndex: Integer = -1); -{ -// this version avoids loading a defective workbook into the grid, -// but tabcontrol navigation is not working here... - -var - book: TsWorkbook; -begin - book := TsWorkbook.Create; - try - if AAutoDetect then - book.ReadFromFile(AFileName) - else - book.ReadFromFile(AFileName, AFormatID); - - InternalCreateNewWorkbook(book); // book --> FWorkbook - except - book.Free; - InternalCreateNewWorkbook; - // --> empty workbook, but avoid having no worksheet - FWorksheet := FWorkbook.AddWorksheet(Format(rsDefaultSheetName, [1])); - DoShowError(Format(rsCannotReadFile, [AFileName])); - end; - - if AWorksheetIndex = -1 then - begin - if FWorkbook.ActiveWorksheet <> nil then - AWorksheetIndex := FWorkbook.GetWorksheetIndex(FWorkbook.ActiveWorksheet) else - AWorksheetIndex := 0; - end; - - SelectWorksheet(FWorkbook.GetWorkSheetByIndex(AWorksheetIndex)); - - // If required, display loading error message - if FWorkbook.ErrorMsg <> '' then - DoShowError(FWorkbook.ErrorMsg); -end; -} var book: TsWorkbook; begin @@ -1018,51 +981,13 @@ begin book.ReadfromFile(AFileName) else book.ReadFromFile(AFileName, AFormatID); + InternalLoadFromWorkbook(book, AWorksheetIndex); except - book.AddErrorMsg(rsCannotReadFile, [AFileName]); - // Code executed subsequently will be a pain if there is no worksheet! --> Add one. - if book.GetWorksheetCount = 0 then - book.AddWorksheet(Format(rsDefaultSheetName, [1])); + // book is normally used as current workbook. But it must be destroyed + // if the file cannot be read. + book.Free; + raise; end; - - InternalLoadFromWorkbook(book, AWorksheetIndex); - - (* - - - // Create a new empty workbook - InternalCreateNewWorkbook; - - DisableControls; - try - // Read workbook from file and get worksheet - try - if AAutoDetect then - FWorkbook.ReadFromFile(AFileName) - else - FWorkbook.ReadFromFile(AFileName, AFormatID); - except - FWorkbook.AddErrorMsg(rsCannotReadFile, [AFileName]); - // Code executed subsequently will be a pain if there is no worksheet! - if FWorkbook.GetWorksheetCount = 0 then - FWorkbook.AddWorksheet(Format(rsDefaultSheetName, [1])); - end; - finally - EnableControls; - end; - - if AWorksheetIndex = -1 then - begin - if FWorkbook.ActiveWorksheet <> nil then - AWorksheetIndex := FWorkbook.GetWorksheetIndex(FWorkbook.ActiveWorksheet) else - AWorksheetIndex := 0; - end; - SelectWorksheet(FWorkbook.GetWorkSheetByIndex(AWorksheetIndex)); - - // If required, display loading error message - if FWorkbook.ErrorMsg <> '' then - DoShowError(FWorkbook.ErrorMsg); - *) end; procedure TsWorkbookSource.InternalLoadFromWorkbook(AWorkbook: TsWorkbook;