f14428aa42b38349ee61cef8b30ce46075e4dff3
[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 UNI_YES        $txt = Unicode::String::utf8('');
41 UNI_NO         $txt = '';
42     }
43                                                                                 
44     return $txt;
45 }
46
47 sub render
48 {
49     my $self = shift;
50     my ($context, $method) = @_;
51
52     $method ||= 'write';
53
54     my ($row, $col) = map { $context->get($self, $_) } qw(ROW COL);
55
56     my $ref = uc $context->get( $self, 'REF' );
57     if (defined $ref && length $ref)
58     {
59         $context->add_reference( $ref, $row, $col );
60     }
61
62     # Apply the cell width to the current column
63     if (my $width = $context->get($self, 'WIDTH'))
64     {
65         $width =~ s/\D//g;
66         $width *= 1;
67         if ($width > 0)
68         {
69             $context->active_worksheet->set_column($col, $col, $width);
70         }
71     }                                                                         
72
73     $context->active_worksheet->$method(
74         $row, $col,
75         $self->get_text($context),
76         $context->active_format,
77     );
78
79     return 1;
80 }
81
82 sub deltas
83 {
84     return {
85         COL => +1,
86     };
87 }
88
89 1;
90 __END__
91
92 =head1 NAME
93
94 Excel::Template::Element::Cell - Excel::Template::Element::Cell
95
96 =head1 PURPOSE
97
98 To actually write stuff to the worksheet
99
100 =head1 NODE NAME
101
102 CELL
103
104 =head1 INHERITANCE
105
106 Excel::Template::Element
107
108 =head1 ATTRIBUTES
109
110 =over 4
111
112 =item * TEXT
113
114 This is the text to write to the cell. This can either be text or a parameter
115 with a dollar-sign in front of the parameter name.
116
117 =item * COL
118
119 Optionally, you can specify which column you want this cell to be in. It can be
120 either a number (zero-based) or an offset. See Excel::Template for more info on
121 offset-based numbering.
122
123 =item * REF
124
125 Adds the current cell to the a list of cells that can be backreferenced.
126 This is useful when the current cell needs to be referenced by a
127 formula. See BACKREF and RANGE.
128
129 =item * WIDTH
130
131 Sets the width of the column the cell is in. The last setting for a given column
132 will win out.
133
134 =back 4
135
136 There will be more parameters added, as features are added.
137
138 =head1 CHILDREN
139
140 Excel::Template::Element::Formula
141
142 =head1 EFFECTS
143
144 This will consume one column on the current row. 
145
146 =head1 DEPENDENCIES
147
148 None
149
150 =head1 USAGE
151
152   <cell text="Some Text Here"/>
153   <cell>Some other text here</cell>
154
155   <cell text="$Param2"/>
156   <cell>Some <var name="Param"> text here</cell>
157
158 In the above example, four cells are written out. The first two have text hard-
159 coded. The second two have variables. The third and fourth items have another
160 thing that should be noted. If you have text where you want a variable in the
161 middle, you have to use the latter form. Variables within parameters are the
162 entire parameter's value.
163
164 Please see Spreadsheet::WriteExcel for what constitutes a legal formula.
165
166 =head1 AUTHOR
167
168 Rob Kinyon (rob.kinyon@gmail.com)
169
170 =head1 SEE ALSO
171
172 ROW, VAR, FORMULA
173
174 =cut