ag4ve has asked for the wisdom of the Perl Monks concerning the following question:

has anyone figured how to write dynamic col widths with Spreadsheet::WriteExcel? i can do set_column well enough to set a default width. however, i haven't found a good way to sed widths based on the longest string i put into the columns.

i can get the max length of a string, but setting the width based on this doesn't seem to work properly (ie, it isn't wide enough). i saw someone recommend Excel::Template::Element::Cell but cpan seems sparse for this and i can't figure out how to use it.

Replies are listed 'Best First'.
Re: WriteExcel dynamic width
by jmcnamara (Monsignor) on Jan 10, 2011 at 11:10 UTC
    See the autofit.pl example in the docs for a working example and an explanation of the issues involved.

    --
    John.

      that's what i was looking for (and than some). thanks

Re: WriteExcel dynamic width
by marto (Cardinal) on Jan 10, 2011 at 11:02 UTC

    I'm not sure it's possible to set a column to autofit, from the docs:

    "The width corresponds to the column width value that is specified in Excel. It is approximately equal to the length of a string in the default font of Arial 10. Unfortunately, there is no way to specify "AutoFit" for a column in the Excel file format. This feature is only available at runtime from within Excel."

    Update: Given that you can determine the maximum string length perhaps you could try adding some padding depending on the font/size being used.