FmtJapan.pm 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. # DISCLAIMER OF WARRANTY
  2. # Because this software is licensed free of charge, there is no warranty for the software,
  3. # to the extent permitted by applicable law. Except when otherwise stated in writing
  4. # the copyright holders and/or other parties provide the software "as is" without
  5. # warranty of any kind, either expressed or implied, including, but not limited to,
  6. # the implied warranties of merchantability and fitness for a particular purpose.
  7. # The entire risk as to the quality and performance of the software is with you.
  8. # Should the software prove defective, you assume the cost of all necessary
  9. # servicing, repair, or correction.
  10. # In no event unless required by applicable law or agreed to in writing will any
  11. # copyright holder, or any other party who may modify and/or redistribute the software
  12. # as permitted by the above licence, be liable to you for damages, including any general,
  13. # special, incidental, or consequential damages arising out of the use or inability
  14. # to use the software (including but not limited to loss of data or data being rendered
  15. # inaccurate or losses sustained by you or third parties or a failure of the software
  16. # to operate with any other software), even if such holder or other party
  17. # has been advised of the possibility of such damages.
  18. # AUTHOR
  19. # Current maintainer 0.40+: John McNamara jmcnamara@cpan.org
  20. # Maintainer 0.27-0.33: Gabor Szabo szabgab@cpan.org
  21. # Original author: Kawai Takanori (Hippo2000) kwitknr@cpan.org
  22. # COPYRIGHT
  23. # Copyright (c) 2009-2010 John McNamara
  24. # Copyright (c) 2006-2008 Gabor Szabo
  25. # Copyright (c) 2000-2006 Kawai Takanori
  26. # All rights reserved. This is free software. You may distribute under the terms of
  27. # the Artistic License(full text of the Artistic License http://dev.perl.org/licenses/artistic.html).
  28. package Spreadsheet::ParseExcel::FmtJapan;
  29. use utf8;
  30. ###############################################################################
  31. #
  32. # Spreadsheet::ParseExcel::FmtJapan - A class for Cell formats.
  33. #
  34. # Used in conjunction with Spreadsheet::ParseExcel.
  35. #
  36. # Copyright (c) 2009 John McNamara
  37. # Copyright (c) 2006-2008 Gabor Szabo
  38. # Copyright (c) 2000-2006 Kawai Takanori
  39. #
  40. # perltidy with standard settings.
  41. #
  42. # Documentation after __END__
  43. #
  44. use strict;
  45. use warnings;
  46. use Encode qw(find_encoding decode);
  47. use base 'Spreadsheet::ParseExcel::FmtDefault';
  48. our $VERSION = '0.57';
  49. my %FormatTable = (
  50. 0x00 => '@',
  51. 0x01 => '0',
  52. 0x02 => '0.00',
  53. 0x03 => '#,##0',
  54. 0x04 => '#,##0.00',
  55. 0x05 => '(\\#,##0_);(\\#,##0)',
  56. 0x06 => '(\\#,##0_);[RED](\\#,##0)',
  57. 0x07 => '(\\#,##0.00_);(\\#,##0.00_)',
  58. 0x08 => '(\\#,##0.00_);[RED](\\#,##0.00_)',
  59. 0x09 => '0%',
  60. 0x0A => '0.00%',
  61. 0x0B => '0.00E+00',
  62. 0x0C => '# ?/?',
  63. 0x0D => '# ??/??',
  64. # 0x0E => 'm/d/yy',
  65. 0x0E => 'yyyy/m/d',
  66. 0x0F => 'd-mmm-yy',
  67. 0x10 => 'd-mmm',
  68. 0x11 => 'mmm-yy',
  69. 0x12 => 'h:mm AM/PM',
  70. 0x13 => 'h:mm:ss AM/PM',
  71. 0x14 => 'h:mm',
  72. 0x15 => 'h:mm:ss',
  73. # 0x16 => 'm/d/yy h:mm',
  74. 0x16 => 'yyyy/m/d h:mm',
  75. #0x17-0x24 -- Differs in Natinal
  76. 0x1E => 'm/d/yy',
  77. 0x1F => 'yyyy"年"m"月"d"日"',
  78. 0x20 => 'h"時"mm"分"',
  79. 0x21 => 'h"時"mm"分"ss"秒"',
  80. #0x17-0x24 -- Differs in Natinal
  81. 0x25 => '(#,##0_);(#,##0)',
  82. 0x26 => '(#,##0_);[RED](#,##0)',
  83. 0x27 => '(#,##0.00);(#,##0.00)',
  84. 0x28 => '(#,##0.00);[RED](#,##0.00)',
  85. 0x29 => '_(*#,##0_);_(*(#,##0);_(*"-"_);_(@_)',
  86. 0x2A => '_(\\*#,##0_);_(\\*(#,##0);_(*"-"_);_(@_)',
  87. 0x2B => '_(*#,##0.00_);_(*(#,##0.00);_(*"-"??_);_(@_)',
  88. 0x2C => '_(\\*#,##0.00_);_(\\*(#,##0.00);_(*"-"??_);_(@_)',
  89. 0x2D => 'mm:ss',
  90. 0x2E => '[h]:mm:ss',
  91. 0x2F => 'mm:ss.0',
  92. 0x30 => '##0.0E+0',
  93. 0x31 => '@',
  94. 0x37 => 'yyyy"年"m"月"',
  95. 0x38 => 'm"月"d"日"',
  96. 0x39 => 'ge.m.d',
  97. 0x3A => 'ggge"年"m"月"d"日"',
  98. );
  99. #------------------------------------------------------------------------------
  100. # new (for Spreadsheet::ParseExcel::FmtJapan)
  101. #------------------------------------------------------------------------------
  102. sub new {
  103. my ( $class, %args ) = @_;
  104. my $encoding = $args{Code} || $args{encoding};
  105. my $self = { Code => $encoding };
  106. if($encoding){
  107. $self->{encoding} = find_encoding($encoding eq 'sjis' ? 'cp932' : $encoding)
  108. or do{
  109. require Carp;
  110. Carp::croak(qq{Unknown encoding '$encoding'});
  111. };
  112. }
  113. return bless $self, $class;
  114. }
  115. #------------------------------------------------------------------------------
  116. # TextFmt (for Spreadsheet::ParseExcel::FmtJapan)
  117. #------------------------------------------------------------------------------
  118. sub TextFmt {
  119. my ( $self, $text, $input_encoding ) = @_;
  120. if(!defined $input_encoding){
  121. $input_encoding = 'utf8';
  122. }
  123. elsif($input_encoding eq '_native_'){
  124. $input_encoding = 'cp932'; # Shift_JIS in Microsoft products
  125. }
  126. $text = decode($input_encoding, $text);
  127. return $self->{Code} ? $self->{encoding}->encode($text) : $text;
  128. }
  129. #------------------------------------------------------------------------------
  130. # FmtStringDef (for Spreadsheet::ParseExcel::FmtJapan)
  131. #------------------------------------------------------------------------------
  132. sub FmtStringDef {
  133. my ( $self, $format_index, $book ) = @_;
  134. return $self->SUPER::FmtStringDef( $format_index, $book, \%FormatTable );
  135. }
  136. #------------------------------------------------------------------------------
  137. # CnvNengo (for Spreadsheet::ParseExcel::FmtJapan)
  138. #------------------------------------------------------------------------------
  139. # Convert A.D. into Japanese Nengo (aka Gengo)
  140. my @Nengo = (
  141. {
  142. name => '平成', # Heisei
  143. abbr_name => 'H',
  144. base => 1988,
  145. start => 19890108,
  146. },
  147. {
  148. name => '昭和', # Showa
  149. abbr_name => 'S',
  150. base => 1925,
  151. start => 19261225,
  152. },
  153. {
  154. name => '大正', # Taisho
  155. abbr_name => 'T',
  156. base => 1911,
  157. start => 19120730,
  158. },
  159. {
  160. name => '明治', # Meiji
  161. abbr_name => 'M',
  162. base => 1867,
  163. start => 18680908,
  164. },
  165. );
  166. # Usage: CnvNengo(name => @tm) or CnvNeng(abbr_name => @tm)
  167. sub CnvNengo {
  168. my ( $kind, @tm ) = @_;
  169. my $year = $tm[5] + 1900;
  170. my $wk = ($year * 10000) + ($tm[4] * 100) + ($tm[3] * 1);
  171. #my $wk = sprintf( '%04d%02d%02d', $year, $tm[4], $tm[3] );
  172. foreach my $nengo(@Nengo){
  173. if( $wk >= $nengo->{start} ){
  174. return $nengo->{$kind} . ($year - $nengo->{base});
  175. }
  176. }
  177. return $year;
  178. }
  179. 1;
  180. __END__
  181. =pod
  182. =head1 NAME
  183. Spreadsheet::ParseExcel::FmtJapan - A class for Cell formats.
  184. =head1 SYNOPSIS
  185. See the documentation for Spreadsheet::ParseExcel.
  186. =head1 DESCRIPTION
  187. This module is used in conjunction with Spreadsheet::ParseExcel. See the documentation for Spreadsheet::ParseExcel.
  188. =head1 AUTHOR
  189. Maintainer 0.40+: John McNamara jmcnamara@cpan.org
  190. Maintainer 0.27-0.33: Gabor Szabo szabgab@cpan.org
  191. Original author: Kawai Takanori kwitknr@cpan.org
  192. =head1 COPYRIGHT
  193. Copyright (c) 2009-2010 John McNamara
  194. Copyright (c) 2006-2008 Gabor Szabo
  195. Copyright (c) 2000-2006 Kawai Takanori
  196. All rights reserved.
  197. You may distribute under the terms of either the GNU General Public License or the Artistic License, as specified in the Perl README file.
  198. =cut