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