Excel calculates its cell width in points (1/72 of an inch). Given the width of each character in a font face for a given font size. Take your lengthiest string (which may not neccessarily need the most room (but should on most occassions) and then calculate how many points are needed to hold that string by

#assuming char width values in inches my %char_width(a=>1, b=>1.5); #etc.etc. #pseudo-code my $cell_width; while(length($string) != 0) { my $char = ($string =~ s/^(.{1})//); $cell_width += $char_width{$char}; }; $cell_width /= 72;
The length of the string has nothing to do with the calculation unless you are using a fixed width font face. In which case you can multiply the number of letters in the string by the width of a character in that font face for that font size
my $cell_width = length($string) * $char_width;

Also, just a question..When you say auto-sized cells, I assume you mean at point of initial template creation. To my knowledge there is no means to keep a cell auto-sized as the data in it changes.


Grygonos

In reply to Re: Excel Width Calculation by Grygonos
in thread Excel Width Calculation by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.