/[webpac2]/trunk/lib/WebPAC/Output/Excel.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /trunk/lib/WebPAC/Output/Excel.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1072 - (show annotations)
Wed Nov 28 00:25:11 2007 UTC (16 years, 5 months ago) by dpavlin
File size: 2888 byte(s)
protect values that look like Excel formulas (=something)

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 # protect data which looks like forula
118 $val = "'$val" if $val =~ m/^=/;
119 $log->debug("$col$l|$val");
120 $worksheet->write( $col . $l , $val );
121 $cols++;
122 }
123
124 $self->line( $l + 1 ) if $cols;
125
126 return $cols;
127 }
128
129 =head2 finish
130
131 $out->finish;
132
133 =cut
134
135 sub finish {
136 my $self = shift;
137
138 my $log = $self->_get_logger();
139
140 my $path = $self->path;
141
142 $self->workbook->close() ||
143 $log->logdie("can't close Excel file $path: $!");
144
145 $log->info("created $path ", -s $path, " bytes with ", $self->line, " rows");
146
147 return $self->line;
148 }
149
150 =head1 AUTHOR
151
152 Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>
153
154 =head1 COPYRIGHT & LICENSE
155
156 Copyright 2007 Dobrica Pavlinusic, All Rights Reserved.
157
158 This program is free software; you can redistribute it and/or modify it
159 under the same terms as Perl itself.
160
161 =cut
162
163 1; # End of WebPAC::Output::Excel

  ViewVC Help
Powered by ViewVC 1.1.26