From 9cf7419380a812d1bbacb74018c111a7e77cb5ac Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Wed, 1 May 2013 12:25:42 -0500 Subject: handle types a bit better --- lib/Spreadsheet/Template/Generator/Parser/Excel.pm | 30 ++++++++++++++++++++-- lib/Spreadsheet/Template/Generator/Parser/XLSX.pm | 14 +++++++--- 2 files changed, 39 insertions(+), 5 deletions(-) diff --git a/lib/Spreadsheet/Template/Generator/Parser/Excel.pm b/lib/Spreadsheet/Template/Generator/Parser/Excel.pm index 4638a8b..1aed4a8 100644 --- a/lib/Spreadsheet/Template/Generator/Parser/Excel.pm +++ b/lib/Spreadsheet/Template/Generator/Parser/Excel.pm @@ -72,10 +72,36 @@ sub _parse_cell { my $self = shift; my ($cell) = @_; + my %types = ( + 'Numeric' => 'number', + 'Text' => 'string', + 'Date' => 'date_time', + ); + + my $contents = $cell->unformatted; + my $type = $cell->type; + + if ($type eq 'Numeric') { + $type = 'number'; + } + elsif ($type eq 'Text') { + if ($contents =~ /^=/) { + $type = 'formula'; + } + else { + $type = 'string'; + } + } + elsif ($type eq 'Date') { + $type = 'date_time'; + } + else { + die "unknown type $type"; + } my $data = { - contents => $self->_filter_cell_contents($cell->unformatted), - type => $cell->type, + contents => $self->_filter_cell_contents($contents, $type), + type => $type, }; return $data; diff --git a/lib/Spreadsheet/Template/Generator/Parser/XLSX.pm b/lib/Spreadsheet/Template/Generator/Parser/XLSX.pm index 8e3c29c..75f8481 100644 --- a/lib/Spreadsheet/Template/Generator/Parser/XLSX.pm +++ b/lib/Spreadsheet/Template/Generator/Parser/XLSX.pm @@ -12,11 +12,19 @@ sub make_excel { return Spreadsheet::XLSX->new($filename); } +# XXX this stuff all feels like working around bugs in Spreadsheet::XLSX - +# maybe look into that at some point sub _filter_cell_contents { my $self = shift; - my ($contents) = @_; - # XXX this decode call really feels like a bug in Spreadsheet::XLSX - return XML::Entities::decode('all', $contents); + my ($contents, $type) = @_; + + $contents = XML::Entities::decode('all', $contents); + + if ($type eq 'number') { + $contents = 0+$contents; + } + + return $contents; } __PACKAGE__->meta->make_immutable; -- cgit v1.2.3-54-g00ecf