Revision ccb40ac4
Von Bernd Bleßmann vor fast 11 Jahren hinzugefügt
SL/Helper/Csv.pm | ||
---|---|---|
7 | 7 |
use Carp; |
8 | 8 |
use IO::File; |
9 | 9 |
use Params::Validate qw(:all); |
10 |
use List::MoreUtils qw(all pairwise); |
|
10 |
use List::MoreUtils qw(all pairwise firstidx);
|
|
11 | 11 |
use Text::CSV_XS; |
12 | 12 |
use Rose::Object::MakeMethods::Generic scalar => [ qw( |
13 | 13 |
file encoding sep_char quote_char escape_char header profile |
14 | 14 |
numberformat dateformat ignore_unknown_columns strict_profile is_multiplexed |
15 | 15 |
_row_header _io _csv _objects _parsed _data _errors all_cvar_configs case_insensitive_header |
16 |
_multiplex_datatype_position |
|
16 | 17 |
) ]; |
17 | 18 |
|
18 | 19 |
use SL::Helper::Csv::Dispatcher; |
... | ... | |
59 | 60 |
$self->_open_file; |
60 | 61 |
return if ! $self->_check_multiplexed; |
61 | 62 |
return if ! $self->_check_header; |
63 |
return if ! $self->_check_multiplex_datatype_position; |
|
62 | 64 |
return if ! $self->dispatcher->parse_profile; |
63 | 65 |
return if ! $self->_parse_data; |
64 | 66 |
|
... | ... | |
216 | 218 |
return $self->header($header); |
217 | 219 |
} |
218 | 220 |
|
221 |
sub _check_multiplex_datatype_position { |
|
222 |
my ($self) = @_; |
|
223 |
|
|
224 |
return 1 if !$self->is_multiplexed; # ok if if not multiplexed |
|
225 |
|
|
226 |
my @positions = map { firstidx { 'datatype' eq lc($_) } @{ $_ } } @{ $self->header }; |
|
227 |
my $first_pos = $positions[0]; |
|
228 |
if (all { $first_pos == $_ } @positions) { |
|
229 |
$self->_multiplex_datatype_position($first_pos); |
|
230 |
return 1; |
|
231 |
} else { |
|
232 |
$self->_push_error([0, |
|
233 |
"datatype field must be at the same position for all datatypes for multiplexed data", |
|
234 |
0, |
|
235 |
0]); |
|
236 |
return 0; |
|
237 |
} |
|
238 |
} |
|
239 |
|
|
219 | 240 |
sub _parse_data { |
220 | 241 |
my ($self, %params) = @_; |
221 | 242 |
my (@data, @errors); |
... | ... | |
261 | 282 |
} |
262 | 283 |
|
263 | 284 |
if ($self->is_multiplexed) { |
264 |
return $self->_row_header->{$row->[0]}
|
|
285 |
return $self->_row_header->{$row->[$self->_multiplex_datatype_position]}
|
|
265 | 286 |
} else { |
266 | 287 |
return $self->header; |
267 | 288 |
} |
... | ... | |
380 | 401 |
This module can handle multiplexed data of different class types. In that case |
381 | 402 |
multiple profiles with classes and row identifiers must be given. Multiple |
382 | 403 |
headers may also be given or read from csv data. Data must contain the row |
383 |
identifier in the first column and it's field name must be 'datatype'.
|
|
404 |
identifier in the column named 'datatype'.
|
|
384 | 405 |
|
385 | 406 |
=back |
386 | 407 |
|
... | ... | |
446 | 467 |
If not given, headers are taken from the first n lines of data, where n is the |
447 | 468 |
number of different class types. |
448 | 469 |
|
449 |
In case of multiplexed data the first column must be named 'datatype'. This |
|
450 |
name must be given in the header. |
|
470 |
In case of multiplexed data there must be a column named 'datatype'. This |
|
471 |
column must be given in each header and must be at the same position in each |
|
472 |
header. |
|
451 | 473 |
|
452 | 474 |
Examples: |
453 | 475 |
|
454 | 476 |
classic data of one type: |
455 | 477 |
[ 'name', 'street', 'zipcode', 'city' ] |
456 | 478 |
|
457 |
multiplexed data with two different types |
|
479 |
multiplexed data with two different types:
|
|
458 | 480 |
[ [ 'datatype', 'ordernumber', 'customer', 'transdate' ], |
459 | 481 |
[ 'datatype', 'partnumber', 'qty', 'sellprice' ] ] |
460 | 482 |
|
Auch abrufbar als: Unified diff
CSV-Import mit Multiplex-Daten: Die Spalte datatype muss nicht an der ersten Position sein.