summaryrefslogtreecommitdiffstats
path: root/lib/Spreadsheet/Template/Generator/Parser/XLSX.pm
blob: e9e89721055578f44c2b931012f9e9d6ae47351c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package Spreadsheet::Template::Generator::Parser::XLSX;
use Moose;

use Spreadsheet::XLSX;
use XML::Entities;
use XML::Twig;

with 'Spreadsheet::Template::Generator::Parser::Excel';

sub make_excel {
    my $self = shift;
    my ($filename) = @_;
    my $excel = Spreadsheet::XLSX->new($filename);

    # XXX Spreadsheet::XLSX doesn't extract this information currently
    my $zip = Archive::Zip->new;
    die "Can't open $filename as zip file"
        unless $zip->read($filename) == Archive::Zip::AZ_OK;

    for my $sheet ($excel->worksheets) {
        my $contents = $zip->memberNamed("xl/$sheet->{path}")->contents;
        next unless $contents;

        my @column_widths;
        my @row_heights;

        my $xml = XML::Twig->new;
        $xml->parse($contents);
        my $root = $xml->root;

        my ($format) = $root->find_nodes('//sheetFormatPr');
        my $default_row_height = $format->att('defaultRowHeight');
        my $default_column_width = $format->att('baseColWidth');

        for my $col ($root->find_nodes('//col')) {
            $column_widths[$col->att('min') - 1] = $col->att('width');
        }

        for my $row ($root->find_nodes('//row')) {
            $row_heights[$row->att('r') - 1] = $row->att('ht');
        }

        $sheet->{DefRowHeight} = $default_row_height;
        $sheet->{DefColWidth} = $default_column_width;
        $sheet->{RowHeight} = [
            map { defined $_ ? $_ : $default_row_height } @row_heights
        ];
        $sheet->{ColWidth} = [
            map { defined $_ ? $_ : $default_column_width } @column_widths
        ];
    }

    return $excel;
}

# 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, $type) = @_;

    $contents = XML::Entities::decode('all', $contents);

    if ($type eq 'number') {
        $contents = 0+$contents;
    }

    return $contents;
}

__PACKAGE__->meta->make_immutable;
no Moose;

1;