| 1 | package Data::XLSX::Parser::Sheet; | ||
| 2 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | |||
| 5 | 1 | ▶ | use File::Temp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 0 | ▶ | use XML::Parser::Expat; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 7 | use Archive::Zip (); | ||
| 8 | use Time::Piece (); | ||
| 9 | use Scalar::Util (); | ||
| 10 | use Carp; | ||
| 11 | |||
| 12 | use constant { | ||
| 13 | STYLE_IDX => 'i', | ||
| 14 | STYLE => 's', | ||
| 15 | FMT => 'f', | ||
| 16 | REF => 'r', | ||
| 17 | COLUMN => 'c', | ||
| 18 | VALUE => 'v', | ||
| 19 | TYPE => 't', | ||
| 20 | TYPE_SHARED_STRING => 's', | ||
| 21 | GENERATED_CELL => 'g', | ||
| 22 | }; | ||
| 23 | |||
| 24 | sub new { | ||
| 25 | my ($class, $doc, $archive, $sheet_id_or_filepath) = @_; | ||
| 26 | |||
| 27 | my $filepath = $sheet_id_or_filepath; | ||
| 28 | if (Scalar::Util::looks_like_number($sheet_id_or_filepath)) { | ||
| 29 | $filepath = sprintf 'worksheets/sheet%d.xml', $sheet_id_or_filepath; | ||
| 30 | } | ||
| 31 | |||
| 32 | my $self = bless { | ||
| 33 | _document => $doc, | ||
| 34 | |||
| 35 | _data => '', | ||
| 36 | _is_sheetdata => 0, | ||
| 37 | _row_count => 0, | ||
| 38 | _current_row => [], | ||
| 39 | _cell => undef, | ||
| 40 | _is_value => 0, | ||
| 41 | |||
| 42 | _shared_strings => $doc->shared_strings, | ||
| 43 | _styles => $doc->styles, | ||
| 44 | |||
| 45 | }, $class; | ||
| 46 | |||
| 47 | my $fh = File::Temp->new( SUFFIX => '.xml' ) or confess "couldn't create tempfile: $!"; | ||
| 48 | |||
| 49 | my $handle = $archive->sheet($filepath) or confess "couldn't get handle to sheet archive $filepath: $!"; | ||
| 50 | confess 'Failed to write temporary file: ', $fh->filename | ||
| 51 | unless $handle->extractToFileNamed($fh->filename) == Archive::Zip::AZ_OK; | ||
| 52 | |||
| 53 | my $parser = XML::Parser::Expat->new(Namespaces=>1); | ||
| 54 | $parser->setHandlers( | ||
| 55 | Start => sub { $self->_start(@_) }, | ||
| 56 | End => sub { $self->_end(@_) }, | ||
| 57 | Char => sub { $self->_char(@_) }, | ||
| 58 | ); | ||
| 59 | $parser->parse($fh); | ||
| 60 | |||
| 61 | $self; | ||
| 62 | } | ||
| 63 | |||
| 64 | sub _start { | ||
| 65 | my ($self, $parser, $name, %attrs) = @_; | ||
| 66 | |||
| 67 | if ($name eq 'sheetData') { | ||
| 68 | $self->{_is_sheetdata} = 1; | ||
| 69 | } elsif ($self->{_is_sheetdata} and $name eq 'row') { | ||
| 70 | $self->{_current_row} = []; | ||
| 71 | } elsif ($name eq 'c') { | ||
| 72 | $self->{_cell} = { | ||
| 73 | STYLE_IDX() => $attrs{ STYLE() }, | ||
| 74 | TYPE() => $attrs{ TYPE() }, | ||
| 75 | REF() => $attrs{ REF() }, | ||
| 76 | COLUMN() => scalar(@{ $self->{_current_row} }) + 1, | ||
| 77 | }; | ||
| 78 | } elsif ($name eq 'v') { | ||
| 79 | $self->{_is_value} = 1; | ||
| 80 | } | ||
| 81 | } | ||
| 82 | |||
| 83 | sub _end { | ||
| 84 | my ($self, $parser, $name) = @_; | ||
| 85 | |||
| 86 | if ($name eq 'sheetData') { | ||
| 87 | $self->{_is_sheetdata} = 0; | ||
| 88 | } elsif ($self->{_is_sheetdata} and $name eq 'row') { | ||
| 89 | $self->{_row_count}++; | ||
| 90 | $self->{_document}->_row_event( delete $self->{_current_row} ); | ||
| 91 | } elsif ($name eq 'c') { | ||
| 92 | my $c = $self->{_cell}; | ||
| 93 | $self->_parse_rel($c); | ||
| 94 | |||
| 95 | if (($c->{ TYPE() } || '') eq TYPE_SHARED_STRING()) { | ||
| 96 | my $idx = int($self->{_data}); | ||
| 97 | $c->{ VALUE() } = $self->{_shared_strings}->get($idx); | ||
| 98 | } else { | ||
| 99 | $c->{ VALUE() } = $self->{_data}; | ||
| 100 | } | ||
| 101 | |||
| 102 | $c->{ STYLE() } = $self->{_styles}->cell_style( $c->{ STYLE_IDX() } ); | ||
| 103 | $c->{ FMT() } = my $cell_type = $self->{_styles}->cell_type_from_style($c->{ STYLE() }); | ||
| 104 | |||
| 105 | my $v = $c->{ VALUE() }; | ||
| 106 | |||
| 107 | if (!defined $c->{ TYPE() }) { | ||
| 108 | # actual value (number or date) | ||
| 109 | if ($v && defined $c->{ FMT() } && | ||
| 110 | $c->{ FMT() } =~ /^datetime\.(date)?(time)?$/) { | ||
| 111 | if (Scalar::Util::looks_like_number($v)) { | ||
| 112 | $c->{ VALUE() } = $self->_convert_serial_time($v); | ||
| 113 | } | ||
| 114 | } elsif (Scalar::Util::looks_like_number($v)) { | ||
| 115 | $c->{ VALUE() } = $v + 0; | ||
| 116 | } | ||
| 117 | } else { | ||
| 118 | if (!defined $v) { | ||
| 119 | $c->{ VALUE() } = ''; | ||
| 120 | } elsif ($cell_type ne 'unicode') { | ||
| 121 | # warn 'not unicode: ' . $cell_type; | ||
| 122 | $c->{ VALUE() } = $v; | ||
| 123 | } | ||
| 124 | } | ||
| 125 | |||
| 126 | push @{ $self->{_current_row} }, $c; | ||
| 127 | |||
| 128 | $self->{_data} = ''; | ||
| 129 | $self->{_cell} = undef; | ||
| 130 | } elsif ($name eq 'v') { | ||
| 131 | $self->{_is_value} = 0; | ||
| 132 | } | ||
| 133 | } | ||
| 134 | |||
| 135 | sub _convert_serial_time { | ||
| 136 | my ($self, $serial_time) = @_; | ||
| 137 | |||
| 138 | # UNIX Epoch(1970/1/1 00:00:00) is 25569.0 | ||
| 139 | my $epoch = ($serial_time - 25569) * 24 * 60 * 60; | ||
| 140 | # round to nearest second to compensate for floating point errors | ||
| 141 | $epoch = $epoch > 0 ? int( $epoch + 0.5) : int( $epoch - 0.5); | ||
| 142 | return Time::Piece::gmtime($epoch); | ||
| 143 | } | ||
| 144 | |||
| 145 | sub _char { | ||
| 146 | my ($self, $parser, $data) = @_; | ||
| 147 | |||
| 148 | if ($self->{_is_value}) { | ||
| 149 | $self->{_data} .= $data; | ||
| 150 | } | ||
| 151 | } | ||
| 152 | |||
| 153 | sub _parse_rel { | ||
| 154 | my ($self, $cell) = @_; | ||
| 155 | |||
| 156 | my ($column, $row); | ||
| 157 | my $v; | ||
| 158 | if ($cell->{ REF() }) { | ||
| 159 | ($column, $row) = $cell->{ REF() } =~ /([A-Z]+)(\d+)/; | ||
| 160 | $v = 0; | ||
| 161 | my $i = 0; | ||
| 162 | for my $ch (split '', $column) { | ||
| 163 | my $s = length($column) - $i++ - 1; | ||
| 164 | $v += (ord($ch) - ord('A') + 1) * (26**$s); | ||
| 165 | } | ||
| 166 | $cell->{ row } = $row + 0; | ||
| 167 | if ($cell->{ COLUMN() } > $v) { | ||
| 168 | carp sprintf 'Detected smaller index than current cell, something is wrong! (row %s): %s <> %s', $row, $v, $cell->{ COLUMN() }; | ||
| 169 | } | ||
| 170 | } else { | ||
| 171 | # fallback if REF() not defined | ||
| 172 | $v = $cell->{ COLUMN() }; | ||
| 173 | $row = $self->{_row_count}+1; | ||
| 174 | $cell->{ row } = $row; | ||
| 175 | } | ||
| 176 | |||
| 177 | # add omitted cells as empty... | ||
| 178 | for ($cell->{ COLUMN() } .. $v-1) { | ||
| 179 | push @{ $self->{_current_row} }, { | ||
| 180 | GENERATED_CELL() => 1, | ||
| 181 | STYLE_IDX() => undef, | ||
| 182 | TYPE() => undef, | ||
| 183 | REF() => [ $_, $row ], | ||
| 184 | COLUMN() => $_, | ||
| 185 | VALUE() => '', | ||
| 186 | FMT() => 'unicode', | ||
| 187 | }; | ||
| 188 | } | ||
| 189 | } | ||
| 190 | |||
| 191 | 1; | ||
| 192 | __END__ | ||