| 1 | package Data::XLSX::Parser::Styles; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | use XML::Parser::Expat; | ||
| 6 | use Archive::Zip (); | ||
| 7 | use File::Temp; | ||
| 8 | use Carp; | ||
| 9 | |||
| 10 | use constant BUILTIN_FMT => 0; | ||
| 11 | use constant BUILTIN_TYPE => 1; | ||
| 12 | |||
| 13 | use constant BUILTIN_NUM_FMTS => [ | ||
| 14 | ['@', 'unicode'], # 0x00 | ||
| 15 | ['0', 'int'], # 0x01 | ||
| 16 | ['0.00', 'float'], # 0x02 | ||
| 17 | ['#,##0', 'float'], # 0x03 | ||
| 18 | ['#,##0.00', 'float'], # 0x04 | ||
| 19 | ['($#,##0_);($#,##0)', 'float'], # 0x05 | ||
| 20 | ['($#,##0_);[RED]($#,##0)', 'float'], # 0x06 | ||
| 21 | ['($#,##0.00_);($#,##0.00_)', 'float'], # 0x07 | ||
| 22 | ['($#,##0.00_);[RED]($#,##0.00_)', 'float'], # 0x08 | ||
| 23 | ['0%', 'int'], # 0x09 | ||
| 24 | ['0.00%', 'float'], # 0x0a | ||
| 25 | ['0.00E+00', 'float'], # 0x0b | ||
| 26 | ['# ?/?', 'float'], # 0x0c | ||
| 27 | ['# ??/??', 'float'], # 0x0d | ||
| 28 | ['m-d-yy', 'datetime.date'], # 0x0e | ||
| 29 | ['d-mmm-yy', 'datetime.date'], # 0x0f | ||
| 30 | ['d-mmm', 'datetime.date'], # 0x10 | ||
| 31 | ['mmm-yy', 'datetime.date'], # 0x11 | ||
| 32 | ['h:mm AM/PM', 'datetime.time'], # 0x12 | ||
| 33 | ['h:mm:ss AM/PM', 'datetime.time'], # 0x13 | ||
| 34 | ['h:mm', 'datetime.time'], # 0x14 | ||
| 35 | ['h:mm:ss', 'datetime.time'], # 0x15 | ||
| 36 | ['m-d-yy h:mm', 'datetime.datetime'], # 0x16 | ||
| 37 | #0x17-0x24 -- Differs in Natinal | ||
| 38 | undef, # 0x17 | ||
| 39 | undef, # 0x18 | ||
| 40 | undef, # 0x19 | ||
| 41 | undef, # 0x1a | ||
| 42 | undef, # 0x1b | ||
| 43 | undef, # 0x1c | ||
| 44 | undef, # 0x1d | ||
| 45 | undef, # 0x1e | ||
| 46 | undef, # 0x1f | ||
| 47 | undef, # 0x20 | ||
| 48 | undef, # 0x21 | ||
| 49 | undef, # 0x22 | ||
| 50 | undef, # 0x23 | ||
| 51 | undef, # 0x24 | ||
| 52 | ['(#,##0_);(#,##0)', 'int'], # 0x25 | ||
| 53 | ['(#,##0_);[RED](#,##0)', 'int'], # 0x26 | ||
| 54 | ['(#,##0.00);(#,##0.00)', 'float'], # 0x27 | ||
| 55 | ['(#,##0.00);[RED](#,##0.00)', 'float'], # 0x28 | ||
| 56 | ['_(*#,##0_);_(*(#,##0);_(*"-"_);_(@_)', 'float'], # 0x29 | ||
| 57 | ['_($*#,##0_);_($*(#,##0);_(*"-"_);_(@_)', 'float'], # 0x2a | ||
| 58 | ['_(*#,##0.00_);_(*(#,##0.00);_(*"-"??_);_(@_)', 'float'], # 0x2b | ||
| 59 | ['_($*#,##0.00_);_($*(#,##0.00);_(*"-"??_);_(@_)', 'float'], # 0x2c | ||
| 60 | ['mm:ss', 'datetime.timedelta'], # 0x2d | ||
| 61 | ['[h]:mm:ss', 'datetime.timedelta'], # 0x2e | ||
| 62 | ['mm:ss.0', 'datetime.timedelta'], # 0x2f | ||
| 63 | ['##0.0E+0', 'float'], # 0x30 | ||
| 64 | ['@', 'unicode'], # 0x31 | ||
| 65 | ]; | ||
| 66 | |||
| 67 | sub new { | ||
| 68 | my ($class, $archive) = @_; | ||
| 69 | |||
| 70 | my $self = bless { | ||
| 71 | _number_formats => [], | ||
| 72 | |||
| 73 | _is_cell_xfs => 0, | ||
| 74 | _current_style => undef, | ||
| 75 | }, $class; | ||
| 76 | |||
| 77 | my $fh = File::Temp->new( SUFFIX => '.xml' ) or confess "couldn't create temporary file: $!"; | ||
| 78 | |||
| 79 | my $handle = $archive->styles or confess "couldn't get handle to styles archive: $!"; | ||
| 80 | confess 'Failed to write temporary file: ', $fh->filename | ||
| 81 | unless $handle->extractToFileNamed($fh->filename) == Archive::Zip::AZ_OK; | ||
| 82 | |||
| 83 | my $parser = XML::Parser::Expat->new(Namespaces=>1); | ||
| 84 | $parser->setHandlers( | ||
| 85 | Start => sub { $self->_start(@_) }, | ||
| 86 | End => sub { $self->_end(@_) }, | ||
| 87 | Char => sub { }, | ||
| 88 | ); | ||
| 89 | $parser->parse($fh); | ||
| 90 | |||
| 91 | $self; | ||
| 92 | } | ||
| 93 | |||
| 94 | sub cell_style { | ||
| 95 | my ($self, $style_id) = @_; | ||
| 96 | $style_id ||= 0; | ||
| 97 | $self->{_number_formats}[int $style_id]; | ||
| 98 | } | ||
| 99 | |||
| 100 | sub cell_type_from_style { | ||
| 101 | my ($self, $style) = @_; | ||
| 102 | |||
| 103 | if ($style->{numFmt} > scalar @{ BUILTIN_NUM_FMTS() }) { | ||
| 104 | return $self->{_num_fmt}{ $style->{numFmt} }{_type} // undef; | ||
| 105 | } | ||
| 106 | |||
| 107 | BUILTIN_NUM_FMTS->[ $style->{numFmt} ][BUILTIN_TYPE]; | ||
| 108 | } | ||
| 109 | |||
| 110 | sub cell_format_from_style { | ||
| 111 | my ($self, $style) = @_; | ||
| 112 | |||
| 113 | if ($style->{numFmt} > scalar @{ BUILTIN_NUM_FMTS() }) { | ||
| 114 | return $self->{_num_fmt}{ $style->{numFmt} }{formatCode} // undef; | ||
| 115 | } | ||
| 116 | |||
| 117 | BUILTIN_NUM_FMTS->[ $style->{numFmt} ][BUILTIN_FMT]; | ||
| 118 | } | ||
| 119 | |||
| 120 | sub _start { | ||
| 121 | my ($self, $parser, $name, %attrs) = @_; | ||
| 122 | |||
| 123 | if ($name eq 'cellXfs') { | ||
| 124 | $self->{_is_cell_xfs} = 1; | ||
| 125 | } elsif ($self->{_is_cell_xfs} and $name eq 'xf') { | ||
| 126 | $self->{_current_style} = { | ||
| 127 | numFmt => int($attrs{numFmtId}) || 0, | ||
| 128 | exists $attrs{fontId} ? ( font => $attrs{fontId} ) : (), | ||
| 129 | exists $attrs{fillId} ? ( fill => $attrs{fillId} ) : (), | ||
| 130 | exists $attrs{borderId} ? ( border => $attrs{borderId} ) : (), | ||
| 131 | exists $attrs{xfId} ? ( xf => $attrs{xfId} ) : (), | ||
| 132 | exists $attrs{applyFont} ? ( applyFont => $attrs{applyFont} ) : (), | ||
| 133 | exists $attrs{applyNumberFormat} ? ( applyNumFmt => $attrs{applyNumberFormat} ) : (), | ||
| 134 | }; | ||
| 135 | } elsif ($name eq 'numFmts') { | ||
| 136 | $self->{_is_num_fmts} = 1; | ||
| 137 | } elsif ($self->{_is_num_fmts} and $name eq 'numFmt'){ | ||
| 138 | $self->{_current_numfmt} = { | ||
| 139 | numFmtId => $attrs{numFmtId}, | ||
| 140 | exists $attrs{formatCode} ? ( | ||
| 141 | formatCode => $attrs{formatCode}, | ||
| 142 | _type => $self->_parse_format_code_type($attrs{formatCode}), | ||
| 143 | ) : (), | ||
| 144 | }; | ||
| 145 | } | ||
| 146 | } | ||
| 147 | |||
| 148 | sub _end { | ||
| 149 | my ($self, $parser, $name) = @_; | ||
| 150 | |||
| 151 | if ($name eq 'cellXfs') { | ||
| 152 | $self->{_is_cell_xfs} = 0; | ||
| 153 | } elsif ($self->{_current_style} and $name eq 'xf') { | ||
| 154 | push @{ $self->{_number_formats } }, delete $self->{_current_style}; | ||
| 155 | } elsif ($name eq 'numFmts') { | ||
| 156 | $self->{_is_num_fmts} = 0; | ||
| 157 | } elsif ($self->{_current_numfmt} and $name eq 'numFmt') { | ||
| 158 | my $id = $self->{_current_numfmt}{numFmtId}; | ||
| 159 | $self->{_num_fmt}{ $id } = delete $self->{_current_numfmt}; | ||
| 160 | } | ||
| 161 | } | ||
| 162 | |||
| 163 | sub _parse_format_code_type { | ||
| 164 | my ($self, $format_code) = @_; | ||
| 165 | |||
| 166 | my $type; | ||
| 167 | if ($format_code =~ /;/) { | ||
| 168 | $type = 'unicode'; | ||
| 169 | } elsif ($format_code =~ /(y|m|d|h|s)/) { | ||
| 170 | $type = 'datetime.'; | ||
| 171 | |||
| 172 | $type .= 'date' if $format_code =~ /(y|d)/; | ||
| 173 | $type .= 'time' if $format_code =~ /(h|s)/; | ||
| 174 | |||
| 175 | $type .= 'date' if $type eq 'datetime.'; # assume as date only specified 'm' | ||
| 176 | } else { | ||
| 177 | $type = 'unicode'; | ||
| 178 | } | ||
| 179 | |||
| 180 | return $type; | ||
| 181 | } | ||
| 182 | |||
| 183 | 1; | ||
| 184 | __END__ |