Example for AutoFitCol


I'm looking for an AutofitCol example.
My actual test is:

xls.AutofitCol(1, xls.ColCount, 10, 500, True, 100, 100, 100, 500, TAutofitMerged.None);

But all information are too small.

Adjustment should be about 1.1-1.3, not 100 which is too big. Adjustment fixed should be normally 0 and probably there is no need to set a min and max width. Also, probably you don't want to ignore the strings.

If you try with:
xlso.AutofitCol(1, xls.ColCount, 10, 500, false, 1.2, 0, 0, 0, TAutofitMerged.None);

Does is still not work?


It seem to work.
Now I have to do fine tuning.

Thank you for fast reply.

Adrian: I am creating a spreadsheet with "xls" as a TXLSFile and I cannot get the AutoFitCol to work:


When I open up the "filename" spreadsheet, no columns were resized.


Rick Stephens

Hi, the first parameter after the "False" is the adjustment, and that is a value that will be multiplied by the width to give some margin to the cells. So say for example FlexCel calculates the needed width of the column to be 5, if autofit is 1.1, then it will fit the column to 5 * 1.1 = 5.5, so the column will be wider to that the exact minimum, and will work when Excel changes resolutions.

 In an ideal world you would multiply by 1, in reality you need to multiply by 1.1 or 1.2. But if you multiply by 0, then no autofitting will happen because FlexCel will think all columns are of width 0, so they fit without any column change.

Just try:

and it should work.
Note also that you don't need to specify all parameters when calling AutoFitCol, you could just do:

and it would be the same.

Just a note: Maybe you were confused about "Adjustment" and "Adjustment fixed". As said in the previous post, adjustment is a number you multiply to the final result. On the other hand, adjustmentFixed is a number you add to the final result.

The formula is:

Final width of the column = Calculated_width * adjustment + adjustment_fixed.

So the "natural" values for the final column to be the calculated width would be:
  adjustment = 1
  adjustment_fixed = 0.

In reality, you normally keep adjustment_fixed 0, but set adjustment to about 1.2 to compensate for what Excel shows at different dpis.
Yes. that works fine now. You are right that I was a bit confused by the earlier post. Your explanation makes perfect sense.

Thank you!