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
This commit is contained in:
wp_xxyyzz
2016-01-18 21:26:19 +00:00
parent 7b707a09f6
commit cdf96e2fa2
2 changed files with 34 additions and 42 deletions

View File

@ -4440,8 +4440,8 @@ end;
procedure TsCustomWorksheetGrid.SetCells(ACol, ARow: Integer; AValue: Variant); procedure TsCustomWorksheetGrid.SetCells(ACol, ARow: Integer; AValue: Variant);
var var
cell: PCell; cell: PCell = nil;
fmt: PsCellFormat; fmt: PsCellFormat = nil;
nfp: TsNumFormatParams; nfp: TsNumFormatParams;
r, c: Cardinal; r, c: Cardinal;
s: String; s: String;
@ -4451,52 +4451,44 @@ begin
r := GetWorksheetRow(ARow); r := GetWorksheetRow(ARow);
c := GetWorksheetCol(ACol); 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 // If the cell already exists and contains a formula then the formula must be
cell := Worksheet.WriteBlank(r, c); // removed. The formula would dominate over the data value.
if cell <> nil then cell^.FormulaValue := ''; cell := Worksheet.FindCell(r, c);
end else if HasFormula(cell) then cell^.FormulaValue := '';
if VarIsNull(AValue) then
Worksheet.WriteBlank(r, c)
else
if VarIsStr(AValue) then if VarIsStr(AValue) then
begin begin
s := VarToStr(AValue); s := VarToStr(AValue);
if s[1] = '=' then if (s <> '') and (s[1] = '=') then
begin Worksheet.WriteFormula(r, c, Copy(s, 2, Length(s)), true)
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))
else else
if VarIsNumeric(AValue) then begin Worksheet.WriteText(r, c, s); // This will erase a non-formatted cell if s = ''
if (cell <> nil) then begin end else
if IsDateTimeFormat(nfp) then if VarIsType(AValue, varDate) then
Worksheet.WriteDateTime(cell, VarToDateTime(AValue)) Worksheet.WriteDateTime(r, c, VarToDateTime(AValue))
{ else
else if IsBoolFormat(nfp) then if VarIsNumeric(AValue) then
Worksheet.WriteBoolValue(cell, not (AValue=0) ) begin
else if IsErrorFormat(nfp) then // Check if the cell already exists and contains a format.
Worksheet.WriteErrorValue(r, c, round(AValue)); // If it is a date/time format write a date/time cell...
} if cell <> nil then
else begin
Worksheet.WriteNumber(cell, AValue); fmt := Workbook.GetPointerToCellFormat(cell^.FormatIndex);
end else 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); Worksheet.WriteNumber(r, c, AValue);
end else end
if VarIsBool(AValue) then else
Worksheet.WriteBoolValue(r, c, AValue) // ... otherwise write a number cell
end; Worksheet.WriteNumber(r, c, AValue);
end else
if VarIsBool(AValue) then
Worksheet.WriteBoolValue(r, c, AValue);
end; end;
function TsCustomWorksheetGrid.GetHorAlignment(ACol, ARow: Integer): TsHorAlignment; function TsCustomWorksheetGrid.GetHorAlignment(ACol, ARow: Integer): TsHorAlignment;