Cell.pm 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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::Cell;
  29. ###############################################################################
  30. #
  31. # Spreadsheet::ParseExcel::Cell - A class for Cell data and formatting.
  32. #
  33. # Used in conjunction with Spreadsheet::ParseExcel.
  34. #
  35. # Copyright (c) 2009 John McNamara
  36. # Copyright (c) 2006-2008 Gabor Szabo
  37. # Copyright (c) 2000-2006 Kawai Takanori
  38. #
  39. # perltidy with standard settings.
  40. #
  41. # Documentation after __END__
  42. #
  43. use strict;
  44. use warnings;
  45. our $VERSION = '0.57';
  46. ###############################################################################
  47. #
  48. # new()
  49. #
  50. # Constructor.
  51. #
  52. sub new {
  53. my ( $package, %properties ) = @_;
  54. my $self = \%properties;
  55. bless $self, $package;
  56. }
  57. ###############################################################################
  58. #
  59. # value()
  60. #
  61. # Returns the formatted value of the cell.
  62. #
  63. sub value {
  64. my $self = shift;
  65. return $self->{_Value};
  66. }
  67. ###############################################################################
  68. #
  69. # unformatted()
  70. #
  71. # Returns the unformatted value of the cell.
  72. #
  73. sub unformatted {
  74. my $self = shift;
  75. return $self->{Val};
  76. }
  77. ###############################################################################
  78. #
  79. # get_format()
  80. #
  81. # Returns the Format object for the cell.
  82. #
  83. sub get_format {
  84. my $self = shift;
  85. return $self->{Format};
  86. }
  87. ###############################################################################
  88. #
  89. # type()
  90. #
  91. # Returns the type of cell such as Text, Numeric or Date.
  92. #
  93. sub type {
  94. my $self = shift;
  95. return $self->{Type};
  96. }
  97. ###############################################################################
  98. #
  99. # encoding()
  100. #
  101. # Returns the character encoding of the cell.
  102. #
  103. sub encoding {
  104. my $self = shift;
  105. if ( !defined $self->{Code} ) {
  106. return 1;
  107. }
  108. elsif ( $self->{Code} eq 'ucs2' ) {
  109. return 2;
  110. }
  111. elsif ( $self->{Code} eq '_native_' ) {
  112. return 3;
  113. }
  114. else {
  115. return 0;
  116. }
  117. return $self->{Code};
  118. }
  119. ###############################################################################
  120. #
  121. # is_merged()
  122. #
  123. # Returns true if the cell is merged.
  124. #
  125. sub is_merged {
  126. my $self = shift;
  127. return $self->{Merged};
  128. }
  129. ###############################################################################
  130. #
  131. # get_rich_text()
  132. #
  133. # Returns an array ref of font information about each string block in a "rich",
  134. # i.e. multi-format, string.
  135. #
  136. sub get_rich_text {
  137. my $self = shift;
  138. return $self->{Rich};
  139. }
  140. ###############################################################################
  141. #
  142. # Mapping between legacy method names and new names.
  143. #
  144. {
  145. no warnings; # Ignore warnings about variables used only once.
  146. *Value = *value;
  147. }
  148. 1;
  149. __END__
  150. =pod
  151. =head1 NAME
  152. Spreadsheet::ParseExcel::Cell - A class for Cell data and formatting.
  153. =head1 SYNOPSIS
  154. See the documentation for Spreadsheet::ParseExcel.
  155. =head1 DESCRIPTION
  156. This module is used in conjunction with Spreadsheet::ParseExcel. See the documentation for Spreadsheet::ParseExcel.
  157. =head1 Methods
  158. The following Cell methods are available:
  159. $cell->value()
  160. $cell->unformatted()
  161. $cell->get_format()
  162. $cell->type()
  163. $cell->encoding()
  164. $cell->is_merged()
  165. $cell->get_rich_text()
  166. =head2 value()
  167. The C<value()> method returns the formatted value of the cell.
  168. my $value = $cell->value();
  169. Formatted in this sense refers to the numeric format of the cell value. For example a number such as 40177 might be formatted as 40,117, 40117.000 or even as the date 2009/12/30.
  170. If the cell doesn't contain a numeric format then the formatted and unformatted cell values are the same, see the C<unformatted()> method below.
  171. For a defined C<$cell> the C<value()> method will always return a value.
  172. In the case of a cell with formatting but no numeric or string contents the method will return the empty string C<''>.
  173. =head2 unformatted()
  174. The C<unformatted()> method returns the unformatted value of the cell.
  175. my $unformatted = $cell->unformatted();
  176. Returns the cell value without a numeric format. See the C<value()> method above.
  177. =head2 get_format()
  178. The C<get_format()> method returns the L<Spreadsheet::ParseExcel::Format> object for the cell.
  179. my $format = $cell->get_format();
  180. If a user defined format hasn't been applied to the cell then the default cell format is returned.
  181. =head2 type()
  182. The C<type()> method returns the type of cell such as Text, Numeric or Date. If the type was detected as Numeric, and the Cell Format matches C<m{^[dmy][-\\/dmy]*$}i>, it will be treated as a Date type.
  183. my $type = $cell->type();
  184. See also L<Dates and Time in Excel>.
  185. =head2 encoding()
  186. The C<encoding()> method returns the character encoding of the cell.
  187. my $encoding = $cell->encoding();
  188. This method is only of interest to developers. In general Spreadsheet::ParseExcel will return all character strings in UTF-8 regardless of the encoding used by Excel.
  189. The C<encoding()> method returns one of the following values:
  190. =over
  191. =item * 0: Unknown format. This shouldn't happen. In the default case the format should be 1.
  192. =item * 1: 8bit ASCII or single byte UTF-16. This indicates that the characters are encoded in a single byte. In Excel 95 and earlier This usually meant ASCII or an international variant. In Excel 97 it refers to a compressed UTF-16 character string where all of the high order bytes are 0 and are omitted to save space.
  193. =item * 2: UTF-16BE.
  194. =item * 3: Native encoding. In Excel 95 and earlier this encoding was used to represent multi-byte character encodings such as SJIS.
  195. =back
  196. =head2 is_merged()
  197. The C<is_merged()> method returns true if the cell is merged.
  198. my $is_merged = $cell->is_merged();
  199. Returns C<undef> if the property isn't set.
  200. =head2 get_rich_text()
  201. The C<get_rich_text()> method returns an array ref of font information about each string block in a "rich", i.e. multi-format, string.
  202. my $rich_text = $cell->get_rich_text();
  203. The return value is an arrayref of arrayrefs in the form:
  204. [
  205. [ $start_position, $font_object ],
  206. ...,
  207. ]
  208. Returns undef if the property isn't set.
  209. =head1 Dates and Time in Excel
  210. Dates and times in Excel are represented by real numbers, for example "Jan 1 2001 12:30 PM" is represented by the number 36892.521.
  211. The integer part of the number stores the number of days since the epoch and the fractional part stores the percentage of the day.
  212. A date or time in Excel is just like any other number. The way in which it is displayed is controlled by the number format:
  213. Number format $cell->value() $cell->unformatted()
  214. ============= ============== ==============
  215. 'dd/mm/yy' '28/02/08' 39506.5
  216. 'mm/dd/yy' '02/28/08' 39506.5
  217. 'd-m-yyyy' '28-2-2008' 39506.5
  218. 'dd/mm/yy hh:mm' '28/02/08 12:00' 39506.5
  219. 'd mmm yyyy' '28 Feb 2008' 39506.5
  220. 'mmm d yyyy hh:mm AM/PM' 'Feb 28 2008 12:00 PM' 39506.5
  221. The L<Spreadsheet::ParseExcel::Utility> module contains a function called C<ExcelLocaltime> which will convert between an unformatted Excel date/time number and a C<localtime()> like array.
  222. For date conversions using the CPAN C<DateTime> framework see L<DateTime::Format::Excel> http://search.cpan.org/search?dist=DateTime-Format-Excel
  223. =head1 AUTHOR
  224. Maintainer 0.40+: John McNamara jmcnamara@cpan.org
  225. Maintainer 0.27-0.33: Gabor Szabo szabgab@cpan.org
  226. Original author: Kawai Takanori kwitknr@cpan.org
  227. =head1 COPYRIGHT
  228. Copyright (c) 2009-2010 John McNamara
  229. Copyright (c) 2006-2008 Gabor Szabo
  230. Copyright (c) 2000-2006 Kawai Takanori
  231. All rights reserved.
  232. You may distribute under the terms of either the GNU General Public License or the Artistic License, as specified in the Perl README file.
  233. =cut