Wrong format when both Row and Column format defined
Posted: Wed Dec 09, 2009 6:29 pm
I have an Excel template into which I am writing values using XLSReadWriteII.
I am using XLSReadWriteII v3 and Excel 2000.
The template has Column Formats, Row Formats, and Cell Formats defined.
For cells which have both column and row format defined, when XLSReadWriteII writes data into an empty cell, the cell is created and formatted with the wrong format: It is taking the column format when it should take the row format.
I have traced the problem to TSheet.GetDefaultFormat.
I have done some testing, and it seems that if both a ROW format and a COLUMN format are defined for an empty cell, Excel uses the ROW format in precedence to the COLUMN format. (If in Excel I format the ROW first and then the COLUMN, so that the intersecting cells should take the COLUMN format, Excel automatically creates BLANK cell records with XF index pointing to appropriate the COLUMN format XF record, thus overriding the ROW format. If I format the COLUMN first and then the ROW, no BLANK cell records are created, and the ROW format takes precedence, as expected.)
Thus, when a new cell is created the cell formatting should be
If CELL format defined, use CELL format
else if ROW format is defined, use ROW format
else if COLUMN format is defined, use COLUMN format
else DEFAULT format.
I have updated my own version of function TSheet.GetDefaultFormat and now my output spreadsheets are formatted correctly.
Regards,
Edward Benson.
//------------------------------------------------------------------
// My version
function TSheet.GetDefaultFormat(Col,Row: integer): word;
var
XRow: TXLSRow;
begin
XRow := FRows.Find(Row);
// Use ROW format, if defined
if (XRow <> Nil) and (XRow.FormatIndex <> DEFAULT_FORMAT) then begin
XRow.Formats[XRow.FormatIndex].IncUsageCount;
Result := XRow.FormatIndex;
end
// Else use COLUMN format, if defined
else if (FColumns[Col] <> Nil) and (FColumns[Col].FormatIndex <> DEFAULT_FORMAT) then begin
Result := FColumns[Col].FormatIndex;
FColumns[Col].Formats[FColumns[Col].FormatIndex].IncUsageCount;
end
// Else use DEFAULT format
else
Result := DEFAULT_FORMAT;
end;
//------------------------------------------------------------------
// Old version
function TSheet.GetDefaultFormat(Col,Row: integer): word;
var
XRow: TXLSRow;
begin
if (FColumns[Col] <> Nil) and (FColumns[Col].FormatIndex <> DEFAULT_FORMAT) then begin
Result := FColumns[Col].FormatIndex;
FColumns[Col].Formats[FColumns[Col].FormatIndex].IncUsageCount;
end
else begin
XRow := FRows.Find(Row);
if (XRow <> Nil) and (XRow.FormatIndex <> DEFAULT_FORMAT) then begin
XRow.Formats[XRow.FormatIndex].IncUsageCount;
Result := XRow.FormatIndex;
end
else
Result := DEFAULT_FORMAT;
end;
end;
I am using XLSReadWriteII v3 and Excel 2000.
The template has Column Formats, Row Formats, and Cell Formats defined.
For cells which have both column and row format defined, when XLSReadWriteII writes data into an empty cell, the cell is created and formatted with the wrong format: It is taking the column format when it should take the row format.
I have traced the problem to TSheet.GetDefaultFormat.
I have done some testing, and it seems that if both a ROW format and a COLUMN format are defined for an empty cell, Excel uses the ROW format in precedence to the COLUMN format. (If in Excel I format the ROW first and then the COLUMN, so that the intersecting cells should take the COLUMN format, Excel automatically creates BLANK cell records with XF index pointing to appropriate the COLUMN format XF record, thus overriding the ROW format. If I format the COLUMN first and then the ROW, no BLANK cell records are created, and the ROW format takes precedence, as expected.)
Thus, when a new cell is created the cell formatting should be
If CELL format defined, use CELL format
else if ROW format is defined, use ROW format
else if COLUMN format is defined, use COLUMN format
else DEFAULT format.
I have updated my own version of function TSheet.GetDefaultFormat and now my output spreadsheets are formatted correctly.
Regards,
Edward Benson.
//------------------------------------------------------------------
// My version
function TSheet.GetDefaultFormat(Col,Row: integer): word;
var
XRow: TXLSRow;
begin
XRow := FRows.Find(Row);
// Use ROW format, if defined
if (XRow <> Nil) and (XRow.FormatIndex <> DEFAULT_FORMAT) then begin
XRow.Formats[XRow.FormatIndex].IncUsageCount;
Result := XRow.FormatIndex;
end
// Else use COLUMN format, if defined
else if (FColumns[Col] <> Nil) and (FColumns[Col].FormatIndex <> DEFAULT_FORMAT) then begin
Result := FColumns[Col].FormatIndex;
FColumns[Col].Formats[FColumns[Col].FormatIndex].IncUsageCount;
end
// Else use DEFAULT format
else
Result := DEFAULT_FORMAT;
end;
//------------------------------------------------------------------
// Old version
function TSheet.GetDefaultFormat(Col,Row: integer): word;
var
XRow: TXLSRow;
begin
if (FColumns[Col] <> Nil) and (FColumns[Col].FormatIndex <> DEFAULT_FORMAT) then begin
Result := FColumns[Col].FormatIndex;
FColumns[Col].Formats[FColumns[Col].FormatIndex].IncUsageCount;
end
else begin
XRow := FRows.Find(Row);
if (XRow <> Nil) and (XRow.FormatIndex <> DEFAULT_FORMAT) then begin
XRow.Formats[XRow.FormatIndex].IncUsageCount;
Result := XRow.FormatIndex;
end
else
Result := DEFAULT_FORMAT;
end;
end;