File Coverage

lib/Spreadsheet/Write/WriteXLS.pm
Criterion Covered Total %
statement 90 115 78.2
branch 38 68 55.8
condition 5 11 45.4
subroutine 9 10 90.0
pod 4 4 100.0
total 146 208 70.1


line stmt bran cond sub pod time code
1             package Spreadsheet::Write::WriteXLS;
2              
3 1     1   24 use 5.008;
  1         2  
4 1     1   17 use base qw'Spreadsheet::Write';
  1         1  
  1         80  
5              
6 1     1   5 use Spreadsheet::WriteExcel;
  1         1  
  1         883  
7              
8             sub new
9             {
10 2     2 1 6 my ($class, %args) = @_;
11 2         3 my $self = bless {}, $class;
12              
13 2   0     7 my $filename = $args{'file'} || $args{'filename'} || die "Need filename.";
14              
15 2         5 $self->{'_FILENAME'} = $filename;
16 2   50     10 $self->{'_SHEETNAME'} = $args{'sheet'} || '';
17 2   50     13 $self->{'_STYLES'} = $args{'styles'} || {};
18              
19 2         10 return $self;
20             }
21              
22             sub _prepare
23             {
24 8     8   8 my $self = shift;
25 8         10 my $worksheet = $self->{'_WORKSHEET'};
26 8         7 my $workbook = $self->{'_WORKBOOK'};
27              
28 8 100       14 if(!$worksheet)
29             {
30 2         4 $self->{'_FH'}->binmode();
31 2         18 $workbook = Spreadsheet::WriteExcel->new($self->{'_FH'});
32 2         3190 $self->{'_WORKBOOK'} = $workbook;
33 2         11 $worksheet = $workbook->add_worksheet($self->{'_SHEETNAME'});
34 2         775 $self->{'_WORKSHEET'} = $worksheet;
35 2         5 $self->{'_WORKBOOK_ROW'} = 0;
36             }
37              
38 8         17 return $self;
39             }
40              
41             sub freeze (@)
42             {
43 2     2 1 11 my $self=shift;
44 2 50       5 $self->_open() || return undef;
45 2         10 $self->{'_WORKSHEET'}->freeze_panes(@_);
46 2         22 return $self;
47             }
48              
49             sub close
50             {
51 4     4 1 12 my $self=shift;
52              
53 4 100       28 return if $self->{'_CLOSED'};
54              
55             $self->{'_WORKBOOK'}->close
56 2 50       18 if $self->{'_WORKBOOK'};
57              
58             $self->{'_FH'}->close
59 2 50       12791 if $self->{'_FH'};
60              
61 2         175 $self->{'_CLOSED'} = 1;
62 2         7 return $self;
63             }
64              
65             sub _format_cache($$)
66             {
67 20     20   24 my ($self, $format) = @_;
68              
69 20         18 my $cache_key='';
70 20         39 foreach my $key (sort keys %$format)
71             {
72 20         36 $cache_key .= $key . $format->{$key};
73             }
74              
75 20 100       35 if(exists($self->{'_FORMAT_CACHE'}->{$cache_key}))
76             {
77 14         22 return $self->{'_FORMAT_CACHE'}->{$cache_key};
78             }
79              
80 6         17 return $self->{'_FORMAT_CACHE'}->{$cache_key} = $self->{'_WORKBOOK'}->add_format(%$format);
81             }
82              
83             sub addsheet ($$)
84             {
85 0     0 1 0 my ($self,$name)=@_;
86              
87 0 0       0 $self->_open() || return undef;
88              
89 0         0 my $worksheet = $self->{'_WORKBOOK'}->add_worksheet($name);
90 0         0 $self->{'_SHEETNAME'} = $name;
91 0         0 $self->{'_WORKSHEET'} = $worksheet;
92 0         0 $self->{'_WORKBOOK_ROW'} = 0;
93              
94 0         0 return $self;
95             }
96              
97             sub _add_prepared_row
98             {
99 6     6   7 my $self = shift;
100              
101 6         6 my $worksheet = $self->{'_WORKSHEET'};
102 6         7 my $workbook = $self->{'_WORKBOOK'};
103 6         7 my $row = $self->{'_WORKBOOK_ROW'};
104 6         4 my $col = 0;
105              
106 6 50       11 scalar(@_) < 256 ||
107             die "Excel'97 back-end does not support rows 256 elements or longer";
108              
109 6         8 for(my $i=0; $i
110             {
111 24         1750 my %props = %{ $_[$i] };
  24         78  
112 24         32 my $value = $props{'content'};
113              
114 24         26 delete $props{'content'};
115 24         24 my $props = \%props;
116              
117 24         19 my %format;
118 24 100       27 if(%props)
119             {
120 20 100       33 if(my $stylelist = $props->{'style'})
121             {
122 12 50       19 $stylelist=[$stylelist] unless ref $stylelist;
123              
124 12 50       31 foreach my $style (ref $stylelist ? @$stylelist : ($stylelist)) {
125 12         14 my $stprops = $self->{'_STYLES'}->{$style};
126 12 50       21 if(!$stprops) {
127 0         0 warn "Style '$style' is not defined\n";
128             }
129             else {
130 12         12 my %a;
131 12         31 @a{keys %$stprops} = values %$stprops;
132 12         17 @a{keys %$props} = values %$props;
133 12         19 $props=\%a;
134             }
135             }
136             }
137              
138 20 100       31 if ($props->{'font_weight'} eq 'bold')
139             {
140 8         7 $format{'bold'} = 1;
141             }
142 20 100       27 if ($props->{'font_style'} eq 'italic')
143             {
144 8         9 $format{'italic'} = 1;
145             }
146 20 50       42 if ($props->{'font_decoration'} =~ m'underline')
147             {
148 0         0 $format{'underline'} = 1;
149             }
150 20 50       34 if ($props->{'font_decoration'} =~ m'strikeout')
151             {
152 0         0 $format{'font_strikeout'} = 1;
153             }
154 20 50       27 if (defined $props->{'font_color'})
155             {
156 0         0 $format{'color'} = $props->{'font_color'};
157             }
158 20 50       23 if (defined $props->{'bg_color'})
159             {
160 0         0 $format{'bg_color'} = $props->{'bg_color'};
161             }
162 20 50       24 if (defined $props->{'font_face'})
163             {
164 0         0 $format{'font'}=$props->{'font_face'};
165             }
166 20 50       25 if (defined $props->{'font_size'})
167             {
168 0         0 $format{'size'}=$props->{'font_size'};
169             }
170 20 50       23 if (defined $props->{'align'})
171             {
172 0         0 $format{'align'}=$props->{'align'};
173             }
174 20 50       22 if (defined $props->{'valign'})
175             {
176 0         0 $format{'valign'}=$props->{'valign'};
177             }
178 20 100       24 if (defined $props->{'format'})
179             {
180 4         7 $format{'num_format'} = $props->{'format'};
181             }
182 20 50       21 if (defined $props->{'width'})
183             {
184 0         0 $worksheet->set_column($col,$col,$props->{'width'});
185             }
186 20 50 33     34 if (defined $props->{'comment'} && length($props->{'comment'}))
187             {
188 0         0 $worksheet->write_comment($row,$col,$props->{'comment'});
189             }
190             }
191              
192 24         39 my @params = ($row, $col++, $value);
193 24 100       44 push @params, $self->_format_cache(\%format) if keys %format;
194              
195 24   100     375 my $type = lc $props->{'type'} || 'auto';
196 24 100       39 if ($type eq 'auto') { $worksheet->write(@params); }
  12 50       25  
    0          
    0          
    0          
    0          
    0          
197 12         26 elsif ($type eq 'string') { $worksheet->write_string(@params); }
198 0         0 elsif ($type eq 'text') { $worksheet->write_string(@params); }
199 0         0 elsif ($type eq 'number') { $worksheet->write_number(@params); }
200 0         0 elsif ($type eq 'blank') { $worksheet->write_blank(@params); }
201 0         0 elsif ($type eq 'formula') { $worksheet->write_formula(@params); }
202 0         0 elsif ($type eq 'url') { $worksheet->write_url(@params); }
203             else
204             {
205 0         0 warn "Unknown cell type $type";
206 0         0 $worksheet->write(@params);
207             }
208             }
209              
210 6         3761 $self->{'_WORKBOOK_ROW'}++;
211              
212 6         29 return $self;
213             }
214              
215             1;