From 80198923186bedda61d4dceb0272210dc8bec533 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Tue, 16 Aug 2016 03:08:25 -0400 Subject: handle cell data without explicit location data (#61) apparently this is a thing sometimes? --- Changes | 2 ++ lib/Spreadsheet/ParseXLSX.pm | 31 +++++++++++++++++++++---------- t/bug-61.t | 11 +++++++++++ t/data/bug-61.xlsx | Bin 0 -> 90974 bytes 4 files changed, 34 insertions(+), 10 deletions(-) create mode 100644 t/bug-61.t create mode 100644 t/data/bug-61.xlsx diff --git a/Changes b/Changes index 452c2b1..d7bfb11 100644 --- a/Changes +++ b/Changes @@ -1,6 +1,8 @@ Revision history for Spreadsheet-ParseXLSX {{$NEXT}} + - Fix issues parsing sheets whose cell elements do not have location + attributes (dgiordano, #61) 0.26 2016-08-16 - Add explicit dependency on perl 5.10 (we apparently broke 5.8 support diff --git a/lib/Spreadsheet/ParseXLSX.pm b/lib/Spreadsheet/ParseXLSX.pm index fac59ba..5df5111 100644 --- a/lib/Spreadsheet/ParseXLSX.pm +++ b/lib/Spreadsheet/ParseXLSX.pm @@ -224,6 +224,7 @@ sub _parse_sheet { my $default_column_width = 10; my %cells; + my $row_idx = 0; my $sheet_xml = $self->_new_twig( twig_roots => { @@ -331,15 +332,6 @@ sub _parse_sheet { $twig->purge; }, - 's:row' => sub { - my ( $twig, $row ) = @_; - - $row_heights[ $row->att('r') - 1 ] = $row->att('ht'); - $rows_hidden[ $row->att('r') - 1 ] = $self->_xml_boolean($row->att('hidden')); - - $twig->purge; - }, - 's:selection' => sub { my ( $twig, $selection ) = @_; @@ -367,9 +359,26 @@ sub _parse_sheet { 's:sheetData/s:row' => sub { my ( $twig, $row_elt ) = @_; + my $explicit_row_idx = $row_elt->att('r'); + $row_idx = $explicit_row_idx - 1 if defined $explicit_row_idx; + $row_heights[$row_idx] = $row_elt->att('ht'); + $rows_hidden[$row_idx] = $self->_xml_boolean($row_elt->att('hidden')); + + my $col_idx = 0; for my $cell ( $row_elt->children('s:c') ){ - my ($row, $col) = $self->_cell_to_row_col($cell->att('r')); + my $loc = $cell->att('r'); + my ($row, $col); + if ($loc) { + ($row, $col) = $self->_cell_to_row_col($loc); + if ($row != $row_idx) { + warn "mismatched coords: got $loc for cell in row $row_idx"; + } + $col_idx = $col - 1; + } + else { + ($row, $col) = ($row_idx, $col_idx); + } $sheet->{MaxRow} = $row if $sheet->{MaxRow} < $row; $sheet->{MaxCol} = $col @@ -444,9 +453,11 @@ sub _parse_sheet { ); $cells{"$row;$col"} = $cell; $sheet->{Cells}[$row][$col] = $cell; + $col_idx++; } $twig->purge; + $row_idx++; }, } ); diff --git a/t/bug-61.t b/t/bug-61.t new file mode 100644 index 0000000..72f8af4 --- /dev/null +++ b/t/bug-61.t @@ -0,0 +1,11 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use Test::More; + +use Spreadsheet::ParseXLSX; + +my $wb = Spreadsheet::ParseXLSX->new->parse('t/data/bug-61.xlsx'); +pass('it parses successfully'); + +done_testing; diff --git a/t/data/bug-61.xlsx b/t/data/bug-61.xlsx new file mode 100644 index 0000000..82f791b Binary files /dev/null and b/t/data/bug-61.xlsx differ -- cgit v1.2.3-54-g00ecf