From: Rob Kinyon Date: Mon, 8 Oct 2007 14:48:07 +0000 (+0000) Subject: Added merge_range as submitted by Stevan Little X-Git-Tag: v0.29~1^2^2 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=refs%2Fheads%2Fbugfixes;p=p5sagit%2FExcel-Template.git Added merge_range as submitted by Stevan Little --- diff --git a/Changes b/Changes index f3eaaab..e903664 100644 --- a/Changes +++ b/Changes @@ -1,5 +1,8 @@ Revision history for Perl distribution Excel::Template +0.29 Mon Oct 08 12:00:00 2007 + - Added merge_range as written by Stevan Little + 0.28 Fri Jul 31 22:00:00 2005 - Added landscape and portrait orientations for worksheets diff --git a/MANIFEST b/MANIFEST index 9198100..f0d3d7a 100644 --- a/MANIFEST +++ b/MANIFEST @@ -33,6 +33,7 @@ lib/Excel/Template/Element/Cell.pm lib/Excel/Template/Element/Formula.pm lib/Excel/Template/Element/FreezePanes.pm lib/Excel/Template/Element/Image.pm +lib/Excel/Template/Element/MergeRange.pm lib/Excel/Template/Element/Range.pm lib/Excel/Template/Element/Var.pm t/001_load.t @@ -78,6 +79,7 @@ t/025_freezepanes.t t/026_vars_in_format.t t/026_vars_in_format.xml t/027_landscape.t +t/028_merge_range.t t/998_pod.t t/999_pod_coverage.t t/mock.pm diff --git a/lib/Excel/Template.pm b/lib/Excel/Template.pm index 26a0d0e..83f2e38 100644 --- a/lib/Excel/Template.pm +++ b/lib/Excel/Template.pm @@ -6,7 +6,7 @@ BEGIN { use Excel::Template::Base; use vars qw ($VERSION @ISA); - $VERSION = '0.28'; + $VERSION = '0.29'; @ISA = qw( Excel::Template::Base ); } diff --git a/lib/Excel/Template/Element/MergeRange.pm b/lib/Excel/Template/Element/MergeRange.pm new file mode 100644 index 0000000..5b42714 --- /dev/null +++ b/lib/Excel/Template/Element/MergeRange.pm @@ -0,0 +1,97 @@ +package Excel::Template::Element::MergeRange; + +use strict; + +BEGIN { + use vars qw(@ISA); + @ISA = qw(Excel::Template::Element); + + use Excel::Template::Element; + use Excel::Template::Element::Range; +} + +sub render { + my $self = shift; + my ($context) = @_; + + my $ref_name = $context->resolve($self, 'REF'); + + my @refs = $context->get_all_references( $ref_name ); + (@refs) + || die "You must specify a ref for MERGE_RANGE"; + + my $range = Excel::Template::Element::Range->_join_refs(@refs); + + # NOTE: + # we need to copy the current format + # because Spreadsheet::WriteExcel will + # mark any format used in a merged cell + # as being specifically for a merged cell + # and therefore not usable elsewhere. + + my $old_format = $context->active_format; + + my %values; + while ( my ($k, $v) = each %$self ) { + $values{$k} = $context->resolve( $self, $k ); + } + + my $format = $context->format_object->copy( + $context, $old_format, %values, + ); + $context->active_format($format); + + $context->active_worksheet->merge_range( + $range, + $context->get($self, 'TEXT'), + $format, + ); + + $context->active_format($old_format); + + return 1; +} + +1; +__END__ + +=head1 NAME + +Excel::Template::Element::MergeRange - Excel::Template::Element::MergeRange + +=head1 PURPOSE + +To merge a range of cells in a spreadsheet + +=head1 NODE NAME + +MERGE_RANGE + +=head1 INHERITANCE + +L + +=head1 EFFECTS + +This will merge a range of cells. + +=head1 DEPENDENCIES + +None + +=head1 USAGE + + + + + Text to insert into merged range + +=head1 AUTHOR + +Stevan Little (stevan.little@iinteractive.com) + +=head1 SEE ALSO + +Nothing + +=cut diff --git a/lib/Excel/Template/Element/Range.pm b/lib/Excel/Template/Element/Range.pm index d3a84d8..0ffa15a 100755 --- a/lib/Excel/Template/Element/Range.pm +++ b/lib/Excel/Template/Element/Range.pm @@ -23,6 +23,12 @@ sub resolve my @refs = $context->get_all_references( $ref_name ); return '' unless @refs; + return $self->_join_refs(@refs); +} + +sub _join_refs { + my ($self, @refs) = @_; + my ($top, $left, $bottom, $right) = ( $refs[0][0], $refs[0][1] ) x 2; diff --git a/lib/Excel/Template/Factory.pm b/lib/Excel/Template/Factory.pm index ed87510..2c33cbe 100644 --- a/lib/Excel/Template/Factory.pm +++ b/lib/Excel/Template/Factory.pm @@ -16,6 +16,7 @@ my %Manifest = ( 'CELL' => 'Excel::Template::Element::Cell', 'FORMULA' => 'Excel::Template::Element::Formula', 'FREEZEPANES' => 'Excel::Template::Element::FreezePanes', + 'MERGE_RANGE' => 'Excel::Template::Element::MergeRange', 'IMAGE' => 'Excel::Template::Element::Image', 'RANGE' => 'Excel::Template::Element::Range', 'VAR' => 'Excel::Template::Element::Var', @@ -50,7 +51,7 @@ my %isBuildable = map { $_ => ~~1 } qw( WORKBOOK WORKSHEET FORMAT BOLD HIDDEN ITALIC LOCKED OUTLINE SHADOW STRIKEOUT IF ROW LOOP SCOPE KEEP_LEADING_ZEROS - CELL FORMULA FREEZEPANES IMAGE + CELL FORMULA FREEZEPANES IMAGE MERGE_RANGE VAR BACKREF RANGE ); @@ -67,7 +68,7 @@ my %isBuildable = map { $_ => ~~1 } qw( eval { require "$filename.pm"; }; if ($@) { - die "Cannot find or compile PM file for '$class' ($filename)\n"; + die "Cannot find or compile PM file for '$class' ($filename) because $@\n"; } $Loaded{$class} = ~~1; diff --git a/lib/Excel/Template/Format.pm b/lib/Excel/Template/Format.pm index 778c49d..16bfccf 100644 --- a/lib/Excel/Template/Format.pm +++ b/lib/Excel/Template/Format.pm @@ -34,7 +34,7 @@ sub _retrieve_key { $_[0]{ $_[1] } } { my @_boolean_formats = qw( bold italic locked hidden font_outline font_shadow font_strikeout - text_wrap text_justlast shrink + text_wrap text_justlast shrink is_merged ); my @_integer_formats = qw( @@ -47,6 +47,10 @@ sub _retrieve_key { $_[0]{ $_[1] } } bottom_color top_color left_color right_color ); + my @_fake_slots = qw( + is_merged + ); + sub _params_to_key { my %params = @_; @@ -123,6 +127,8 @@ sub _retrieve_key { $_[0]{ $_[1] } } my $format = _retrieve_format($self, $new_key); return $format if $format; + delete $params{$_} for @_fake_slots; + $format = $context->{XLS}->add_format(%params); _assign($self, $new_key, $format); return $format; diff --git a/t/028_merge_range.t b/t/028_merge_range.t new file mode 100644 index 0000000..51f8d89 --- /dev/null +++ b/t/028_merge_range.t @@ -0,0 +1,42 @@ +BEGIN{ $^W = 0 } +use strict; + +use Test::More tests => 4; + +use lib 't'; +use mock; +mock::reset; + +my $CLASS = 'Excel::Template'; +use_ok( $CLASS ); + +my $object = $CLASS->new( + file => \*DATA, +); +isa_ok( $object, $CLASS ); + +ok( $object->write_file( 'filename' ), 'Successfuly wrote file' ); + +my @calls = mock::get_calls; +is( join( $/, @calls, '' ), <<__END_EXPECTED__, 'Calls match up' ); +Spreadsheet::WriteExcel::new( 'filename' ) +Spreadsheet::WriteExcel::add_format( '' ) +Spreadsheet::WriteExcel::add_worksheet( '' ) +Spreadsheet::WriteExcel::Worksheet::new( '' ) +Spreadsheet::WriteExcel::Worksheet::write( '0', '0', '', '1' ) +Spreadsheet::WriteExcel::Worksheet::write( '0', '1', '', '1' ) +Spreadsheet::WriteExcel::add_format( '' ) +Spreadsheet::WriteExcel::Worksheet::merge_range( 'A1:B1', 'This is the Foo Range', '2' ) +Spreadsheet::WriteExcel::close( '' ) +__END_EXPECTED__ + +__DATA__ + + + + + + + + + diff --git a/t/Spreadsheet/WriteExcel/Worksheet.pm b/t/Spreadsheet/WriteExcel/Worksheet.pm index 5c779b4..ee5ecf6 100644 --- a/t/Spreadsheet/WriteExcel/Worksheet.pm +++ b/t/Spreadsheet/WriteExcel/Worksheet.pm @@ -19,7 +19,7 @@ sub new { my @funcs = qw( write_string write_number write_blank write_url write_formula write set_row set_column keep_leading_zeros insert_bitmap freeze_panes - set_landscape set_portrait + set_landscape set_portrait merge_range ); foreach my $func ( @funcs ) {