AutoWidthCol does not work properly with user defined format

Questions and answers on how to use XLSReadWriteII 5.
Post Reply
d3nton
Posts: 137
Joined: Thu Oct 25, 2012 9:48 am

AutoWidthCol does not work properly with user defined format

Post by d3nton »

Hi.
I am using v5.20.49 and use the AutoWidthCol function to adjuste the cell width of user defined cells like '125,5 KB'.
Unfortunately most of the time the widest cell is cut of a bit.
I have debuged the code and the problem is caused by a wrong while loop in XLSMask5.pas function TExcelMask.FormatNumberNumber(FD: TFormatData; Value: double): AxUCString;
lines 728 - 733:

Code: Select all

            j := 0;
            while (j < Length(Data[i].S)) and (ResPos < SZ_RESBUF) do begin
              Res[ResPos] := Data[i].S[j];
              Inc(j);
              Inc(ResPos);
            end;
This code caused the function FormatNumberNumber to return '125,5 K' instead of '125,5 KB'
Strings are not zero based in delphi. I think the code have to be:

j := 1;
while (j <= Length(Data.S)) and (ResPos < SZ_RESBUF) do begin
Res[ResPos] := Data.S[j];
Inc(j);
Inc(ResPos);
end;

Also please take a look into XLSSheetData5.pas function TXLSWorksheet.AutoWidthCol(const ACol: integer): integer; lines 1528 and 1530:

Code: Select all

          W := Canvas.TextWidth(GetAsFmtString(ACol,FCells.IterCellRow)) + TM.tmAveCharWidth + 5;
The function call GetAsFmtString returns a wrong value due to the wrong implementation of FormatNumberNumber described above. You added TM.tmAveCharWidth (i think because one the missing character) when calling Canvas.TextWidth in this line. i think this is no longer necessary if you fix the problem above.

Code: Select all

 + TM.tmAveCharWidth
could be removed here.

I am sorry for my bad english but I hope you understand the problem. If s.th. is not clear to you, please ask me.

I have also a question regarding the same lines (1528/1530): why do you add "+5" to the text width?
I guess you add this constant to the width because a few lines below (line 1538) you call

Code: Select all

 FColumns[ACol].PixelWidth := Result;

The setter of the property PixelWith is implemented like this:

Code: Select all

procedure TXLSColumn.SetPixelWidth(const AValue: integer);
begin
  SetWidth(Round(Trunc((AValue - 5) / FOwner.FStyles.StdFontWidth * 100 + 0.5) / 100) * 256);
end;
Why do you add +5 but remove it in the setter?
Also in my tests it seems that +5 is not sufficient. The values in the cells are still cut of. Can you fix this (e.g. using a higher value like +15, or set it to +10 but remove the -5 from the setter)?
Thanks once again for your help.
larsa
Site Admin
Posts: 926
Joined: Mon Jun 27, 2005 9:30 pm

Re: AutoWidthCol does not work properly with user defined format

Post by larsa »

Hello

Thank you for your fix. It will be included in the next update. +5 is added for the text margin in the cell. It's not documented how to calculate the margin.
Lars Arvidsson, Axolot Data
Post Reply