Intermediate commit - just have to add/fix POD for two classes, then done
[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     elsif ($self->{TXTOBJ})
35     {
36         $txt = $self->{TXTOBJ}->resolve($context)
37     }
38     else
39     {
40         $txt = $context->use_unicode
41             ? Unicode::String::utf8('')
42             : '';
43     }
44                                                                                 
45     return $txt;
46 }
47
48 my %legal_types = (
49     'blank'   => 'write_blank',
50     'formula' => 'write_formula',
51     'number'  => 'write_number',
52     'string'  => 'write_string',
53     'url'     => 'write_url',
54 );
55
56 sub render
57 {
58     my $self = shift;
59     my ($context, $method) = @_;
60
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
80     $method ||= 'write';
81
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
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
101     $context->active_worksheet->$method(
102         $row, $col,
103         $self->_get_text($context),
104         $context->active_format,
105     );
106
107     return 1;
108 }
109
110 sub deltas
111 {
112     return {
113         COL => +1,
114     };
115 }
116
117 1;
118 __END__
119
120 =head1 NAME
121
122 Excel::Template::Element::Cell - Excel::Template::Element::Cell
123
124 =head1 PURPOSE
125
126 To actually write stuff to the worksheet
127
128 =head1 NODE NAME
129
130 CELL
131
132 =head1 INHERITANCE
133
134 Excel::Template::Element
135
136 =head1 ATTRIBUTES
137
138 =over 4
139
140 =item * TEXT
141
142 This is the text to write to the cell. This can either be text or a parameter
143 with a dollar-sign in front of the parameter name.
144
145 =item * COL
146
147 Optionally, you can specify which column you want this cell to be in. It can be
148 either a number (zero-based) or an offset. See Excel::Template for more info on
149 offset-based numbering.
150
151 =item * REF
152
153 Adds the current cell to the a list of cells that can be backreferenced.
154 This is useful when the current cell needs to be referenced by a
155 formula. See BACKREF and RANGE.
156
157 =item * WIDTH
158
159 Sets the width of the column the cell is in. The last setting for a given column
160 will win out.
161
162 =item * TYPE
163
164 This allows you to specify what write_*() method will be used. The default is to
165 call write() and let S::WE make the right call. However, you may wish to
166 override it. Excel::Template will not do any form of validation on what you
167 provide. You are assumed to know what you're doing.
168
169 The 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
185 q.v. L<Spreadsheet::WriteExcel> for more info.
186
187 =back
188
189 =head1 CHILDREN
190
191 Excel::Template::Element::Formula
192
193 =head1 EFFECTS
194
195 This will consume one column on the current row. 
196
197 =head1 DEPENDENCIES
198
199 None
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
209 In the above example, four cells are written out. The first two have text hard-
210 coded. The second two have variables. The third and fourth items have another
211 thing that should be noted. If you have text where you want a variable in the
212 middle, you have to use the latter form. Variables within parameters are the
213 entire parameter's value.
214
215 Please see Spreadsheet::WriteExcel for what constitutes a legal formula.
216
217 =head1 AUTHOR
218
219 Rob Kinyon (rob.kinyon@gmail.com)
220
221 =head1 SEE ALSO
222
223 ROW, VAR, FORMULA
224
225 =cut