1 |
package WebPAC::Output::Excel; |
2 |
|
3 |
use warnings; |
4 |
use strict; |
5 |
|
6 |
use WebPAC::Common; |
7 |
use base qw/WebPAC::Common WebPAC::Output Class::Accessor/; |
8 |
__PACKAGE__->mk_accessors(qw( |
9 |
path |
10 |
filter |
11 |
|
12 |
workbook |
13 |
worksheet |
14 |
line |
15 |
)); |
16 |
|
17 |
use Spreadsheet::WriteExcel; |
18 |
use File::Slurp; |
19 |
use Encode qw/decode/; |
20 |
|
21 |
=head1 NAME |
22 |
|
23 |
WebPAC::Output::Excel - Create binary Excel file |
24 |
|
25 |
=cut |
26 |
|
27 |
=head1 SYNOPSIS |
28 |
|
29 |
Create Excel output for export into other systems from C<csv()> constructs |
30 |
with columns named C<A>..C<Z> (or whatever L<Spreadsheet::WriteExcel> supports) |
31 |
|
32 |
=head1 FUNCTIONS |
33 |
|
34 |
=head2 new |
35 |
|
36 |
my $out = new WebPAC::Output::Excel({ |
37 |
path => '/path/to/file.xls', |
38 |
filter => 'csv', |
39 |
}); |
40 |
|
41 |
Options are: |
42 |
|
43 |
=over 4 |
44 |
|
45 |
=item path |
46 |
|
47 |
path to Excel file |
48 |
|
49 |
=item filter |
50 |
|
51 |
select name of variable from C<< to('csv','A',...) >> constructs used by |
52 |
L<WebPAC::Normalize> |
53 |
|
54 |
=back |
55 |
|
56 |
=head2 init |
57 |
|
58 |
$out->init; |
59 |
|
60 |
=cut |
61 |
|
62 |
sub init { |
63 |
my $self = shift; |
64 |
my $log = $self->_get_logger; |
65 |
|
66 |
if ( ! $self->path ) { |
67 |
$log->logwarn("need path for ", __PACKAGE__); |
68 |
return 0; |
69 |
} |
70 |
|
71 |
$self->workbook( Spreadsheet::WriteExcel->new( $self->path ) ) || |
72 |
$log->logdie("can't open ", $self->path,": $!"); |
73 |
|
74 |
$self->worksheet( $self->workbook->add_worksheet() ) || |
75 |
$log->logdie("can't add_worksheet"); |
76 |
|
77 |
$self->line( 1 ); |
78 |
|
79 |
$self->filter( 'csv' ) unless $self->filter; |
80 |
|
81 |
return 1; |
82 |
} |
83 |
|
84 |
|
85 |
=head2 add |
86 |
|
87 |
Adds one entry to database. |
88 |
|
89 |
$out->add( 42, $ds ); |
90 |
|
91 |
Returns number of columns added |
92 |
|
93 |
=cut |
94 |
|
95 |
sub add { |
96 |
my $self = shift; |
97 |
|
98 |
my ( $id, $ds ) = @_; |
99 |
|
100 |
my $log = $self->_get_logger; |
101 |
$log->logdie("need id") unless defined $id; |
102 |
$log->logdie("need ds") unless $ds; |
103 |
|
104 |
$log->debug("id: $id ds = ",sub { dump($ds) }); |
105 |
|
106 |
my $l = $self->line; |
107 |
|
108 |
my $cols = 0; |
109 |
|
110 |
my $hash = $self->ds_to_hash( $ds, $self->filter, disable_key_mungle => 1, single_values => 1 ); |
111 |
$log->debug("hash from ",$self->filter," = ", sub { dump( $hash ) }); |
112 |
|
113 |
my $worksheet = $self->worksheet || $log->logconfess("no worksheet?"); |
114 |
foreach my $col ( sort grep { /^[A-Z]/ } keys %$hash ) { |
115 |
# FIXME internal WebPAC encoding is ISO-8859-2 |
116 |
my $val = decode('ISO-8859-2', $hash->{$col}); |
117 |
$log->debug("$col$l|$val"); |
118 |
$worksheet->write( $col . $l , $val ); |
119 |
$cols++; |
120 |
} |
121 |
|
122 |
$self->line( $l + 1 ) if $cols; |
123 |
|
124 |
return $cols; |
125 |
} |
126 |
|
127 |
=head2 finish |
128 |
|
129 |
$out->finish; |
130 |
|
131 |
=cut |
132 |
|
133 |
sub finish { |
134 |
my $self = shift; |
135 |
|
136 |
my $log = $self->_get_logger(); |
137 |
|
138 |
my $path = $self->path; |
139 |
|
140 |
$self->workbook->close() || |
141 |
$log->logdie("can't close Excel file $path: $!"); |
142 |
|
143 |
$log->info("created $path ", -s $path, " bytes with ", $self->line, " rows"); |
144 |
|
145 |
return $self->line; |
146 |
} |
147 |
|
148 |
=head1 AUTHOR |
149 |
|
150 |
Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >> |
151 |
|
152 |
=head1 COPYRIGHT & LICENSE |
153 |
|
154 |
Copyright 2007 Dobrica Pavlinusic, All Rights Reserved. |
155 |
|
156 |
This program is free software; you can redistribute it and/or modify it |
157 |
under the same terms as Perl itself. |
158 |
|
159 |
=cut |
160 |
|
161 |
1; # End of WebPAC::Output::Excel |