blib/lib/Spreadsheet/WriteExcel/Simple/Tabs.pm

filter: Statementstmt 91.4% 85 / 93 Branchbran 71.4% 20 / 28 Conditioncond 40.0% 2 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 82.3% 14 / 17 Podpod 100% 8 / 8 totaltotal 83.7% 129 / 154 CC 18 SCAR 10.5
CC
18
Cov
85%
first
4.1
default
4.1
add
4.0
CRAP 2.8
↑ Summary
1 package Spreadsheet::WriteExcel::Simple::Tabs;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use IO::Scalar qw{};
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Spreadsheet::WriteExcel qw{};
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 our $VERSION='0.11';
8 1 our $PACKAGE=__PACKAGE__;
9  
10 =head1 NAME
11  
12 Spreadsheet::WriteExcel::Simple::Tabs - Simple Interface to the Spreadsheet::WriteExcel Package
13  
14 =head1 SYNOPSIS
15  
16 use Spreadsheet::WriteExcel::Simple::Tabs;
17 my $ss=Spreadsheet::WriteExcel::Simple::Tabs->new;
18 my @data=(
19 ["Heading1", "Heading2"],
20 ["data1", "data2" ],
21 ["data3", "data4" ],
22 );
23 $ss->add(Tab1=>\@data, Tab2=>\@data);
24 print $ss->header(filename=>"filename.xls"), $ss->content;
25  
26 =head1 DESCRIPTION
27  
28 This is a simple wrapper around Spreadsheet::WriteExcel that creates tabs for data. It is meant to be simple not full featured. I use this package to export data from the L<DBIx::Array> sqlarrayarrayname method which is an array of array references where the first array is the column headings.
29  
30 =head1 CONSTRUCTOR
31  
32 =head2 new
33  
34 =cut
35  
36 1 ▶sub new {
Subroutine new: called CC=2 SCAR=7.1
Pod documented
37 1 my $this = shift();
38 1 ▶ my $class = ref($this) || $this;
Condition33%1/3
ref $this || $this
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $this || $this
ref $this $this
Truth table33%1/3
A || B
A= ref $thisB= $this
ABresult
1X1
011
000
39 1 my $self = {};
40 1 bless $self, $class;
41 1 $self->initialize(@_);
42 1 return $self;
43 }
44  
45 =head2 initialize
46  
47 =cut
48  
49 1 ▶sub initialize {
Subroutine initialize: called CC=1 SCAR=0.0
Pod documented
50 1 my $self=shift;
51 1 %$self=@_;
52 }
53  
54 =head2 book
55  
56 Returns the workbook object
57  
58 =cut
59  
60 20 ▶sub book {
Subroutine book: called CC=2 SCAR=6.9
Pod documented
61 20 my $self=shift;
62 #Thanks to Tony Bowden for the IO::Scalar stuff
63 20 ▶ unless (defined($self->{"book"})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"book"}) 1 19 20
64 $self->{"book"}=Spreadsheet::WriteExcel->new(
65 1 IO::Scalar->new_tie(\($self->{"content"}))
66 );
67 }
68 20 return $self->{"book"};
69 }
70  
71 =head2 add
72  
73 $ss->add("Tab Name", \@data);
74 $ss->add(Tab1=>\@data, Tab2=>\@data);
75  
76 =cut
77  
78 1 ▶sub add {
Subroutine add: called CC=4 SCAR=14.0
Pod documented
79 1 my $self=shift;
80 1 ▶ die("Error: The $PACKAGE->add method requires an even number of arguments")
Branch: 1 branch
BranchTrueFalseTotal
if scalar(@_) % 2 0 1 1
81 if scalar(@_) % 2;
82 1 ▶ while (@_ > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 0) 4 1 5
83 4 my $tab=shift;
84 4 my $data=shift;
85 4 ▶ die(sprintf(qq{Error: Expecting data to be an array reference but got "%s" in $PACKAGE->add}, ref($data)))
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data eq "ARRAY" 0 4 4
86 unless ref($data) eq "ARRAY";
87 4 $self->_add1($tab=>$data);
88 }
89 1 return $self;
90 }
91  
92 4 ▶sub _add1 {
Subroutine _add1: called CC=2 SCAR=7.7
93 4 my $self=shift;
94 4 my $tab=shift;
95 4 $tab=~s/[\[\]:\*\?\/\\]/ /g; #Invalid character []:*?/\ in worksheet name
96 4 ▶ $tab=substr($tab,0,31) if length($tab) > 31; #must be <= 31 chars
Branch: 1 branch
BranchTrueFalseTotal
if length $tab > 31 0 4 4
97 4 my $data=shift;
98 4 my $sheet=$self->book->add_worksheet($tab);
99 4 my %format=$self->default; $format{"num_format"}='mm/dd/yyyy hh:mm:ss';
100 4 my $format_datetime=$self->book->add_format(%format);
101 0 ▶ my $subref=sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
102 0 my $sheet=shift;
103 0 my @args=@_;
104 0 my ($m,$d,$y,$h,$n,$s)=split(/[\/ :]/, $args[2]);
105 0 $args[2]=sprintf("%4d-%02d-%02dT%02d:%02d:%02d", $y, $m, $d, $h, $n, $s);
106 0 $args[3]=$format_datetime;
107 0 return $sheet->write_date_time(@args);
108 4 };
109 0 ▶ $sheet->add_write_handler(qr/^\d{16,}$/, sub{shift->write_string(@_)}); #Long Integer Support - RT61869
Subroutine __ANON__: not called CC=1 SCAR=1.2
110 0 ▶ $sheet->add_write_handler(qr/^0\d+$/, sub{shift->write_string(@_)}); #Leading Zero Support
Subroutine __ANON__: not called CC=1 SCAR=1.2
111 4 $sheet->add_write_handler(qr{^\d{2}/\d{2}/\d{4} \d{2}:\d{2}:\d{2}$}, $subref); #DateTime Support
112 4 $self->_add_data($sheet, $data);
113 4 $sheet->freeze_panes(1, 0);
114 4 return $sheet;
115 }
116  
117 4 ▶sub _add_data {
Subroutine _add_data: called CC=4 SCAR=13.9
118 4 my $self=shift;
119 4 my $worksheet=shift;
120 4 my $data=shift;
121 4 my $header=shift(@$data);
122 4 $worksheet->write_col(0,0,[$header], $self->book->add_format($self->default, $self->first));
123 4 $worksheet->write_col(1,0, $data, $self->book->add_format($self->default));
124  
125 4 unshift @$data, $header; #put the data back together it is a reference!
126  
127 #Auto resize columns
128 4 foreach my $col (0 .. scalar(@$header) - 1) {
129 7 ▶ my $width=(sort {$a<=>$b} map {length($_->[$col]||'')} @$data)[-1];
Condition50%1/2
$_->[$col] || ''
l!l
210
MC/DC0%0/1
$_->[$col] || ''
$_->[$col]
Truth table50%1/2
A
A= $_->[$col]
Aresult
11
00
130 7 ▶ $width = 8 if $width < 8;
Branch: 1 branch
BranchTrueFalseTotal
if $width < 8 7 0 7
131 7 $worksheet->set_column($col, $col, $width);
132 }
133 4 return $self;
134 }
135  
136 =head2 header
137  
138 Returns a header appropriate for a web application
139  
140 Content-type: application/vnd.ms-excel
141 Content-Disposition: attachment; filename=filename.xls
142  
143 $ss->header #embedded in browser
144 $ss->header(filename=>"filename.xls") #download prompt
145 $ss->header(content_type=>"application/vnd.ms-excel") #default content type
146  
147 =cut
148  
149 4 ▶sub header {
Subroutine header: called CC=3 SCAR=11.0
Pod documented
150 4 my $self=shift;
151 4 my %data=@_;
152 $data{"content_type"}="application/vnd.ms-excel"
153 4 ▶ unless defined $data{"content_type"};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data{"content_type"} 2 2 4
154 4 my $header=sprintf("Content-type: %s\n", $data{"content_type"});
155 $header.=sprintf(qq{Content-Disposition: attachment; filename="%s";\n},
156 4 ▶ $data{"filename"}) if defined $data{"filename"};
Branch: 1 branch
BranchTrueFalseTotal
if defined $data{"filename"} 2 2 4
157 4 $header.="\n";
158 4 return $header;
159 }
160  
161 =head2 content
162  
163 This returns the binary content of the spreadsheet.
164  
165 print $ss->content;
166  
167 print $ss->header, $ss->content; #CGI Application
168  
169 binmod($fh);
170 print $fh, $ss->content;
171  
172 =cut
173  
174 1 ▶sub content {
Subroutine content: called CC=1 SCAR=0.0
Pod documented
175 1 my $self=shift;
176 1 $self->book->close;
177 1 return $self->{"content"};
178 }
179  
180 =head1 PROPERTIES
181  
182 =head2 first
183  
184 Returns a hash of additional settings for the first row
185  
186 $ss->first({setting=>"value"}); #settings from L<Spreadsheet::WriteExcel>
187  
188 =cut
189  
190 4 ▶sub first {
Subroutine first: called CC=4 SCAR=14.1
Pod documented
191 4 my $self=shift;
192 4 ▶ $self->{"first"}=shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 4 4
193 $self->{"first"}={bg_color=>"silver", bold=>1}
194 4 ▶ unless ref($self->{"first"}) eq "HASH";
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self->{"first"} eq "HASH" 1 3 4
195 4 ▶ return wantarray ? %{$self->{"first"}} : $self->{"first"};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 4 0 4
196 }
197  
198 =head2 default
199  
200 Returns a hash of default settings for the body
201  
202 $ss->default({setting=>"value"}); #settings from L<Spreadsheet::WriteExcel>
203  
204 =cut
205  
206 12 ▶sub default {
Subroutine default: called CC=4 SCAR=14.1
Pod documented
207 12 my $self=shift;
208 12 ▶ $self->{"default"}=shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 12 12
209 $self->{"default"}={border=>1, border_color=>"gray"}
210 12 ▶ unless ref($self->{"default"}) eq "HASH";
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self->{"default"} eq "HASH" 1 11 12
211 12 ▶ return wantarray ? %{$self->{"default"}} : $self->{"default"};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 12 0 12
212 }
213  
214 =head1 AUTHOR
215  
216 Michael R. Davis
217  
218 =head1 COPYRIGHT
219  
220 Copyright (c) 2009 Michael R. Davis
221 Copyright (c) 2001-2005 Tony Bowden (IO::Scalar portion used here "under the same terms as Perl itself")
222  
223 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
224  
225 The full text of the license can be found in the LICENSE file included with this module.
226  
227 =head1 SEE ALSO
228  
229 L<Spreadsheet::WriteExcel::Simple>, L<DBIx::Array> sqlarrayarrayname method, L<IO::Scalar>, L<Spreadsheet::WriteExcel>
230  
231 =cut
232  
233 1 1;
↑ Summary