Merge current-dev 0.30 back to trunk
[p5sagit/Excel-Template.git] / lib / Excel / Template / Element / Cell.pm
1 package Excel::Template::Element::Cell;
2
3 use strict;
4
5 BEGIN {
6     use vars qw(@ISA);
7     @ISA = qw(Excel::Template::Element);
8
9     use Excel::Template::Element;
10 }
11
12 sub new
13 {
14     my $class = shift;
15     my $self = $class->SUPER::new(@_);
16
17     $self->{TXTOBJ} = Excel::Template::Factory->_create('TEXTOBJECT');
18
19     return $self;
20 }
21
22 sub _get_text
23 {
24     my $self = shift;
25     my ($context) = @_;
26
27     my $txt = $context->get($self, 'TEXT');
28     if (defined $txt)
29     {
30         my $txt_obj = Excel::Template::Factory->_create('TEXTOBJECT');
31         push @{$txt_obj->{STACK}}, $txt;
32         $txt = $txt_obj->resolve($context);
33     }
34     else
35     {
36         $txt = $self->{TXTOBJ}->resolve($context)
37     }
38
39     return $txt;
40 }
41
42 my %legal_types = (
43     'blank'     => 'write_blank',
44     'formula'   => 'write_formula',
45     'number'    => 'write_number',
46     'string'    => 'write_string',
47     'url'       => 'write_url',
48     'date_time' => 'write_date_time',
49 );
50
51 sub render
52 {
53     my $self = shift;
54     my ($context, $method) = @_;
55
56     unless ( $method )
57     {
58         my $type = $context->get( $self, 'TYPE' );
59         if ( defined $type )
60         {
61             my $type = lc $type;
62
63             if ( exists $legal_types{ $type } )
64             {
65                 $method = $legal_types{ $type };
66             }
67             else
68             {
69                 warn "'$type' is not a legal cell type.\n"
70                     if $^W;
71             }
72         }
73     }
74
75     $method ||= 'write';
76
77     my ($row, $col) = map { $context->get($self, $_) } qw(ROW COL);
78
79     my $ref = $context->get( $self, 'REF' );
80     if (defined $ref && length $ref)
81     {
82         $context->add_reference( uc( $ref ), $row, $col );
83     }
84
85     # Apply the cell width to the current column
86     if (my $width = $context->get($self, 'WIDTH'))
87     {
88         $width =~ s/[^\d.]//g;
89         $width *= 1;
90         if ($width > 0)
91         {
92             $context->active_worksheet->set_column($col, $col, $width);
93         }
94     }
95
96     $context->active_worksheet->$method(
97         $row, $col,
98         $self->_get_text($context),
99         $context->active_format,
100     );
101
102     return 1;
103 }
104
105 sub deltas
106 {
107     return {
108         COL => +1,
109     };
110 }
111
112 1;
113 __END__
114
115 =head1 NAME
116
117 Excel::Template::Element::Cell - Excel::Template::Element::Cell
118
119 =head1 PURPOSE
120
121 To actually write stuff to the worksheet
122
123 =head1 NODE NAME
124
125 CELL
126
127 =head1 INHERITANCE
128
129 L<ELEMENT|Excel::Template::Element>
130
131 =head1 ATTRIBUTES
132
133 =over 4
134
135 =item * TEXT
136
137 This 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.
138
139 =item * COL
140
141 Optionally, 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.
142
143 =item * REF
144
145 Adds 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>.
146
147 =item * WIDTH
148
149 Sets the width of the column the cell is in. The last setting for a given column
150 will win out.
151
152 =item * TYPE
153
154 This 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.
155
156 The legal types (taken from L<Spreadsheet::WriteExcel>) are:
157
158 =over 4
159
160 =item * blank
161
162 =item * formula
163
164 =item * number
165
166 =item * string
167
168 =item * url
169
170 =item * date_time
171
172 =back
173
174 other write_* methods as defined defined L<Spreadsheet::WriteExcel> would be integrated by request
175
176 =back
177
178 =head1 CHILDREN
179
180 L<FORMULA|Excel::Template::Element::Formula>
181
182 =head1 EFFECTS
183
184 This will consume one column in the current row.
185
186 =head1 DEPENDENCIES
187
188 None
189
190 =head1 USAGE
191
192   <cell text="Some Text Here"/>
193   <cell>Some other text here</cell>
194
195   <cell text="$Param2"/>
196   <cell>Some <var name="Param"> text here</cell>
197
198 In 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.
199
200 Please see L<Spreadsheet::WriteExcel> for what constitutes a legal formula.
201
202 =head1 AUTHOR
203
204 Rob Kinyon (rob.kinyon@gmail.com)
205
206 =head1 SEE ALSO
207
208 L<ROW|Excel::Template::Container::Row>, L<VAR|Excel::Template::Element::Var>, L<FORMULA|Excel::Template::Element::Formula>
209
210 =cut