I want to set the width of one column to be the same width as the cell in another column where I have added text and then used AutoFit to set the set the cell width.
Therefore the steps are:
1. Add the text to the 1st cell;
2. Use AutoFit on the 2nd cell;
3. Measure the width of the 1st cell ;
4. Set the 2nd column to the width of the 1st cell;
5. Measure the width of the 2nd column.;

I have done this in the Perl below and as the printout shows the 1st cell’s width is 108 and the 2nd column is 570.75.
This is an increase of 5.285.
I also set another column to 60. The measured width is 318.75.
This is an increase of 5.3125.

Can anyone explain:
1. Why this increase is happening;
2. I can reliably get exactly what I want?
use OLE; use Win32::OLE::Const "Microsoft Excel"; use strict "vars"; my ($excel, $workbook, $sheet); my ($w1, $w2, $w3, $w4); #___ DEFINE EXCEL $excel = CreateObject OLE "Excel.Application"; #___ MAKE EXCEL VISIBLE $excel -> {Visible} = 1; #___ ADD NEW WORKBOOK $workbook = $excel -> Workbooks -> Add; $sheet = $workbook -> Worksheets("Sheet1"); $sheet -> Activate; # find initial width of B4 $w1 = $sheet->Range("B4")->Width; # add text to B4 and autofit the cell $sheet -> Range("B4") -> {Value} = "The cat sat on the mat"; $sheet -> Range("B4") -> Columns -> {AutoFit} = "True"; # find new width of B4 $w2 = $sheet->Range("B4")->Width; # set column D to cell width found for cell F4 $sheet -> Range("D4") -> {ColumnWidth} = $w2; # find width of column D $w3 = $sheet->Range("D4")->Width; # set column F to 60 $sheet -> Range("F4") -> {ColumnWidth} = 60; # find width of column F $w4 = $sheet->Range("F4")->Width; print "F4 width 1<$w1> 2 <$w2> H4 width <$w3> F4 width <$w4>\n";

In reply to Problems setting Excel cell width by merrymonk

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.