File Coverage

blib/lib/IO/StructuredOutput/Sheets.pm
Criterion Covered Total %
statement 79 99 79.8
branch 27 54 50.0
condition 5 9 55.5
subroutine 11 13 84.6
pod 0 8 0.0
total 122 183 66.6


\n" . $row . "\n";
line stmt bran cond sub pod time code
1             package IO::StructuredOutput::Sheets;
2              
3 1     1   30 use 5.00503;
  1         3  
  1         31  
4 1     1   5 use strict;
  1         1  
  1         30  
5              
6             require Exporter;
7 1     1   4 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
  1         1  
  1         67  
8             @ISA = qw(Exporter);
9              
10 1     1   5 use Carp qw(croak);
  1         1  
  1         51  
11 1     1   1081 use Text::CSV_XS;
  1         21864  
  1         1618  
12              
13             # Items to export into callers namespace by default. Note: do not export
14             # names by default without a very good reason. Use EXPORT_OK instead.
15             # Do not simply export all your public functions/methods/constants.
16              
17             # This allows declaration use test1 ':all';
18             # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
19             # will save memory.
20             %EXPORT_TAGS = ( 'all' => [ qw(
21            
22             ) ] );
23              
24             @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
25              
26             @EXPORT = qw(
27            
28             );
29              
30             $VERSION = sprintf "%d.%03d", q$Revision: 1.8 $ =~ /(\d+)/g;
31              
32             # Preloaded methods go here.
33              
34             sub addsheet
35             {
36 6     6 0 23 my $proto = shift;
37 6   33     25 my $class = ref($proto) || $proto;
38 6         13 my $attr = shift;
39 6   50     17 my $name = $attr->{name} || 'Sheet';
40 6   50     16 my $format = $attr->{format} || 'html';
41 6         9 my $default_style = $attr->{style};
42 6   100     26 my $wb = $attr->{wb} || "";
43              
44 6 100       28 if ($format eq 'csv')
    100          
    50          
45             {
46 2         14 my $csv = Text::CSV_XS->new();
47 2         198 my $newsheet = { ws => "", name => $name, format => $format, style => $default_style, csv => $csv };
48 2         5 bless $newsheet, $class;
49 2         8 return $newsheet;
50             } elsif ($format eq 'html') {
51             # we'll be encapsulating the output w/ &&
when
52             # output is requested, so no need to do anything here
53 2         10 my $newsheet = { ws => "", name => $name, format => $format, style => $default_style };
54 2         7 bless $newsheet, $class;
55 2         7 return $newsheet;
56             } elsif ($format eq 'xls') {
57 2         16 my $ws = $wb->add_worksheet($name);
58 2         1012 my $newsheet = { ws => $ws, name => $name, format => $format, style => $default_style };
59 2         7 bless $newsheet, $class;
60 2         8 return $newsheet;
61             } else {
62 0         0 croak "invalid or missing format";
63             }
64             }
65              
66             sub name
67             {
68 10 50   10 0 2296 ref(my $self = shift) or croak "instance variable needed";
69             # can't change name once set (limit of excel module)
70             # croak "instance isn't a sheet object" unless ($self->isa("Sheet"));
71 10         67 return $self->{name};
72             }
73              
74             sub sheet
75             {
76 4 50   4 0 13 ref(my $self = shift) or croak "instance variable needed";
77 4         14 return $self->{ws};
78             }
79              
80             sub addrow
81             {
82 6 50   6 0 24 ref(my $self = shift) or croak "instance variable needed";
83              
84 6 50       26 return unless (ref(@_[0]) eq 'ARRAY'); # need to pass in some data to add
85 6         10 my $data = shift;
86 6         7 my $styles = shift;
87 6 50       21 my $style = $styles ? $styles : $self->{style};
88              
89 6         20 my $format = $self->format(); # cut down on method calls
90 6 100       28 if ($format eq 'csv')
    100          
    50          
91             {
92 2         3 my @row;
93 2         4 for (my $i = 0; $i < @{$data}; $i++)
  8         20  
94             {
95 6         16 my $column = $data->[$i];
96             # my $thisstyle = ref($style) ? $style->[$i] : $style;
97             # $thisstyle = $self->{style} unless ($thisstyle);
98            
99             # column may also be an array ref, indicating data spanning
100             # multiple columns. csv doesn't support that, but we'll
101             # handle it anyway.
102 6 50       16 if (ref($column) eq 'ARRAY')
    50          
103             {
104             # push(@row, shift(@{$column}) );
105 0         0 push(@row,@{$column});
  0         0  
106             } elsif (ref($column)) {
107             # skip. Hash and subroutine referances not supported
108             } else {
109 6         14 push(@row,$column);
110             }
111             }
112 2         10 $self->{csv}->combine(@row);
113 2         81 $self->{ws} .= $self->{csv}->string() . "\n";
114 2         23 $self->{rowcount}++;
115             } elsif ($format eq 'html') {
116 2         4 my $row;
117 2         3 for (my $i = 0; $i < @{$data}; $i++)
  8         28  
118             {
119 6         8 my $column = $data->[$i];
120 6 50       16 my $thisstyle = (ref($style) eq 'ARRAY') ? $style->[$i] : $style;
121 6 50       15 $thisstyle = $self->{style} unless ($thisstyle);
122             # column may also be an array ref, indicating data spanning
123             # multiple columns.
124 6 50       24 if (ref($column) eq 'ARRAY')
    50          
125             {
126 0         0 $row .= $thisstyle->output_style($column->[0], scalar(@{$column}) );
  0         0  
127             } elsif (ref($column)) {
128             # skip. Hash and subroutine referances not supported
129             } else {
130 6         23 $row .= $thisstyle->output_style($column);
131             }
132             }
133 2         8 $self->{ws} .= "
134 2         8 $self->{rowcount}++;
135             } elsif ($format eq 'xls') {
136 2         4 $self->{rowcount}++;
137 2         5 my $row = ($self->{rowcount} - 1);
138 2         4 my $col = 0;
139 2         5 for (my $i = 0; $i < @{$data}; $i++)
  8         30  
140             {
141 6         10 my $column = $data->[$i];
142 6 50       17 my $thisstyle = (ref($style) eq 'ARRAY') ? $style->[$i] : $style;
143 6 50       15 $thisstyle = $self->{style} unless ($thisstyle);
144             # column may also be an array ref, indicating data spanning
145             # multiple columns.
146 6 50       17 if (ref($column) eq 'ARRAY')
    50          
147             {
148 0         0 my $span = scalar(@{$column});
  0         0  
149 0         0 $self->{ws}->merge_range($row,$col,$row,($col + $span - 1),$column->[0], $thisstyle->output_style() );
150 0         0 $col += $span;
151             } elsif (ref($column)) {
152             # skip. Hash and subroutine referances not supported
153             } else {
154 6         27 $self->{ws}->write($row,$col,$column,$thisstyle->output_style() );
155 6         689 $col++;
156             }
157             }
158             } else {
159 0         0 croak "invalid or missing format";
160             }
161             }
162              
163             sub setwidth
164             {
165 0 0   0 0 0 ref(my $self = shift) or croak "instance variable needed";
166 0 0       0 if ($self->format() eq 'xls')
167             { # setting width of a column currently only supported in xls
168 0         0 my $first_column = shift;
169 0         0 my $second_column = shift;
170 0         0 my $width = shift;
171 0         0 $self->{ws}->set_column($first_column,$second_column,$width);
172             }
173             }
174              
175             sub freeze_panes
176             {
177 0 0   0 0 0 ref(my $self = shift) or croak "instance variable needed";
178 0 0       0 if ($self->format() eq 'xls')
179             { # this thing will never be supported in html or csv
180             # but I needed it for some excel output.
181             # see Spreadsheet::WriteExcel for docs
182 0         0 my @args = @_;
183 0         0 $self->{ws}->freeze_panes(@_);
184             }
185             }
186              
187             sub rowcount
188             {
189 6 50   6 0 22 ref(my $self = shift) or croak "instance variable needed";
190 6         39 return $self->{rowcount};
191             }
192              
193             sub format
194             {
195 6 50   6 0 20 ref(my $self = shift) or croak "instance variable needed";
196 6         16 return $self->{format};
197             }
198              
199             1;
200             __END__