From cdf96e2fa202b8f2c46f5aa7c86779521fad8de8 Mon Sep 17 00:00:00 2001 From: wp_xxyyzz Date: Mon, 18 Jan 2016 21:26:19 +0000 Subject: [PATCH] fpspreadsheet: Fix TsWorksheet occasionally crashing in new SetCells method. git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@4436 8e941d3f-bd1b-0410-a28a-d453659cc2b4 --- .../fpspreadsheet/fpspreadsheetgrid.pas | 76 ++++++++---------- .../reference/BIFFExplorer/BIFFExplorer.res | Bin 26628 -> 27196 bytes 2 files changed, 34 insertions(+), 42 deletions(-) diff --git a/components/fpspreadsheet/fpspreadsheetgrid.pas b/components/fpspreadsheet/fpspreadsheetgrid.pas index 46103e3af..85c3a90ad 100644 --- a/components/fpspreadsheet/fpspreadsheetgrid.pas +++ b/components/fpspreadsheet/fpspreadsheetgrid.pas @@ -4440,8 +4440,8 @@ end; procedure TsCustomWorksheetGrid.SetCells(ACol, ARow: Integer; AValue: Variant); var - cell: PCell; - fmt: PsCellFormat; + cell: PCell = nil; + fmt: PsCellFormat = nil; nfp: TsNumFormatParams; r, c: Cardinal; s: String; @@ -4451,52 +4451,44 @@ begin r := GetWorksheetRow(ARow); c := GetWorksheetCol(ACol); - cell := Worksheet.FindCell(r, c); - if cell <> nil then begin - fmt := Workbook.GetPointerToCellFormat(cell^.FormatIndex); - if fmt <> nil then nfp := Workbook.GetNumberFormat(fmt^.NumberFormatIndex); - end else - fmt := nil; - if VarIsNull(AValue) then begin - cell := Worksheet.WriteBlank(r, c); - if cell <> nil then cell^.FormulaValue := ''; - end else + // If the cell already exists and contains a formula then the formula must be + // removed. The formula would dominate over the data value. + cell := Worksheet.FindCell(r, c); + if HasFormula(cell) then cell^.FormulaValue := ''; + + if VarIsNull(AValue) then + Worksheet.WriteBlank(r, c) + else if VarIsStr(AValue) then begin s := VarToStr(AValue); - if s[1] = '=' then - begin - Worksheet.WriteFormula(r, c, Copy(s, 2, Length(s)), true); - exit; - end; - Worksheet.WriteCellValueAsString(r, c, s); - cell^.FormulaValue := ''; - end else - begin - if cell <> nil then - cell^.FormulaValue := ''; - if VarIsType(AValue, varDate) then - Worksheet.WriteDateTime(r, c, VarToDateTime(AValue)) + if (s <> '') and (s[1] = '=') then + Worksheet.WriteFormula(r, c, Copy(s, 2, Length(s)), true) else - if VarIsNumeric(AValue) then begin - if (cell <> nil) then begin - if IsDateTimeFormat(nfp) then - Worksheet.WriteDateTime(cell, VarToDateTime(AValue)) - { - else if IsBoolFormat(nfp) then - Worksheet.WriteBoolValue(cell, not (AValue=0) ) - else if IsErrorFormat(nfp) then - Worksheet.WriteErrorValue(r, c, round(AValue)); - } - else - Worksheet.WriteNumber(cell, AValue); - end else + Worksheet.WriteText(r, c, s); // This will erase a non-formatted cell if s = '' + end else + if VarIsType(AValue, varDate) then + Worksheet.WriteDateTime(r, c, VarToDateTime(AValue)) + else + if VarIsNumeric(AValue) then + begin + // Check if the cell already exists and contains a format. + // If it is a date/time format write a date/time cell... + if cell <> nil then + begin + fmt := Workbook.GetPointerToCellFormat(cell^.FormatIndex); + if fmt <> nil then nfp := Workbook.GetNumberFormat(fmt^.NumberFormatIndex); + if (fmt <> nil) and IsDateTimeFormat(nfp) then + Worksheet.WriteDateTime(r, c, VarToDateTime(AValue)) else Worksheet.WriteNumber(r, c, AValue); - end else - if VarIsBool(AValue) then - Worksheet.WriteBoolValue(r, c, AValue) - end; + end + else + // ... otherwise write a number cell + Worksheet.WriteNumber(r, c, AValue); + end else + if VarIsBool(AValue) then + Worksheet.WriteBoolValue(r, c, AValue); end; function TsCustomWorksheetGrid.GetHorAlignment(ACol, ARow: Integer): TsHorAlignment; diff --git a/components/fpspreadsheet/reference/BIFFExplorer/BIFFExplorer.res b/components/fpspreadsheet/reference/BIFFExplorer/BIFFExplorer.res index abecf2329b857ce56a4e4c5303938b41634de613..701ea761fe84b511a3d91ed901a8319a3984b29c 100644 GIT binary patch delta 560 zcmaLV%}N6?5C?E-MM3O4#P#a5`Q8NDUZp1wf}SUv1Qu#{v8z%<7QqKld=l}h@8H4r z(WUiJwe2y<@SEX3dH86(eYPIS)?nu)Ico1`#YKr#?(%V7J>4JH%ja45nkB=|9wB`! z%WtseZz$Va`BIDqq6HOAqK$D9CX`)vYnL@jqMDOoD`@O~DcNafl_B?8Ty&iABXWvyS Bok{=z delta 37 tcmdmUg|X!V;{*k6HWmg31qKF&|NkW>+p~*qbl_v2+{*fEa|7F+WB}WJ3_$<@