File Coverage

blib/lib/Spreadsheet/XLSX/Fmt2007.pm
Criterion Covered Total %
statement 36 51 70.5
branch 15 30 50.0
condition 2 18 11.1
subroutine 7 9 77.7
pod 0 6 0.0
total 60 114 52.6


line stmt bran cond sub pod time code
1             package Spreadsheet::XLSX::Fmt2007;
2 7     7   44 use strict;
  7         20  
  7         269  
3 7     7   36 use warnings;
  7         12  
  7         437  
4              
5 7     7   4063 use Spreadsheet::XLSX::Utility2007 qw(ExcelFmt);
  7         44  
  7         5251  
6             our $VERSION = '0.18'; #
7              
8             my %hFmtDefault = (
9             0x00 => '@',
10             0x01 => '0',
11             0x02 => '0.00',
12             0x03 => '#,##0',
13             0x04 => '#,##0.00',
14             0x05 => '($#,##0_);($#,##0)',
15             0x06 => '($#,##0_);[RED]($#,##0)',
16             0x07 => '($#,##0.00_);($#,##0.00_)',
17             0x08 => '($#,##0.00_);[RED]($#,##0.00_)',
18             0x09 => '0%',
19             0x0A => '0.00%',
20             0x0B => '0.00E+00',
21             0x0C => '# ?/?',
22             0x0D => '# ??/??',
23             0x0E => 'yyyy-mm-dd',
24             0x0F => 'd-mmm-yy',
25             0x10 => 'd-mmm',
26             0x11 => 'mmm-yy',
27             0x12 => 'h:mm AM/PM',
28             0x13 => 'h:mm:ss AM/PM',
29             0x14 => 'h:mm',
30             0x15 => 'h:mm:ss',
31             0x16 => 'm-d-yy h:mm',
32              
33             #0x17-0x24 -- Differs in Natinal
34             0x25 => '(#,##0_);(#,##0)',
35             0x26 => '(#,##0_);[RED](#,##0)',
36             0x27 => '(#,##0.00);(#,##0.00)',
37             0x28 => '(#,##0.00);[RED](#,##0.00)',
38             0x29 => '_(*#,##0_);_(*(#,##0);_(*"-"_);_(@_)',
39             0x2A => '_($*#,##0_);_($*(#,##0);_(*"-"_);_(@_)',
40             0x2B => '_(*#,##0.00_);_(*(#,##0.00);_(*"-"??_);_(@_)',
41             0x2C => '_($*#,##0.00_);_($*(#,##0.00);_(*"-"??_);_(@_)',
42             0x2D => 'mm:ss',
43             0x2E => '[h]:mm:ss',
44             0x2F => 'mm:ss.0',
45             0x30 => '##0.0E+0',
46             0x31 => '@',
47             );
48              
49             #------------------------------------------------------------------------------
50             # new (for Spreadsheet::XLSX::Fmt2007)
51             #------------------------------------------------------------------------------
52             sub new {
53 10     10 0 26 my ($sPkg, %hKey) = @_;
54 10         18 my $oThis = {};
55 10         22 bless $oThis;
56 10         29 return $oThis;
57             }
58              
59             #------------------------------------------------------------------------------
60             # TextFmt (for Spreadsheet::XLSX::Fmt2007)
61             #------------------------------------------------------------------------------
62             sub TextFmt {
63 836     836 0 2497 my ($oThis, $sTxt, $sCode) = @_;
64 836 50 33     3012 return $sTxt if ((!defined($sCode)) || ($sCode eq '_native_'));
65 0         0 return pack('U*', unpack('n*', $sTxt));
66             }
67              
68             #------------------------------------------------------------------------------
69             # FmtStringDef (for Spreadsheet::XLSX::Fmt2007)
70             #------------------------------------------------------------------------------
71             sub FmtStringDef {
72 0     0 0 0 my ($oThis, $iFmtIdx, $oBook, $rhFmt) = @_;
73 0         0 my $sFmtStr = $oBook->{FormatStr}->{$iFmtIdx};
74              
75 0 0 0     0 if (!(defined($sFmtStr)) && defined($rhFmt)) {
76 0         0 $sFmtStr = $rhFmt->{$iFmtIdx};
77             }
78 0 0       0 $sFmtStr = $hFmtDefault{$iFmtIdx} unless ($sFmtStr);
79 0         0 return $sFmtStr;
80             }
81              
82             #------------------------------------------------------------------------------
83             # FmtString (for Spreadsheet::XLSX::Fmt2007)
84             #------------------------------------------------------------------------------
85             sub FmtString {
86 1622     1622 0 3158 my ($oThis, $oCell, $oBook) = @_;
87              
88 1622         2371 my $sFmtStr; # = $oThis->FmtStringDef(
89              
90             # $oBook->{Format}[$oCell->{FormatNo}]->{FmtIdx}, $oBook);
91              
92             # Check for formula error before evaluating format
93 1622 50       4538 return '@' if ( $oCell->{Val} =~ m/^#/ );
94            
95 1622 50       3382 unless (defined($sFmtStr)) {
96 1622 100       3972 if ($oCell->{Type} eq 'Numeric') {
    100          
97 784 100       2645 if ($oCell->{Format}) {
    100          
98 395         857 $sFmtStr = $oCell->{Format};
99             } elsif (int($oCell->{Val}) != $oCell->{Val}) {
100 242         468 $sFmtStr = '0.00';
101             } else {
102 147         209 $sFmtStr = '0';
103             }
104             } elsif ($oCell->{Type} eq 'Date') {
105 2 50       6 if ($oCell->{Format}) {
    0          
106 2         5 $sFmtStr = $oCell->{Format};
107             } elsif (int($oCell->{Val}) <= 0) {
108 0         0 $sFmtStr = 'h:mm:ss';
109             } else {
110 0         0 $sFmtStr = 'm-d-yy';
111             }
112             } else {
113 836         1515 $sFmtStr = '@';
114             }
115             }
116 1622         3934 return $sFmtStr;
117             }
118              
119             #------------------------------------------------------------------------------
120             # ValFmt (for Spreadsheet::XLSX::Fmt2007)
121             #------------------------------------------------------------------------------
122             sub ValFmt {
123 1622     1622 0 3127 my ($oThis, $oCell, $oBook) = @_;
124              
125 1622         5127 my ($Dt, $iFmtIdx, $iNumeric, $Flg1904);
126              
127 1622 100       3894 if ($oCell->{Type} eq 'Text') {
128 836 50 33     4416 $Dt = ((defined $oCell->{Val}) && ($oCell->{Val} ne '')) ? $oThis->TextFmt($oCell->{Val}, $oCell->{Code}) : '';
129             } else {
130 786         1693 $Dt = $oCell->{Val};
131             }
132 1622         3260 $Flg1904 = $oBook->{Flg1904};
133 1622         3826 my $sFmtStr = $oThis->FmtString($oCell, $oBook);
134 1622         4689 return ExcelFmt($sFmtStr, $Dt, $Flg1904, $oCell->{Type});
135             }
136              
137             #------------------------------------------------------------------------------
138             # ChkType (for Spreadsheet::XLSX::Fmt2007)
139             #------------------------------------------------------------------------------
140             sub ChkType {
141 0     0 0   my ($oPkg, $iNumeric, $iFmtIdx) = @_;
142 0 0         if ($iNumeric) {
143 0 0 0       if ( (($iFmtIdx >= 0x0E) && ($iFmtIdx <= 0x16))
      0        
      0        
144             || (($iFmtIdx >= 0x2D) && ($iFmtIdx <= 0x2F))) {
145 0           return "Date";
146             } else {
147 0           return "Numeric";
148             }
149             } else {
150 0           return "Text";
151             }
152             }
153             1;
154              
155             __END__