4dd3e210b95d435acd47e85c9df7d192e7622653
[p5sagit/Excel-Template.git] / lib / Excel / Template / Element / Range.pm
1 package Excel::Template::Element::Range;
2
3 use strict;
4 use Spreadsheet::WriteExcel::Utility;
5
6 BEGIN {
7     use vars qw(@ISA);
8     @ISA = qw(Excel::Template::Element);
9
10     use Excel::Template::Element;
11 }
12
13 sub min { $_[0] < $_[1] ? $_[0] : $_[1] }
14 sub max { $_[0] > $_[1] ? $_[0] : $_[1] }
15
16 sub 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
26     return $self->_join_refs(@refs);
27 }
28
29 sub _join_refs {
30     my ($self, @refs) = @_;
31     
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
50 1;
51 __END__
52
53 =head1 NAME
54
55 Excel::Template::Element::Range - Excel::Template::Element::Range
56
57 =head1 PURPOSE
58
59 Returns a range of cell locations (i.e. B2:C2) that contains all calls using
60 this reference. To return the location of the last cell, use BACKREF.
61
62 =head1 NODE NAME
63
64 RANGE
65
66 =head1 INHERITANCE
67
68 Excel::Template::Element
69
70 =head1 ATTRIBUTES
71
72 =over 4
73
74 =item * REF
75
76 This is the name of the reference to look up.
77
78 =back
79
80 =head1 CHILDREN
81
82 None
83
84 =head1 EFFECTS
85
86 None
87
88 =head1 DEPENDENCIES
89
90 This will only be used within CELL tags.
91
92 =head1 USAGE
93
94 In 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
103 The formula in row 2 would be =SUM(B1:C1).
104
105 =head1 AUTHOR
106
107 Rob Kinyon (rkinyon@columbus.rr.com)
108
109 =head1 SEE ALSO
110
111 CELL, BACKREF
112
113 =cut