Added merge_range as submitted by Stevan Little
[p5sagit/Excel-Template.git] / lib / Excel / Template / Element / Range.pm
CommitLineData
37513eae 1package Excel::Template::Element::Range;
2
3use strict;
4use Spreadsheet::WriteExcel::Utility;
5
6BEGIN {
7 use vars qw(@ISA);
8 @ISA = qw(Excel::Template::Element);
9
10 use Excel::Template::Element;
11}
12
13sub min { $_[0] < $_[1] ? $_[0] : $_[1] }
14sub max { $_[0] > $_[1] ? $_[0] : $_[1] }
15
16sub resolve
17{
18 my $self = shift;
19 my ($context) = @_;
20
21 my $ref_name = $context->resolve($self, 'REF');
22
23 my @refs = $context->get_all_references( $ref_name );
24 return '' unless @refs;
25
ddb9abcf 26 return $self->_join_refs(@refs);
27}
28
29sub _join_refs {
30 my ($self, @refs) = @_;
31
37513eae 32 my ($top, $left, $bottom, $right) =
33 ( $refs[0][0], $refs[0][1] ) x 2;
34
35 shift @refs;
36 foreach my $ref ( @refs )
37 {
38 $top = min( $top, $ref->[0]);
39 $bottom = max( $bottom, $ref->[0]);
40 $left = min( $left, $ref->[1]);
41 $right = max( $right, $ref->[1]);
42 }
43
44 return join( ':',
45 xl_rowcol_to_cell($top, $left),
46 xl_rowcol_to_cell($bottom, $right)
47 );
48}
49
501;
51__END__
52
53=head1 NAME
54
55Excel::Template::Element::Range
56
57=head1 PURPOSE
58
b6bc5a5d 59Returns a range of cell locations (i.e. B2:C2) that contains all calls using
60this reference. To return the location of the last cell, use BACKREF.
37513eae 61
62=head1 NODE NAME
63
b6bc5a5d 64RANGE
37513eae 65
66=head1 INHERITANCE
67
68Excel::Template::Element
69
70=head1 ATTRIBUTES
71
72=over 4
73
74=item * REF
75
76This is the name of the reference to look up.
77
6dd4c89d 78=back
37513eae 79
80=head1 CHILDREN
81
82None
83
84=head1 EFFECTS
85
86None
87
88=head1 DEPENDENCIES
89
90This will only be used within CELL tags.
91
92=head1 USAGE
93
94In the example...
95
96 <row>
97 <cell ref="this_cell"/><cell ref="that_cell"><cell ref="that_cell">
98 </row>
99 <row>
100 <formula>=SUM(<range ref="that_cell">)</formula>
101 </row>
102
103The formula in row 2 would be =SUM(B1:C1).
104
105=head1 AUTHOR
106
107Rob Kinyon (rkinyon@columbus.rr.com)
108
109=head1 SEE ALSO
110
b6bc5a5d 111CELL, BACKREF
37513eae 112
113=cut