in reply to Problems setting Excel cell width

The problem is with mixed units. Width deals with pixel width (I think). ColumnWidth deals with character width (I think). So, for example, you are setting D4 width in characters to B4 width in pixels. I modified part of your code to illustrate the difference.
... # find initial width of B4 $w1 = $sheet->Range("B4")->Width; my $cw1 = $sheet->Range("B4")->{ColumnWidth}; # find new width of B4 $w2 = $sheet->Range("B4")->Width; my $cw2 = $sheet->Range("B4")->{ColumnWidth}; # 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; my $cw3 = $sheet->Range("D4")->{ColumnWidth}; # set column F to 60 $sheet -> Range("F4") -> {ColumnWidth} = 60; # find width of column F $w4 = $sheet->Range("F4")->Width; my $cw4 = $sheet->Range("F4")->{ColumnWidth}; print "B4 width 1<$w1> 2 <$w2> D4 width <$w3> F4 width <$w4>\n"; print "B4 colwidth 1<$cw1> 2 <$cw2> D4 colwidth <$cw3> F4 colwidth <$c +w4>\n";
Which gives the result:
B4 width 1<48> 2 <105> D4 width <555> F4 width <318.75> B4 colwidth 1<8.43> 2 <19.29> D4 colwidth <105> F4 colwidth <60>

Replies are listed 'Best First'.
Re^2: Problems setting Excel cell width
by merrymonk (Hermit) on Jan 27, 2010 at 16:33 UTC
    Thanks for that. I wondered if it was a units issue .
    Do you happen to know if there is some way of retrieving and setting a cell or column width that uses the same unit?
      ColumnWidth will do it.

      To set:

      $sheet -> Range("F4") -> {ColumnWidth} = 60;
      To get:
      my $columnwidth = $sheet -> Range("F4") -> {ColumnWidth};