Skip to content

Commit bdf6bc4

Browse files
committed
autofit: preliminary work for setting max autofit width
1 parent d6adc38 commit bdf6bc4

File tree

3 files changed

+82
-3
lines changed

3 files changed

+82
-3
lines changed

lib/Excel/Writer/XLSX/Worksheet.pm

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -774,8 +774,16 @@ sub set_column_pixels {
774774
#
775775
sub autofit {
776776
my $self = shift;
777+
my $max_width = shift || 255.0;
777778
my %col_width = ();
778779

780+
# Convert the autofit maximum pixel width to a column/character width, but
781+
# limit it to the Excel max limit.
782+
$max_width = _pixels_to_width($max_width);
783+
if ( $max_width > 255.0 ) {
784+
$max_width = 255.0;
785+
}
786+
779787
# Create a reverse lookup for the share strings table so we can convert
780788
# the string id back to the original string.
781789
my @strings;
@@ -919,9 +927,9 @@ sub autofit {
919927
# additional padding of 7 pixels, like Excel.
920928
my $width = _pixels_to_width( $pixel_width + 7 );
921929

922-
# The max column character width in Excel is 255.
923-
if ( $width > 255.0 ) {
924-
$width = 255.0;
930+
# Limit the width to the maximum user or Excel value.
931+
if ( $width > $max_width ) {
932+
$width = $max_width;
925933
}
926934

927935
# Add the width to an existing col info structure or add a new one.

t/regression/autofit14.t

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
###############################################################################
2+
#
3+
# Tests the output of Excel::Writer::XLSX against Excel generated files.
4+
#
5+
# Copyright 2000-2024, John McNamara, jmcnamara@cpan.org
6+
#
7+
# SPDX-License-Identifier: Artistic-1.0-Perl OR GPL-1.0-or-later
8+
#
9+
10+
use lib 't/lib';
11+
use TestFunctions qw(_compare_xlsx_files _is_deep_diff);
12+
use strict;
13+
use warnings;
14+
15+
use Test::More tests => 1;
16+
17+
###############################################################################
18+
#
19+
# Tests setup.
20+
#
21+
my $filename = 'autofit14.xlsx';
22+
my $dir = 't/regression/';
23+
my $got_filename = $dir . "ewx_$filename";
24+
my $exp_filename = $dir . 'xlsx_files/' . $filename;
25+
26+
my $ignore_members = [];
27+
my $ignore_elements = {};
28+
29+
30+
###############################################################################
31+
#
32+
# Test the creation of a simple Excel::Writer::XLSX file.
33+
#
34+
use Excel::Writer::XLSX;
35+
36+
my $workbook = Excel::Writer::XLSX->new( $got_filename );
37+
my $worksheet = $workbook->add_worksheet();
38+
39+
$worksheet->write_string( 0, 0, "This is some long text to test autofit" );
40+
41+
$worksheet->autofit(200);
42+
43+
$workbook->close();
44+
45+
46+
###############################################################################
47+
#
48+
# Compare the generated and existing Excel files.
49+
#
50+
51+
my ( $got, $expected, $caption ) = _compare_xlsx_files(
52+
53+
$got_filename,
54+
$exp_filename,
55+
$ignore_members,
56+
$ignore_elements,
57+
);
58+
59+
_is_deep_diff( $got, $expected, $caption );
60+
61+
62+
###############################################################################
63+
#
64+
# Cleanup.
65+
#
66+
unlink $got_filename;
67+
68+
__END__
69+
70+
71+
5.72 KB
Binary file not shown.

0 commit comments

Comments
 (0)