6 |
use Spreadsheet::ParseExcel; |
use Spreadsheet::ParseExcel; |
7 |
use Spreadsheet::ParseExcel::Utility qw/int2col/; |
use Spreadsheet::ParseExcel::Utility qw/int2col/; |
8 |
use base qw/WebPAC::Common/; |
use base qw/WebPAC::Common/; |
9 |
|
use Text::Unaccent::PurePerl qw/unac_string/; |
10 |
|
use Data::Dump qw/dump/; |
11 |
|
|
12 |
=head1 NAME |
=head1 NAME |
13 |
|
|
15 |
|
|
16 |
=cut |
=cut |
17 |
|
|
18 |
our $VERSION = '0.05'; |
our $VERSION = '0.06'; |
19 |
|
|
20 |
|
|
21 |
=head1 SYNOPSIS |
=head1 SYNOPSIS |
115 |
$log->debug("fetch_rec( $mfn ) row: $row cols: ",$sheet->{MinCol}," - ",$sheet->{MaxCol}); |
$log->debug("fetch_rec( $mfn ) row: $row cols: ",$sheet->{MinCol}," - ",$sheet->{MaxCol}); |
116 |
|
|
117 |
foreach my $col ( $sheet->{MinCol} ... $sheet->{MaxCol} ) { |
foreach my $col ( $sheet->{MinCol} ... $sheet->{MaxCol} ) { |
118 |
if (my $v = $sheet->{Cells}->[$row]->[$col]->{_Value}) { ## XXX _Value = formatted | Val = unformated ! |
my $v = $sheet->{Cells}->[$row]->[$col]->{_Value}; ## XXX _Value = formatted | Val = unformated ! |
119 |
$rec->{ int2col($col) } = $v; |
$rec->{ int2col($col) } = $v if defined $v; |
|
} |
|
120 |
} |
} |
121 |
|
|
122 |
# add mfn only to records with data |
# add mfn only to records with data |
138 |
return $self->{size}; |
return $self->{size}; |
139 |
} |
} |
140 |
|
|
|
sub default_encoding { 'UTF-16' } |
|
|
|
|
141 |
our @labels; |
our @labels; |
142 |
our @names; |
our @names; |
143 |
|
|
148 |
|
|
149 |
my $sheet = $self->{sheet}; |
my $sheet = $self->{sheet}; |
150 |
|
|
151 |
|
my $ds; |
152 |
|
|
153 |
if ( ! @labels ) { |
if ( ! @labels ) { |
154 |
push @labels, $sheet->{Cells}->[0]->[$_]->{_Value} |
|
155 |
foreach ( $sheet->{MinCol} ... $sheet->{MaxCol} ) |
my $labels; |
156 |
; |
|
157 |
@names = map { s{\W+}{_}; $_ } @labels; |
foreach ( $sheet->{MinCol} ... $sheet->{MaxCol} ) { |
158 |
$log->loginfo("column labels:", @labels, @names); |
my $label = $sheet->{Cells}->[0]->[$_]->{_Value}; |
159 |
|
last if length($label) == 0; |
160 |
|
push @labels, $label; |
161 |
|
} |
162 |
|
@names = map { |
163 |
|
my $t = unac_string($_); |
164 |
|
$t =~ s{[^a-z0-9]+}{_}gi; |
165 |
|
$t =~ s{_+$}{}; |
166 |
|
$t =~ s{^_+}{}; |
167 |
|
$t = lc($t); |
168 |
|
$labels .= "$t\t$_\n"; |
169 |
|
$t; |
170 |
|
} @labels; |
171 |
|
|
172 |
|
$log->info("columns = ", dump( @names ), " labels = ", dump( @labels ) ); |
173 |
|
|
174 |
|
$ds = { |
175 |
|
'_labels' => [ @labels ], |
176 |
|
'_names' => [ @names ], |
177 |
|
}; |
178 |
|
|
179 |
|
my $path = $self->{labels} || 'var/labels.txt'; |
180 |
|
{ |
181 |
|
warn $labels; |
182 |
|
open(my $fh, '>:raw', $path) || die "$path: $!"; |
183 |
|
print $fh $labels; |
184 |
|
close $fh; |
185 |
|
} |
186 |
|
$log->info("created labels $path ", -s $path, " bytes"); |
187 |
} |
} |
188 |
|
|
189 |
|
|
190 |
my $row = $self->{from} + $mfn - 1; |
my $row = $self->{from} + $mfn - 1; |
191 |
|
|
192 |
|
my $data; |
193 |
|
foreach ( $sheet->{MinCol} ... $sheet->{MaxCol} ) { |
194 |
|
my $name = $names[$_]; |
195 |
|
next unless $name; |
196 |
|
my $v = $sheet->{Cells}->[$row]->[$_]->{_Value}; |
197 |
|
$data->{ $name } = $v; |
198 |
|
$ds->{ $name } = { search => [ $v ] } if defined $v; |
199 |
|
} |
200 |
|
|
201 |
|
$ds->{'_rows'} = { $self->{sheet}->{Name} => [ $data ] }; |
202 |
|
|
203 |
|
return $ds; |
204 |
} |
} |
205 |
|
|
206 |
=head1 AUTHOR |
=head1 AUTHOR |