Commit | Line | Data |
d0eafc11 |
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(@_); |
8fd01531 |
16 | |
6dd4c89d |
17 | $self->{TXTOBJ} = Excel::Template::Factory->_create('TEXTOBJECT'); |
8fd01531 |
18 | |
d0eafc11 |
19 | return $self; |
20 | } |
21 | |
6dd4c89d |
22 | sub _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 |
42 | my %legal_types = ( |
1e753f77 |
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', |
8c63e224 |
49 | ); |
50 | |
d0eafc11 |
51 | sub render |
52 | { |
53 | my $self = shift; |
b6bc5a5d |
54 | my ($context, $method) = @_; |
55 | |
8c63e224 |
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 | |
b6bc5a5d |
75 | $method ||= 'write'; |
d0eafc11 |
76 | |
37513eae |
77 | my ($row, $col) = map { $context->get($self, $_) } qw(ROW COL); |
78 | |
8fd01531 |
79 | my $ref = $context->get( $self, 'REF' ); |
37513eae |
80 | if (defined $ref && length $ref) |
81 | { |
8fd01531 |
82 | $context->add_reference( uc( $ref ), $row, $col ); |
37513eae |
83 | } |
84 | |
dee1f239 |
85 | # Apply the cell width to the current column |
86 | if (my $width = $context->get($self, 'WIDTH')) |
87 | { |
d5382a2d |
88 | $width =~ s/[^\d.]//g; |
dee1f239 |
89 | $width *= 1; |
90 | if ($width > 0) |
91 | { |
92 | $context->active_worksheet->set_column($col, $col, $width); |
93 | } |
8fd01531 |
94 | } |
dee1f239 |
95 | |
b6bc5a5d |
96 | $context->active_worksheet->$method( |
37513eae |
97 | $row, $col, |
6dd4c89d |
98 | $self->_get_text($context), |
d0eafc11 |
99 | $context->active_format, |
100 | ); |
101 | |
997b1ce3 |
102 | my $comment = $context->get($self, 'COMMENT'); |
103 | if (defined $comment && length $comment){ |
104 | $context->active_worksheet->write_comment($row, $col,$comment); |
105 | } |
106 | |
d0eafc11 |
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 | |
8fd01531 |
134 | L<ELEMENT|Excel::Template::Element> |
d0eafc11 |
135 | |
136 | =head1 ATTRIBUTES |
137 | |
138 | =over 4 |
139 | |
140 | =item * TEXT |
141 | |
8fd01531 |
142 | 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. |
d0eafc11 |
143 | |
144 | =item * COL |
145 | |
8fd01531 |
146 | 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. |
d0eafc11 |
147 | |
b6bc5a5d |
148 | =item * REF |
149 | |
8fd01531 |
150 | 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>. |
b6bc5a5d |
151 | |
dee1f239 |
152 | =item * WIDTH |
153 | |
154 | Sets the width of the column the cell is in. The last setting for a given column |
155 | will win out. |
156 | |
8c63e224 |
157 | =item * TYPE |
158 | |
8fd01531 |
159 | 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. |
8c63e224 |
160 | |
8fd01531 |
161 | The legal types (taken from L<Spreadsheet::WriteExcel>) are: |
8c63e224 |
162 | |
d523f4e4 |
163 | =item * COMMENT |
164 | |
165 | Add a comment to the cell |
166 | |
8c63e224 |
167 | =over 4 |
168 | |
169 | =item * blank |
170 | |
171 | =item * formula |
172 | |
173 | =item * number |
174 | |
175 | =item * string |
176 | |
177 | =item * url |
178 | |
1e753f77 |
179 | =item * date_time |
180 | |
8c63e224 |
181 | =back |
182 | |
1e753f77 |
183 | other write_* methods as defined defined L<Spreadsheet::WriteExcel> would be integrated by request |
d0eafc11 |
184 | |
8c63e224 |
185 | =back |
d0eafc11 |
186 | |
187 | =head1 CHILDREN |
188 | |
8fd01531 |
189 | L<FORMULA|Excel::Template::Element::Formula> |
d0eafc11 |
190 | |
191 | =head1 EFFECTS |
192 | |
8fd01531 |
193 | This will consume one column in the current row. |
d0eafc11 |
194 | |
195 | =head1 DEPENDENCIES |
196 | |
197 | None |
198 | |
199 | =head1 USAGE |
200 | |
201 | <cell text="Some Text Here"/> |
202 | <cell>Some other text here</cell> |
203 | |
204 | <cell text="$Param2"/> |
205 | <cell>Some <var name="Param"> text here</cell> |
206 | |
8fd01531 |
207 | 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. |
d0eafc11 |
208 | |
8fd01531 |
209 | Please see L<Spreadsheet::WriteExcel> for what constitutes a legal formula. |
d0eafc11 |
210 | |
d0eafc11 |
211 | =head1 AUTHOR |
212 | |
c09684ff |
213 | Rob Kinyon (rob.kinyon@gmail.com) |
d0eafc11 |
214 | |
215 | =head1 SEE ALSO |
216 | |
8fd01531 |
217 | L<ROW|Excel::Template::Container::Row>, L<VAR|Excel::Template::Element::Var>, L<FORMULA|Excel::Template::Element::Formula> |
d0eafc11 |
218 | |
219 | =cut |