/[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 1068 - (show annotations)
Tue Nov 27 23:45:28 2007 UTC (16 years, 5 months ago) by dpavlin
File size: 2647 byte(s)
 r1670@llin:  dpavlin | 2007-11-28 00:13:29 +0100
 - WebPAC::Output::Excel

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

  ViewVC Help
Powered by ViewVC 1.1.26