Skip to content

Commit 02bd6a1

Browse files
committed
chartsheet: add set_zoom_to_fit() method
Add a method to automatically zoom a chartsheet chart to fit the screen.
1 parent fa99759 commit 02bd6a1

File tree

5 files changed

+122
-0
lines changed

5 files changed

+122
-0
lines changed

lib/Excel/Writer/XLSX.pm

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -753,6 +753,7 @@ The following methods are available through a new worksheet:
753753
merge_range()
754754
merge_range_type()
755755
set_zoom()
756+
set_zoom_to_fit()
756757
right_to_left()
757758
hide_zero()
758759
set_background()
@@ -2623,6 +2624,13 @@ The default zoom factor is 100. You cannot zoom to "Selection" because it is cal
26232624
Note, C<set_zoom()> does not affect the scale of the printed page. For that you should use C<set_print_scale()>.
26242625
26252626
2627+
=head2 set_zoom_to_fit()
2628+
2629+
Set a chartsheet to zoom to fit the screen.
2630+
2631+
This method is used to ensure that a chartsheet is zoomed automatically by Excel to fit the screen even when the window is resized.
2632+
2633+
It only has an effect for chartsheets.
26262634
26272635
26282636
=head2 right_to_left()

lib/Excel/Writer/XLSX/Chart.pm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9188,6 +9188,7 @@ The following Worksheet methods are also available through a non-embedded Chart
91889188
set_first_sheet()
91899189
protect()
91909190
set_zoom()
9191+
set_zoom_to_fit()
91919192
set_tab_color()
91929193
91939194
set_landscape()

lib/Excel/Writer/XLSX/Worksheet.pm

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ sub new {
7777
$self->{_embedded_image_indexes} = $_[14];
7878
$self->{_embedded_images} = $_[15];
7979

80+
$self->{_is_chartsheet} = 0;
8081
$self->{_ext_sheets} = [];
8182
$self->{_fileclosed} = 0;
8283
$self->{_excel_version} = 2007;
@@ -155,6 +156,7 @@ sub new {
155156

156157
$self->{_zoom} = 100;
157158
$self->{_zoom_scale_normal} = 1;
159+
$self->{_zoom_to_fit} = 0;
158160
$self->{_print_scale} = 100;
159161
$self->{_right_to_left} = 0;
160162
$self->{_show_zeros} = 1;
@@ -2110,6 +2112,20 @@ sub set_zoom {
21102112
}
21112113

21122114

2115+
###############################################################################
2116+
#
2117+
# set_zoom_to_fit()
2118+
#
2119+
# Set the worksheet zoom to selection/fit. Only works for chartsheets.
2120+
#
2121+
sub set_zoom_to_fit {
2122+
2123+
my $self = shift;
2124+
2125+
$self->{_zoom_to_fit} = 1;
2126+
}
2127+
2128+
21132129
###############################################################################
21142130
#
21152131
# set_print_scale($scale)
@@ -7842,6 +7858,7 @@ sub _write_sheet_view {
78427858
my $tab_selected = $self->{_selected};
78437859
my $view = $self->{_page_view} || 0;
78447860
my $zoom = $self->{_zoom};
7861+
my $zoom_to_fit = $self->{_zoom_to_fit};
78457862
my $row_col_headers = $self->{_hide_row_col_headers};
78467863
my $top_left_cell = $self->{_top_left_cell};
78477864
my $workbook_view_id = 0;
@@ -7908,6 +7925,12 @@ sub _write_sheet_view {
79087925

79097926
push @attributes, ( 'workbookViewId' => $workbook_view_id );
79107927

7928+
if ( $self->{_is_chartsheet} ) {
7929+
if ( $zoom_to_fit ) {
7930+
push @attributes, ( 'zoomToFit' => 1 );
7931+
}
7932+
}
7933+
79117934
if ( @{ $self->{_panes} } || @{ $self->{_selections} } ) {
79127935
$self->xml_start_tag( 'sheetView', @attributes );
79137936
$self->_write_panes();

t/regression/chartsheet11.t

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
###############################################################################
2+
#
3+
# Tests the output of Excel::Writer::XLSX against Excel generated files.
4+
#
5+
# Copyright 2000-2025, 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 = 'chartsheet11.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+
28+
my $ignore_elements = { 'xl/workbook.xml' => ['<workbookView'] };
29+
30+
31+
###############################################################################
32+
#
33+
# Test the worksheet properties of an Excel::Writer::XLSX chartsheet file.
34+
#
35+
use Excel::Writer::XLSX;
36+
37+
my $workbook = Excel::Writer::XLSX->new( $got_filename );
38+
my $worksheet = $workbook->add_worksheet();
39+
my $chart = $workbook->add_chart( type => 'bar' );
40+
41+
# For testing, copy the randomly generated axis ids in the target xlsx file.
42+
$chart->{_chart}->{_axis_ids} = [ 63194240, 63195776 ];
43+
44+
my $data = [
45+
[ 1, 2, 3, 4, 5 ],
46+
[ 2, 4, 6, 8, 10 ],
47+
[ 3, 6, 9, 12, 15 ],
48+
49+
];
50+
51+
$worksheet->write( 'A1', $data );
52+
53+
$chart->add_series( values => '=Sheet1!$A$1:$A$5' );
54+
$chart->add_series( values => '=Sheet1!$B$1:$B$5' );
55+
$chart->add_series( values => '=Sheet1!$C$1:$C$5' );
56+
57+
# Chartsheet test.
58+
$chart->activate();
59+
60+
$chart->set_zoom_to_fit();
61+
62+
$workbook->close();
63+
64+
65+
###############################################################################
66+
#
67+
# Compare the generated and existing Excel files.
68+
#
69+
70+
my ( $got, $expected, $caption ) = _compare_xlsx_files(
71+
72+
$got_filename,
73+
$exp_filename,
74+
$ignore_members,
75+
$ignore_elements,
76+
);
77+
78+
_is_deep_diff( $got, $expected, $caption );
79+
80+
81+
###############################################################################
82+
#
83+
# Cleanup.
84+
#
85+
unlink $got_filename;
86+
87+
__END__
88+
89+
90+
9.39 KB
Binary file not shown.

0 commit comments

Comments
 (0)