| 1 | package Ftree::DataParsers::ExcelxFormat; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use version; our $VERSION = qv('2.3.41'); | ||
| 5 | |||
| 6 | use Spreadsheet::XLSX; | ||
| 7 | # use Spreadsheet::ParseExcel; | ||
| 8 | use Ftree::DataParsers::ExtendedSimonWardFormat | ||
| 9 | ; # for getting pictures. Temporal solution | ||
| 10 | use Ftree::FamilyTreeData; | ||
| 11 | # use CGI::Carp qw(fatalsToBrowser); | ||
| 12 | use Encode qw(decode); | ||
| 13 | |||
| 14 | sub createFamilyTreeDataFromFile { | ||
| 15 | my ($config_) = @_; | ||
| 16 | my $file_name = $config_->{file_name} | ||
| 17 | or die "No file_name is given in config"; | ||
| 18 | |||
| 19 | my $family_tree_data = Ftree::FamilyTreeData->new(); | ||
| 20 | my $excel = Spreadsheet::XLSX -> new ( $file_name);#, $converter); | ||
| 21 | # my $excel = Spreadsheet::ParseXLSX->new->parse($file_name); | ||
| 22 | # my $workbook = ReadData($file_name) | ||
| 23 | # or die "Unable to parse file " . $file_name; | ||
| 24 | |||
| 25 | # my $excel = Spreadsheet::ParseXLSX->new->parse($file_name); | ||
| 26 | # Spreadsheet::ParseXLSX::Workbook->Parse($file_name) | ||
| 27 | # or die "Unable to parse file " . $file_name; | ||
| 28 | foreach my $sheet ( @{ $excel->{Worksheet} } ) { | ||
| 29 | foreach my $row ( $sheet->{MinRow} + 1 .. $sheet->{MaxRow} ) { | ||
| 30 | my $tempperson = { | ||
| 31 | id => convertCell( $sheet->{Cells}[$row][0] ), | ||
| 32 | first_name => convertCell( $sheet->{Cells}[$row][3] ), | ||
| 33 | mid_name => convertCell( $sheet->{Cells}[$row][4] ), | ||
| 34 | last_name => convertCell( $sheet->{Cells}[$row][5] ), | ||
| 35 | title => convertCell( $sheet->{Cells}[$row][1] ), | ||
| 36 | prefix => convertCell( $sheet->{Cells}[$row][2] ), | ||
| 37 | suffix => convertCell( $sheet->{Cells}[$row][6] ), | ||
| 38 | nickname => convertCell( $sheet->{Cells}[$row][7] ), | ||
| 39 | father_id => convertCell( $sheet->{Cells}[$row][8] ), | ||
| 40 | mother_id => convertCell( $sheet->{Cells}[$row][9] ), | ||
| 41 | email => convertCell( $sheet->{Cells}[$row][10] ), | ||
| 42 | homepage => convertCell( $sheet->{Cells}[$row][11] ), | ||
| 43 | date_of_birth => convertCell( $sheet->{Cells}[$row][12] ), | ||
| 44 | date_of_death => convertCell( $sheet->{Cells}[$row][13] ), | ||
| 45 | gender => convertCell( $sheet->{Cells}[$row][14] ), | ||
| 46 | is_living => convertCell( $sheet->{Cells}[$row][15] ), | ||
| 47 | place_of_birth => convertCell( $sheet->{Cells}[$row][16] ), | ||
| 48 | place_of_death => convertCell( $sheet->{Cells}[$row][17] ), | ||
| 49 | cemetery => convertCell( $sheet->{Cells}[$row][18] ), | ||
| 50 | schools => ( defined $sheet->{Cells}[$row][19] ) | ||
| 51 | ? [ split( /,/, convertCell( $sheet->{Cells}[$row][19] ) ) ] | ||
| 52 | : undef, | ||
| 53 | jobs => ( defined $sheet->{Cells}[$row][20] ) | ||
| 54 | ? [ split( /,/, convertCell( $sheet->{Cells}[$row][20] ) ) ] | ||
| 55 | : undef, | ||
| 56 | work_places => ( defined $sheet->{Cells}[$row][21] ) | ||
| 57 | ? [ split( /,/, convertCell( $sheet->{Cells}[$row][21] ) ) ] | ||
| 58 | : undef, | ||
| 59 | places_of_living => convertCell( $sheet->{Cells}[$row][22] ), | ||
| 60 | general => convertCell( $sheet->{Cells}[$row][23] ) | ||
| 61 | }; | ||
| 62 | $family_tree_data->add_person($tempperson); | ||
| 63 | } | ||
| 64 | } | ||
| 65 | |||
| 66 | if ( defined $config_->{photo_dir} ) { | ||
| 67 | Ftree::DataParsers::ExtendedSimonWardFormat::setPictureDirectory( $config_->{photo_dir} ); | ||
| 68 | Ftree::DataParsers::ExtendedSimonWardFormat::fill_up_pictures($family_tree_data); | ||
| 69 | } | ||
| 70 | |||
| 71 | return $family_tree_data; | ||
| 72 | } | ||
| 73 | |||
| 74 | |||
| 75 | sub convertCell { | ||
| 76 | my ($cell) = @_; | ||
| 77 | return undef unless defined $cell; | ||
| 78 | # my $rowtxt = ''; | ||
| 79 | # if ( !$cell ) { | ||
| 80 | # $rowtxt = "\t"; | ||
| 81 | |||
| 82 | # # next; | ||
| 83 | # } | ||
| 84 | my $val = $cell->{Val}; | ||
| 85 | # if ( !defined($val) or $val eq '' ) { | ||
| 86 | # $rowtxt = "\t"; | ||
| 87 | |||
| 88 | # # next; | ||
| 89 | # } | ||
| 90 | utf8::decode($val); | ||
| 91 | # $val = decode( "UTF-16BE", $val ) if ( $cell->{Code} eq 'ucs2' ); | ||
| 92 | # $val =~ s/^\s+//; | ||
| 93 | # $val =~ s/\s+$//; | ||
| 94 | # if ( $val eq '' ) { | ||
| 95 | # $rowtxt = "\t"; | ||
| 96 | # next; | ||
| 97 | # } | ||
| 98 | # $rowtxt = "$val"; | ||
| 99 | |||
| 100 | return $val;#$rowtxt; | ||
| 101 | |||
| 102 | # return $cell->{Val} if( $cell->{Type} eq "Numeric" ); | ||
| 103 | # return $cell->{Val} unless( defined $cell->{Code} ); | ||
| 104 | # use Encode qw(decode encode); | ||
| 105 | # $characters = decode('UTF-8', $octets, Encode::FB_CROAK); | ||
| 106 | # return decode('UTF-8',$cell->{Val},Encode::FB_CROAK);#decode('UTF-16LE', $cell->{Val}); | ||
| 107 | # Encode::from_to( $cell->{Val}, "utf8", "utf16le" ); | ||
| 108 | } | ||
| 109 | |||
| 110 | 1; |