Skip to content

Commit

Permalink
worksheet: add very_hidden() method
Browse files Browse the repository at this point in the history
Request #228
  • Loading branch information
jmcnamara committed Oct 8, 2023
1 parent 41f194a commit 9de5e9b
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 9 deletions.
1 change: 1 addition & 0 deletions lib/Excel/Writer/XLSX.pm
Original file line number Diff line number Diff line change
Expand Up @@ -735,6 +735,7 @@ The following methods are available through a new worksheet:
activate()
select()
hide()
very_hidden()
set_first_sheet()
protect()
unprotect_range()
Expand Down
18 changes: 12 additions & 6 deletions lib/Excel/Writer/XLSX/Package/Packager.pm
Original file line number Diff line number Diff line change
Expand Up @@ -396,18 +396,24 @@ sub _write_app_file {

_mkdir( $dir . '/docProps' );

# Add the Worksheet heading pairs.
$app->_add_heading_pair( [ 'Worksheets', $self->{_worksheet_count} ] );

# Add the Chartsheet heading pairs.
$app->_add_heading_pair( [ 'Charts', $self->{_chartsheet_count} ] );

# Add the Worksheet parts.
my $worksheet_count = 0;
for my $worksheet ( @{ $self->{_workbook}->{_worksheets} } ) {
next if $worksheet->{_is_chartsheet};

# Don't write/count veryHidden sheets.
next if $worksheet->{_hidden} == 2;

$app->_add_part_name( $worksheet->get_name() );
$worksheet_count++;
}

# Add the Worksheet heading pairs.
$app->_add_heading_pair( [ 'Worksheets', $worksheet_count ] );

# Add the Chartsheet heading pairs.
$app->_add_heading_pair( [ 'Charts', $self->{_chartsheet_count} ] );

# Add the Chartsheet parts.
for my $worksheet ( @{ $self->{_workbook}->{_worksheets} } ) {
next unless $worksheet->{_is_chartsheet};
Expand Down
10 changes: 8 additions & 2 deletions lib/Excel/Writer/XLSX/Workbook.pm
Original file line number Diff line number Diff line change
Expand Up @@ -2787,10 +2787,16 @@ sub _write_sheet {
'sheetId' => $sheet_id,
);

push @attributes, ( 'state' => 'hidden' ) if $hidden;
push @attributes, ( 'r:id' => $r_id );
if ( $hidden == 1 ) {
push @attributes, ( 'state' => 'hidden' );
}
elsif ( $hidden == 2 ) {
push @attributes, ( 'state' => 'veryHidden' );
}


push @attributes, ( 'r:id' => $r_id );

$self->xml_empty_tag( 'sheet', @attributes );
}

Expand Down
17 changes: 16 additions & 1 deletion lib/Excel/Writer/XLSX/Worksheet.pm
Original file line number Diff line number Diff line change
Expand Up @@ -472,8 +472,9 @@ sub activate {
sub hide {

my $self = shift;
my $hidden = shift || 1;

$self->{_hidden} = 1;
$self->{_hidden} = $hidden;

# A hidden worksheet shouldn't be active or selected.
$self->{_selected} = 0;
Expand All @@ -482,6 +483,20 @@ sub hide {
}


###############################################################################
#
# very_hidden()
#
# Hide this worksheet. This can only be unhidden from VBA.
#
sub very_hidden {

my $self = shift;

$self->hide( 2 );
}


###############################################################################
#
# set_first_sheet()
Expand Down
72 changes: 72 additions & 0 deletions t/regression/hide02.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
###############################################################################
#
# Tests the output of Excel::Writer::XLSX against Excel generated files.
#
# Copyright 2000-2023, John McNamara, [email protected]
#
# SPDX-License-Identifier: Artistic-1.0-Perl OR GPL-1.0-or-later
#

use lib 't/lib';
use TestFunctions qw(_compare_xlsx_files _is_deep_diff);
use strict;
use warnings;

use Test::More tests => 1;

###############################################################################
#
# Tests setup.
#
my $filename = 'hide02.xlsx';
my $dir = 't/regression/';
my $got_filename = $dir . "ewx_$filename";
my $exp_filename = $dir . 'xlsx_files/' . $filename;

my $ignore_members = [];
my $ignore_elements = {};


###############################################################################
#
# Test the creation of a simple Excel::Writer::XLSX file.
#
use Excel::Writer::XLSX;

my $workbook = Excel::Writer::XLSX->new( $got_filename );

my $worksheet1 = $workbook->add_worksheet();
my $worksheet2 = $workbook->add_worksheet();
my $worksheet3 = $workbook->add_worksheet();

$worksheet2->very_hidden();

$workbook->close();


###############################################################################
#
# Compare the generated and existing Excel files.
#

my ( $got, $expected, $caption ) = _compare_xlsx_files(

$got_filename,
$exp_filename,
$ignore_members,
$ignore_elements,
);

_is_deep_diff( $got, $expected, $caption );


###############################################################################
#
# Cleanup.
#
unlink $got_filename;

__END__
Binary file added t/regression/xlsx_files/hide02.xlsx
Binary file not shown.

0 comments on commit 9de5e9b

Please sign in to comment.