You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: lib/Excel/Writer/XLSX.pm
+28-1Lines changed: 28 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -2389,7 +2389,7 @@ Examples:
2389
2389
2390
2390
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 Calibri 11. To set the width in pixels use the C<set_column_pixels()> method, see below.
2391
2391
2392
-
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.
2392
+
See also the C<autofit()> method to set the column widths based on the data in the column, approximately.
2393
2393
2394
2394
As usual the C<$format> parameter is optional, for additional information, see L</CELL FORMATTING>. If you wish to set the format without changing the width you can pass C<undef> as the width parameter:
2395
2395
@@ -2459,6 +2459,33 @@ The option to hide unused rows is used by Excel as an optimisation so that the u
2459
2459
See the C<hide_row_col.pl> example program.
2460
2460
2461
2461
2462
+
=head2autofit($max_width)
2463
+
2464
+
Autofit the worksheet column widths to the widest data in the column, approximately.
2465
+
2466
+
$worksheet->autofit();
2467
+
2468
+
Excel autofits columns at runtime when it has access to all of the required worksheet information as well as the Windows functions for calculating display areas based on fonts and formatting. Excel::Writer::XLSX doesn't have access to these Windows functions so it simulates autofit by calculating string widths based on metrics taken from Excel. This isn't perfect but for most cases it should be sufficient and indistinguishable from the output of Excel. However there are some limitations to be aware of when using this method:
2469
+
2470
+
=over4
2471
+
2472
+
=item* It is based on the default Excel font type and size of Calibri 11. It will not give accurate results for other fonts or font sizes.
2473
+
2474
+
=item* It doesn't take formatting of numbers or dates account, although this may be addressed in a later version.
2475
+
2476
+
=item* Autofit is a relatively expensive operation since it performs a calculation for all the populated cells in a worksheet. See the note on performance below.
2477
+
2478
+
=back
2479
+
2480
+
For cases that don't match your desired output you can set explicit column widths via C<set_column()> or C<set_column_pixels()> method ignores columns that have already been explicitly set if the width is greater than the calculated autofit width. Alternatively, setting the column width explicitly after calling C<autofit()> will override the autofit value. You can also set an upper limit using the optional C<$max_width> parameter as explained below.
2481
+
2482
+
Excel autofits very long strings up to limit of 1790 pixels/255 characters. This is often too wide to display on a single screen at normal zoom. As such the optional C<$max_width> parameter is provided to enable a smaller upper pixel limit for autofitting long strings. A value of 300 pixels is recommended as a good compromise between column width and readability:
2483
+
2484
+
$worksheet->autofit(300);
2485
+
2486
+
If you need more control over the autofit effect you can use the L<Excel::Writer::XLSX::Utility>C<xl_cell_autofit_width()> function to calculate the autofit width for a cell value. This is the same calculation used internally by C<autofit()>.
# Get the pixel width of a string based on individual character widths taken
535
535
# from Excel. UTF8 characters are given a default width of 8.
536
536
#
537
537
# Note, Excel adds an additional 7 pixels padding to a cell.
538
538
#
539
-
subxl_string_pixel_width {
539
+
subxl_cell_autofit_width {
540
540
my$length = 0;
541
541
542
542
formy$char (split //, shift) {
@@ -1092,6 +1092,24 @@ This functions takes a cell reference string in A1 notation and decrements the c
1092
1092
my $str = xl_dec_col( '$C1' ); # $B1
1093
1093
my $str = xl_dec_col( '$E$5' ); # $D$5
1094
1094
1095
+
1096
+
=head2xl_cell_autofit_width($string)
1097
+
1098
+
Parameters: $string, The string to calculate the cell width for.
1099
+
1100
+
Returns: The string autofit width in pixels. Returns 0 if the string is empty.
1101
+
1102
+
This function calculates the width required to auto-fit a string in a cell:
1103
+
1104
+
my $width = xl_cell_autofit_width($string);
1105
+
1106
+
The Worksheet C<autofit()> method can be used to auto-fit cell data to the optimal column width. However, in some cases you may wish to handle auto-fitting yourself and apply additional logic to limit the maximum and minimum ranges.
1107
+
1108
+
The C<xl_cell_autofit_width()> function can be used to perform the required calculation. It works by estimating the pixel width of a string based on the width of each character. It also adds a 7 pixel padding for the cell boundary in the same way that Excel does.
1109
+
1110
+
You can use the calculated width in conjunction with Worksheet C<set_column_pixels()> method.
1111
+
1112
+
1095
1113
=head1TIME AND DATE FUNCTIONS
1096
1114
1097
1115
Dates and times in Excel are represented by real numbers, for example "Jan 1 2001 12:30:14 PM" is represented by the number 36892.521.
0 commit comments