blob: 416866f67909f4e624f6eea33ff5111ee3eb5a2d [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001package Spreadsheet::ParseExcel::FmtDefault;
2
3###############################################################################
4#
5# Spreadsheet::ParseExcel::FmtDefault - A class for Cell formats.
6#
7# Used in conjunction with Spreadsheet::ParseExcel.
8#
9# Copyright (c) 2009 John McNamara
10# Copyright (c) 2006-2008 Gabor Szabo
11# Copyright (c) 2000-2006 Kawai Takanori
12#
13# perltidy with standard settings.
14#
15# Documentation after __END__
16#
17
18use strict;
19use warnings;
20
21use Spreadsheet::ParseExcel::Utility qw(ExcelFmt);
22our $VERSION = '0.59';
23
24my %hFmtDefault = (
25 0x00 => '@',
26 0x01 => '0',
27 0x02 => '0.00',
28 0x03 => '#,##0',
29 0x04 => '#,##0.00',
30 0x05 => '($#,##0_);($#,##0)',
31 0x06 => '($#,##0_);[RED]($#,##0)',
32 0x07 => '($#,##0.00_);($#,##0.00_)',
33 0x08 => '($#,##0.00_);[RED]($#,##0.00_)',
34 0x09 => '0%',
35 0x0A => '0.00%',
36 0x0B => '0.00E+00',
37 0x0C => '# ?/?',
38 0x0D => '# ??/??',
39 0x0E => 'yyyy-mm-dd', # Was 'm-d-yy', which is bad as system default
40 0x0F => 'd-mmm-yy',
41 0x10 => 'd-mmm',
42 0x11 => 'mmm-yy',
43 0x12 => 'h:mm AM/PM',
44 0x13 => 'h:mm:ss AM/PM',
45 0x14 => 'h:mm',
46 0x15 => 'h:mm:ss',
47 0x16 => 'm-d-yy h:mm',
48
49 #0x17-0x24 -- Differs in Natinal
50 0x25 => '(#,##0_);(#,##0)',
51 0x26 => '(#,##0_);[RED](#,##0)',
52 0x27 => '(#,##0.00);(#,##0.00)',
53 0x28 => '(#,##0.00);[RED](#,##0.00)',
54 0x29 => '_(*#,##0_);_(*(#,##0);_(*"-"_);_(@_)',
55 0x2A => '_($*#,##0_);_($*(#,##0);_(*"-"_);_(@_)',
56 0x2B => '_(*#,##0.00_);_(*(#,##0.00);_(*"-"??_);_(@_)',
57 0x2C => '_($*#,##0.00_);_($*(#,##0.00);_(*"-"??_);_(@_)',
58 0x2D => 'mm:ss',
59 0x2E => '[h]:mm:ss',
60 0x2F => 'mm:ss.0',
61 0x30 => '##0.0E+0',
62 0x31 => '@',
63);
64
65#------------------------------------------------------------------------------
66# new (for Spreadsheet::ParseExcel::FmtDefault)
67#------------------------------------------------------------------------------
68sub new {
69 my ( $sPkg, %hKey ) = @_;
70 my $oThis = {};
71 bless $oThis;
72 return $oThis;
73}
74
75#------------------------------------------------------------------------------
76# TextFmt (for Spreadsheet::ParseExcel::FmtDefault)
77#------------------------------------------------------------------------------
78sub TextFmt {
79 my ( $oThis, $sTxt, $sCode ) = @_;
80 return $sTxt if ( ( !defined($sCode) ) || ( $sCode eq '_native_' ) );
81 return pack( 'U*', unpack( 'n*', $sTxt ) );
82}
83
84#------------------------------------------------------------------------------
85# FmtStringDef (for Spreadsheet::ParseExcel::FmtDefault)
86#------------------------------------------------------------------------------
87sub FmtStringDef {
88 my ( $oThis, $iFmtIdx, $oBook, $rhFmt ) = @_;
89 my $sFmtStr = $oBook->{FormatStr}->{$iFmtIdx};
90
91 if ( !( defined($sFmtStr) ) && defined($rhFmt) ) {
92 $sFmtStr = $rhFmt->{$iFmtIdx};
93 }
94 $sFmtStr = $hFmtDefault{$iFmtIdx} unless ($sFmtStr);
95 return $sFmtStr;
96}
97
98#------------------------------------------------------------------------------
99# FmtString (for Spreadsheet::ParseExcel::FmtDefault)
100#------------------------------------------------------------------------------
101sub FmtString {
102 my ( $oThis, $oCell, $oBook ) = @_;
103
104 my $sFmtStr =
105 $oThis->FmtStringDef( $oBook->{Format}[ $oCell->{FormatNo} ]->{FmtIdx},
106 $oBook );
107
108 # Special case for cells that use Lotus123 style leading
109 # apostrophe to designate text formatting.
110 if ( $oBook->{Format}[ $oCell->{FormatNo} ]->{Key123} ) {
111 $sFmtStr = '@';
112 }
113
114 unless ( defined($sFmtStr) ) {
115 if ( $oCell->{Type} eq 'Numeric' ) {
116 if ( int( $oCell->{Val} ) != $oCell->{Val} ) {
117 $sFmtStr = '0.00';
118 }
119 else {
120 $sFmtStr = '0';
121 }
122 }
123 elsif ( $oCell->{Type} eq 'Date' ) {
124 if ( int( $oCell->{Val} ) <= 0 ) {
125 $sFmtStr = 'h:mm:ss';
126 }
127 else {
128 $sFmtStr = 'yyyy-mm-dd';
129 }
130 }
131 else {
132 $sFmtStr = '@';
133 }
134 }
135 return $sFmtStr;
136}
137
138#------------------------------------------------------------------------------
139# ValFmt (for Spreadsheet::ParseExcel::FmtDefault)
140#------------------------------------------------------------------------------
141sub ValFmt {
142 my ( $oThis, $oCell, $oBook ) = @_;
143
144 my ( $Dt, $iFmtIdx, $iNumeric, $Flg1904 );
145
146 if ( $oCell->{Type} eq 'Text' ) {
147 $Dt =
148 ( ( defined $oCell->{Val} ) && ( $oCell->{Val} ne '' ) )
149 ? $oThis->TextFmt( $oCell->{Val}, $oCell->{Code} )
150 : '';
151
152 return $Dt;
153 }
154 else {
155 $Dt = $oCell->{Val};
156 $Flg1904 = $oBook->{Flg1904};
157 my $sFmtStr = $oThis->FmtString( $oCell, $oBook );
158
159 return ExcelFmt( $sFmtStr, $Dt, $Flg1904, $oCell->{Type} );
160 }
161}
162
163#------------------------------------------------------------------------------
164# ChkType (for Spreadsheet::ParseExcel::FmtDefault)
165#------------------------------------------------------------------------------
166sub ChkType {
167 my ( $oPkg, $iNumeric, $iFmtIdx ) = @_;
168 if ($iNumeric) {
169 if ( ( ( $iFmtIdx >= 0x0E ) && ( $iFmtIdx <= 0x16 ) )
170 || ( ( $iFmtIdx >= 0x2D ) && ( $iFmtIdx <= 0x2F ) ) )
171 {
172 return "Date";
173 }
174 else {
175 return "Numeric";
176 }
177 }
178 else {
179 return "Text";
180 }
181}
182
1831;
184
185__END__
186
187=pod
188
189=head1 NAME
190
191Spreadsheet::ParseExcel::FmtDefault - A class for Cell formats.
192
193=head1 SYNOPSIS
194
195See the documentation for Spreadsheet::ParseExcel.
196
197=head1 DESCRIPTION
198
199This module is used in conjunction with Spreadsheet::ParseExcel. See the documentation for Spreadsheet::ParseExcel.
200
201=head1 AUTHOR
202
203Maintainer 0.40+: John McNamara jmcnamara@cpan.org
204
205Maintainer 0.27-0.33: Gabor Szabo szabgab@cpan.org
206
207Original author: Kawai Takanori kwitknr@cpan.org
208
209=head1 COPYRIGHT
210
211Copyright (c) 2009-2010 John McNamara
212
213Copyright (c) 2006-2008 Gabor Szabo
214
215Copyright (c) 2000-2006 Kawai Takanori
216
217All rights reserved.
218
219You may distribute under the terms of either the GNU General Public License or the Artistic License, as specified in the Perl README file.
220
221=cut