File Coverage

blib/lib/Spreadsheet/WriteExcel/Simple.pm
Criterion Covered Total %
statement 9 40 22.5
branch 0 6 0.0
condition 0 2 0.0
subroutine 3 11 27.2
pod 7 7 100.0
total 19 66 28.7


line stmt bran cond sub pod time code
1             package Spreadsheet::WriteExcel::Simple;
2              
3             $VERSION = '1.04';
4              
5 1     1   653 use strict;
  1         1  
  1         39  
6              
7 1     1   2087 use Spreadsheet::WriteExcel 0.31;
  1         100961  
  1         54  
8 1     1   1082 use IO::Scalar 1.126;
  1         7666  
  1         522  
9              
10             =head1 NAME
11              
12             Spreadsheet::WriteExcel::Simple - A simple single-sheet Excel document
13              
14             =head1 SYNOPSIS
15              
16             my $ss = Spreadsheet::WriteExcel::Simple->new;
17             $ss->write_bold_row(\@headings);
18             $ss->write_row(\@data);
19              
20             print $ss->data;
21             # or
22             $ss->save("filename.xls");
23              
24             =head1 DESCRIPTION
25              
26             This provides an abstraction to the L module
27             for easier creation of simple single-sheet Excel documents.
28              
29             In its most basic form it provides two methods for writing data:
30             write_row and write_bold_row which write the data supplied to
31             the next row of the spreadsheet.
32              
33             However, you can also use $ss->book and $ss->sheet to get at the
34             underlying workbook and worksheet from Spreadsheet::WriteExcel if you
35             wish to manipulate these directly.
36              
37             =head1 METHODS
38              
39             =head2 new
40              
41             my $ss = Spreadsheet::WriteExcel::Simple->new;
42              
43             Create a new single-sheet Excel document. You should not supply this
44             a filename or filehandle. The data is stored internally, and can be
45             retrieved later through the 'data' method or saved using the 'save'
46             method.
47              
48             =cut
49              
50             sub new {
51 0     0 1   my $class = shift;
52 0           my $self = bless {}, $class;
53              
54 0           my $fh = shift;
55             # Store the workbook in a tied scalar filehandle
56 0           $self->{book} = Spreadsheet::WriteExcel->new(
57             IO::Scalar->new_tie(\($self->{content}))
58             );
59 0           $self->{bold} = $self->book->addformat();
60 0           $self->{bold}->set_bold;
61 0           $self->{sheet} = $self->book->addworksheet;
62 0           $self->{_row} = 0;
63 0           $self;
64             }
65              
66             =head2 write_row / write_bold_row
67              
68             $ss->write_bold_row(\@headings);
69             $ss->write_row(\@data);
70              
71             These write the list of data into the next row of the spreadsheet.
72              
73             Caveat: An internal counter is kept as to which row is being written
74             to, so if you mix these functions with direct writes of your own,
75             these functions will continue where they left off, not where you have
76             written to.
77              
78             =cut
79              
80             sub write_row {
81 0     0 1   my $self = shift;
82 0           my $dataref = shift;
83 0 0         my @data = map { defined $_ ? $_ : '' } @$dataref;
  0            
84 0   0       my $fmt = shift || '';
85 0           my $col = 0;
86 0           my $ws = $self->sheet;
87 0           $ws->write($self->{_row}, $col++, $_, $fmt) foreach @data;
88 0           $self->{_row}++;
89             }
90              
91 0     0 1   sub write_bold_row { $_[0]->write_row($_[1], $_[0]->_bold) }
92              
93             =head2 data
94              
95             print $ss->data;
96              
97             This returns the data of the spreadsheet. If you're planning to print this
98             to a web-browser, be sure to print an 'application/excel' header first.
99              
100             =cut
101              
102             sub data {
103 0     0 1   my $self = shift;
104 0           $self->book->close;
105 0           return $self->{content};
106             }
107              
108             =head2 book / sheet
109              
110             my $workbook = $ss->book;
111             my $worksheet = $ss->sheet;
112              
113             These return the underlying Spreadsheet::WriteExcel objects representing
114             the workbook and worksheet respectively. If you find yourself making
115             more that a trivial amount of use of these, you probably shouldn't be
116             using this module, but using Spreadsheet::WriteExcel directly.
117              
118             =cut
119              
120 0     0 1   sub book { $_[0]->{book} }
121 0     0 1   sub sheet { $_[0]->{sheet} }
122              
123 0     0     sub _bold { $_[0]->{bold} }
124              
125             =head2 save
126              
127             $ss->save("filename.xls");
128              
129             Save the spreadsheet with the given filename.
130              
131             =cut
132              
133             sub save {
134 0     0 1   my $self = shift;
135 0 0         my $name = shift or die 'save() needs a file name';
136 0 0         open my $file, ">$name" or die "Could not open $name for writing: $!";
137 0           binmode $file;
138 0           print $file $self->data;
139 0           close $file;
140             }
141              
142             =head1 BUGS
143              
144             This can't yet handle dates in a sensible manner.
145              
146             =head1 AUTHOR
147              
148             Tony Bowden
149              
150             =head1 BUGS and QUERIES
151              
152             Please direct all correspondence regarding this module to:
153             bug-Spreadsheet-WriteExcel-Simple@rt.cpan.org
154              
155             =head1 SEE ALSO
156              
157             L. John McNamara has done a great job with
158             this module.
159              
160             =head1 COPYRIGHT
161              
162             Copyright (C) 2001-2005 Tony Bowden. All rights reserved.
163              
164             This module is free software; you can redistribute it and/or modify
165             it under the same terms as Perl itself.
166              
167             =cut
168              
169             1;
170