Intermediate commit - just have to add/fix POD for two classes, then done
[p5sagit/Excel-Template.git] / lib / Excel / Template / Element / Cell.pm
CommitLineData
d0eafc11 1package Excel::Template::Element::Cell;
2
3use strict;
4
5BEGIN {
6 use vars qw(@ISA);
7 @ISA = qw(Excel::Template::Element);
8
9 use Excel::Template::Element;
10}
11
12sub new
13{
14 my $class = shift;
15 my $self = $class->SUPER::new(@_);
16
6dd4c89d 17 $self->{TXTOBJ} = Excel::Template::Factory->_create('TEXTOBJECT');
d0eafc11 18
19 return $self;
20}
21
6dd4c89d 22sub _get_text
d0eafc11 23{
24 my $self = shift;
25 my ($context) = @_;
26
27 my $txt = $context->get($self, 'TEXT');
28 if (defined $txt)
29 {
6dd4c89d 30 my $txt_obj = Excel::Template::Factory->_create('TEXTOBJECT');
d0eafc11 31 push @{$txt_obj->{STACK}}, $txt;
32 $txt = $txt_obj->resolve($context);
33 }
34 elsif ($self->{TXTOBJ})
35 {
36 $txt = $self->{TXTOBJ}->resolve($context)
37 }
38 else
39 {
8c63e224 40 $txt = $context->use_unicode
41 ? Unicode::String::utf8('')
42 : '';
d0eafc11 43 }
44
45 return $txt;
46}
47
8c63e224 48my %legal_types = (
49 'blank' => 'write_blank',
50 'formula' => 'write_formula',
51 'number' => 'write_number',
52 'string' => 'write_string',
53 'url' => 'write_url',
54);
55
d0eafc11 56sub render
57{
58 my $self = shift;
b6bc5a5d 59 my ($context, $method) = @_;
60
8c63e224 61 unless ( $method )
62 {
63 my $type = $context->get( $self, 'TYPE' );
64 if ( defined $type )
65 {
66 my $type = lc $type;
67
68 if ( exists $legal_types{ $type } )
69 {
70 $method = $legal_types{ $type };
71 }
72 else
73 {
74 warn "'$type' is not a legal cell type.\n"
75 if $^W;
76 }
77 }
78 }
79
b6bc5a5d 80 $method ||= 'write';
d0eafc11 81
37513eae 82 my ($row, $col) = map { $context->get($self, $_) } qw(ROW COL);
83
84 my $ref = uc $context->get( $self, 'REF' );
85 if (defined $ref && length $ref)
86 {
87 $context->add_reference( $ref, $row, $col );
88 }
89
dee1f239 90 # Apply the cell width to the current column
91 if (my $width = $context->get($self, 'WIDTH'))
92 {
93 $width =~ s/\D//g;
94 $width *= 1;
95 if ($width > 0)
96 {
97 $context->active_worksheet->set_column($col, $col, $width);
98 }
99 }
100
b6bc5a5d 101 $context->active_worksheet->$method(
37513eae 102 $row, $col,
6dd4c89d 103 $self->_get_text($context),
d0eafc11 104 $context->active_format,
105 );
106
107 return 1;
108}
109
110sub deltas
111{
112 return {
113 COL => +1,
114 };
115}
116
1171;
118__END__
119
120=head1 NAME
121
122Excel::Template::Element::Cell - Excel::Template::Element::Cell
123
124=head1 PURPOSE
125
126To actually write stuff to the worksheet
127
128=head1 NODE NAME
129
130CELL
131
132=head1 INHERITANCE
133
134Excel::Template::Element
135
136=head1 ATTRIBUTES
137
138=over 4
139
140=item * TEXT
141
142This is the text to write to the cell. This can either be text or a parameter
143with a dollar-sign in front of the parameter name.
144
145=item * COL
146
147Optionally, you can specify which column you want this cell to be in. It can be
148either a number (zero-based) or an offset. See Excel::Template for more info on
149offset-based numbering.
150
b6bc5a5d 151=item * REF
152
153Adds the current cell to the a list of cells that can be backreferenced.
154This is useful when the current cell needs to be referenced by a
155formula. See BACKREF and RANGE.
156
dee1f239 157=item * WIDTH
158
159Sets the width of the column the cell is in. The last setting for a given column
160will win out.
161
8c63e224 162=item * TYPE
163
164This allows you to specify what write_*() method will be used. The default is to
165call write() and let S::WE make the right call. However, you may wish to
166override it. Excel::Template will not do any form of validation on what you
167provide. You are assumed to know what you're doing.
168
169The legal types are:
170
171=over 4
172
173=item * blank
174
175=item * formula
176
177=item * number
178
179=item * string
180
181=item * url
182
183=back
184
185q.v. L<Spreadsheet::WriteExcel> for more info.
d0eafc11 186
8c63e224 187=back
d0eafc11 188
189=head1 CHILDREN
190
191Excel::Template::Element::Formula
192
193=head1 EFFECTS
194
195This will consume one column on the current row.
196
197=head1 DEPENDENCIES
198
199None
200
201=head1 USAGE
202
203 <cell text="Some Text Here"/>
204 <cell>Some other text here</cell>
205
206 <cell text="$Param2"/>
207 <cell>Some <var name="Param"> text here</cell>
208
209In the above example, four cells are written out. The first two have text hard-
210coded. The second two have variables. The third and fourth items have another
211thing that should be noted. If you have text where you want a variable in the
212middle, you have to use the latter form. Variables within parameters are the
213entire parameter's value.
214
215Please see Spreadsheet::WriteExcel for what constitutes a legal formula.
216
d0eafc11 217=head1 AUTHOR
218
c09684ff 219Rob Kinyon (rob.kinyon@gmail.com)
d0eafc11 220
221=head1 SEE ALSO
222
223ROW, VAR, FORMULA
224
225=cut