AdvancedValueBinder.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. /** PHPExcel root directory */
  3. if (!defined('PHPEXCEL_ROOT')) {
  4. /**
  5. * @ignore
  6. */
  7. define('PHPEXCEL_ROOT', dirname(__FILE__) . '/../../');
  8. require(PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php');
  9. }
  10. /**
  11. * PHPExcel_Cell_AdvancedValueBinder
  12. *
  13. * Copyright (c) 2006 - 2015 PHPExcel
  14. *
  15. * This library is free software; you can redistribute it and/or
  16. * modify it under the terms of the GNU Lesser General Public
  17. * License as published by the Free Software Foundation; either
  18. * version 2.1 of the License, or (at your option) any later version.
  19. *
  20. * This library is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  23. * Lesser General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Lesser General Public
  26. * License along with this library; if not, write to the Free Software
  27. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  28. *
  29. * @category PHPExcel
  30. * @package PHPExcel_Cell
  31. * @copyright Copyright (c) 2006 - 2015 PHPExcel (http://www.codeplex.com/PHPExcel)
  32. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  33. * @version ##VERSION##, ##DATE##
  34. */
  35. class PHPExcel_Cell_AdvancedValueBinder extends PHPExcel_Cell_DefaultValueBinder implements PHPExcel_Cell_IValueBinder
  36. {
  37. /**
  38. * Bind value to a cell
  39. *
  40. * @param PHPExcel_Cell $cell Cell to bind value to
  41. * @param mixed $value Value to bind in cell
  42. * @return boolean
  43. */
  44. public function bindValue(PHPExcel_Cell $cell, $value = null)
  45. {
  46. // sanitize UTF-8 strings
  47. if (is_string($value)) {
  48. $value = PHPExcel_Shared_String::SanitizeUTF8($value);
  49. }
  50. // Find out data type
  51. $dataType = parent::dataTypeForValue($value);
  52. // Style logic - strings
  53. if ($dataType === PHPExcel_Cell_DataType::TYPE_STRING && !$value instanceof PHPExcel_RichText) {
  54. // Test for booleans using locale-setting
  55. if ($value == PHPExcel_Calculation::getTRUE()) {
  56. $cell->setValueExplicit(true, PHPExcel_Cell_DataType::TYPE_BOOL);
  57. return true;
  58. } elseif ($value == PHPExcel_Calculation::getFALSE()) {
  59. $cell->setValueExplicit(false, PHPExcel_Cell_DataType::TYPE_BOOL);
  60. return true;
  61. }
  62. // Check for number in scientific format
  63. if (preg_match('/^'.PHPExcel_Calculation::CALCULATION_REGEXP_NUMBER.'$/', $value)) {
  64. $cell->setValueExplicit((float) $value, PHPExcel_Cell_DataType::TYPE_NUMERIC);
  65. return true;
  66. }
  67. // Check for fraction
  68. if (preg_match('/^([+-]?)\s*([0-9]+)\s?\/\s*([0-9]+)$/', $value, $matches)) {
  69. // Convert value to number
  70. $value = $matches[2] / $matches[3];
  71. if ($matches[1] == '-') {
  72. $value = 0 - $value;
  73. }
  74. $cell->setValueExplicit((float) $value, PHPExcel_Cell_DataType::TYPE_NUMERIC);
  75. // Set style
  76. $cell->getWorksheet()->getStyle($cell->getCoordinate())
  77. ->getNumberFormat()->setFormatCode('??/??');
  78. return true;
  79. } elseif (preg_match('/^([+-]?)([0-9]*) +([0-9]*)\s?\/\s*([0-9]*)$/', $value, $matches)) {
  80. // Convert value to number
  81. $value = $matches[2] + ($matches[3] / $matches[4]);
  82. if ($matches[1] == '-') {
  83. $value = 0 - $value;
  84. }
  85. $cell->setValueExplicit((float) $value, PHPExcel_Cell_DataType::TYPE_NUMERIC);
  86. // Set style
  87. $cell->getWorksheet()->getStyle($cell->getCoordinate())
  88. ->getNumberFormat()->setFormatCode('# ??/??');
  89. return true;
  90. }
  91. // Check for percentage
  92. if (preg_match('/^\-?[0-9]*\.?[0-9]*\s?\%$/', $value)) {
  93. // Convert value to number
  94. $value = (float) str_replace('%', '', $value) / 100;
  95. $cell->setValueExplicit($value, PHPExcel_Cell_DataType::TYPE_NUMERIC);
  96. // Set style
  97. $cell->getWorksheet()->getStyle($cell->getCoordinate())
  98. ->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_PERCENTAGE_00);
  99. return true;
  100. }
  101. // Check for currency
  102. $currencyCode = PHPExcel_Shared_String::getCurrencyCode();
  103. $decimalSeparator = PHPExcel_Shared_String::getDecimalSeparator();
  104. $thousandsSeparator = PHPExcel_Shared_String::getThousandsSeparator();
  105. if (preg_match('/^'.preg_quote($currencyCode).' *(\d{1,3}('.preg_quote($thousandsSeparator).'\d{3})*|(\d+))('.preg_quote($decimalSeparator).'\d{2})?$/', $value)) {
  106. // Convert value to number
  107. $value = (float) trim(str_replace(array($currencyCode, $thousandsSeparator, $decimalSeparator), array('', '', '.'), $value));
  108. $cell->setValueExplicit($value, PHPExcel_Cell_DataType::TYPE_NUMERIC);
  109. // Set style
  110. $cell->getWorksheet()->getStyle($cell->getCoordinate())
  111. ->getNumberFormat()->setFormatCode(
  112. str_replace('$', $currencyCode, PHPExcel_Style_NumberFormat::FORMAT_CURRENCY_USD_SIMPLE)
  113. );
  114. return true;
  115. } elseif (preg_match('/^\$ *(\d{1,3}(\,\d{3})*|(\d+))(\.\d{2})?$/', $value)) {
  116. // Convert value to number
  117. $value = (float) trim(str_replace(array('$',','), '', $value));
  118. $cell->setValueExplicit($value, PHPExcel_Cell_DataType::TYPE_NUMERIC);
  119. // Set style
  120. $cell->getWorksheet()->getStyle($cell->getCoordinate())
  121. ->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_CURRENCY_USD_SIMPLE);
  122. return true;
  123. }
  124. // Check for time without seconds e.g. '9:45', '09:45'
  125. if (preg_match('/^(\d|[0-1]\d|2[0-3]):[0-5]\d$/', $value)) {
  126. // Convert value to number
  127. list($h, $m) = explode(':', $value);
  128. $days = $h / 24 + $m / 1440;
  129. $cell->setValueExplicit($days, PHPExcel_Cell_DataType::TYPE_NUMERIC);
  130. // Set style
  131. $cell->getWorksheet()->getStyle($cell->getCoordinate())
  132. ->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_DATE_TIME3);
  133. return true;
  134. }
  135. // Check for time with seconds '9:45:59', '09:45:59'
  136. if (preg_match('/^(\d|[0-1]\d|2[0-3]):[0-5]\d:[0-5]\d$/', $value)) {
  137. // Convert value to number
  138. list($h, $m, $s) = explode(':', $value);
  139. $days = $h / 24 + $m / 1440 + $s / 86400;
  140. // Convert value to number
  141. $cell->setValueExplicit($days, PHPExcel_Cell_DataType::TYPE_NUMERIC);
  142. // Set style
  143. $cell->getWorksheet()->getStyle($cell->getCoordinate())
  144. ->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_DATE_TIME4);
  145. return true;
  146. }
  147. // Check for datetime, e.g. '2008-12-31', '2008-12-31 15:59', '2008-12-31 15:59:10'
  148. if (($d = PHPExcel_Shared_Date::stringToExcel($value)) !== false) {
  149. // Convert value to number
  150. $cell->setValueExplicit($d, PHPExcel_Cell_DataType::TYPE_NUMERIC);
  151. // Determine style. Either there is a time part or not. Look for ':'
  152. if (strpos($value, ':') !== false) {
  153. $formatCode = 'yyyy-mm-dd h:mm';
  154. } else {
  155. $formatCode = 'yyyy-mm-dd';
  156. }
  157. $cell->getWorksheet()->getStyle($cell->getCoordinate())
  158. ->getNumberFormat()->setFormatCode($formatCode);
  159. return true;
  160. }
  161. // Check for newline character "\n"
  162. if (strpos($value, "\n") !== false) {
  163. $value = PHPExcel_Shared_String::SanitizeUTF8($value);
  164. $cell->setValueExplicit($value, PHPExcel_Cell_DataType::TYPE_STRING);
  165. // Set style
  166. $cell->getWorksheet()->getStyle($cell->getCoordinate())
  167. ->getAlignment()->setWrapText(true);
  168. return true;
  169. }
  170. }
  171. // Not bound yet? Use parent...
  172. return parent::bindValue($cell, $value);
  173. }
  174. }