0
0

Functions.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759
  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. /** MAX_VALUE */
  11. define('MAX_VALUE', 1.2e308);
  12. /** 2 / PI */
  13. define('M_2DIVPI', 0.63661977236758134307553505349006);
  14. /** MAX_ITERATIONS */
  15. define('MAX_ITERATIONS', 256);
  16. /** PRECISION */
  17. define('PRECISION', 8.88E-016);
  18. /**
  19. * PHPExcel_Calculation_Functions
  20. *
  21. * Copyright (c) 2006 - 2015 PHPExcel
  22. *
  23. * This library is free software; you can redistribute it and/or
  24. * modify it under the terms of the GNU Lesser General Public
  25. * License as published by the Free Software Foundation; either
  26. * version 2.1 of the License, or (at your option) any later version.
  27. *
  28. * This library is distributed in the hope that it will be useful,
  29. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  30. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  31. * Lesser General Public License for more details.
  32. *
  33. * You should have received a copy of the GNU Lesser General Public
  34. * License along with this library; if not, write to the Free Software
  35. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  36. *
  37. * @category PHPExcel
  38. * @package PHPExcel_Calculation
  39. * @copyright Copyright (c) 2006 - 2015 PHPExcel (http://www.codeplex.com/PHPExcel)
  40. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  41. * @version ##VERSION##, ##DATE##
  42. */
  43. class PHPExcel_Calculation_Functions
  44. {
  45. /** constants */
  46. const COMPATIBILITY_EXCEL = 'Excel';
  47. const COMPATIBILITY_GNUMERIC = 'Gnumeric';
  48. const COMPATIBILITY_OPENOFFICE = 'OpenOfficeCalc';
  49. const RETURNDATE_PHP_NUMERIC = 'P';
  50. const RETURNDATE_PHP_OBJECT = 'O';
  51. const RETURNDATE_EXCEL = 'E';
  52. /**
  53. * Compatibility mode to use for error checking and responses
  54. *
  55. * @access private
  56. * @var string
  57. */
  58. protected static $compatibilityMode = self::COMPATIBILITY_EXCEL;
  59. /**
  60. * Data Type to use when returning date values
  61. *
  62. * @access private
  63. * @var string
  64. */
  65. protected static $returnDateType = self::RETURNDATE_EXCEL;
  66. /**
  67. * List of error codes
  68. *
  69. * @access private
  70. * @var array
  71. */
  72. protected static $errorCodes = array(
  73. 'null' => '#NULL!',
  74. 'divisionbyzero' => '#DIV/0!',
  75. 'value' => '#VALUE!',
  76. 'reference' => '#REF!',
  77. 'name' => '#NAME?',
  78. 'num' => '#NUM!',
  79. 'na' => '#N/A',
  80. 'gettingdata' => '#GETTING_DATA'
  81. );
  82. /**
  83. * Set the Compatibility Mode
  84. *
  85. * @access public
  86. * @category Function Configuration
  87. * @param string $compatibilityMode Compatibility Mode
  88. * Permitted values are:
  89. * PHPExcel_Calculation_Functions::COMPATIBILITY_EXCEL 'Excel'
  90. * PHPExcel_Calculation_Functions::COMPATIBILITY_GNUMERIC 'Gnumeric'
  91. * PHPExcel_Calculation_Functions::COMPATIBILITY_OPENOFFICE 'OpenOfficeCalc'
  92. * @return boolean (Success or Failure)
  93. */
  94. public static function setCompatibilityMode($compatibilityMode)
  95. {
  96. if (($compatibilityMode == self::COMPATIBILITY_EXCEL) ||
  97. ($compatibilityMode == self::COMPATIBILITY_GNUMERIC) ||
  98. ($compatibilityMode == self::COMPATIBILITY_OPENOFFICE)) {
  99. self::$compatibilityMode = $compatibilityMode;
  100. return true;
  101. }
  102. return false;
  103. }
  104. /**
  105. * Return the current Compatibility Mode
  106. *
  107. * @access public
  108. * @category Function Configuration
  109. * @return string Compatibility Mode
  110. * Possible Return values are:
  111. * PHPExcel_Calculation_Functions::COMPATIBILITY_EXCEL 'Excel'
  112. * PHPExcel_Calculation_Functions::COMPATIBILITY_GNUMERIC 'Gnumeric'
  113. * PHPExcel_Calculation_Functions::COMPATIBILITY_OPENOFFICE 'OpenOfficeCalc'
  114. */
  115. public static function getCompatibilityMode()
  116. {
  117. return self::$compatibilityMode;
  118. }
  119. /**
  120. * Set the Return Date Format used by functions that return a date/time (Excel, PHP Serialized Numeric or PHP Object)
  121. *
  122. * @access public
  123. * @category Function Configuration
  124. * @param string $returnDateType Return Date Format
  125. * Permitted values are:
  126. * PHPExcel_Calculation_Functions::RETURNDATE_PHP_NUMERIC 'P'
  127. * PHPExcel_Calculation_Functions::RETURNDATE_PHP_OBJECT 'O'
  128. * PHPExcel_Calculation_Functions::RETURNDATE_EXCEL 'E'
  129. * @return boolean Success or failure
  130. */
  131. public static function setReturnDateType($returnDateType)
  132. {
  133. if (($returnDateType == self::RETURNDATE_PHP_NUMERIC) ||
  134. ($returnDateType == self::RETURNDATE_PHP_OBJECT) ||
  135. ($returnDateType == self::RETURNDATE_EXCEL)) {
  136. self::$returnDateType = $returnDateType;
  137. return true;
  138. }
  139. return false;
  140. }
  141. /**
  142. * Return the current Return Date Format for functions that return a date/time (Excel, PHP Serialized Numeric or PHP Object)
  143. *
  144. * @access public
  145. * @category Function Configuration
  146. * @return string Return Date Format
  147. * Possible Return values are:
  148. * PHPExcel_Calculation_Functions::RETURNDATE_PHP_NUMERIC 'P'
  149. * PHPExcel_Calculation_Functions::RETURNDATE_PHP_OBJECT 'O'
  150. * PHPExcel_Calculation_Functions::RETURNDATE_EXCEL 'E'
  151. */
  152. public static function getReturnDateType()
  153. {
  154. return self::$returnDateType;
  155. }
  156. /**
  157. * DUMMY
  158. *
  159. * @access public
  160. * @category Error Returns
  161. * @return string #Not Yet Implemented
  162. */
  163. public static function DUMMY()
  164. {
  165. return '#Not Yet Implemented';
  166. }
  167. /**
  168. * DIV0
  169. *
  170. * @access public
  171. * @category Error Returns
  172. * @return string #Not Yet Implemented
  173. */
  174. public static function DIV0()
  175. {
  176. return self::$errorCodes['divisionbyzero'];
  177. }
  178. /**
  179. * NA
  180. *
  181. * Excel Function:
  182. * =NA()
  183. *
  184. * Returns the error value #N/A
  185. * #N/A is the error value that means "no value is available."
  186. *
  187. * @access public
  188. * @category Logical Functions
  189. * @return string #N/A!
  190. */
  191. public static function NA()
  192. {
  193. return self::$errorCodes['na'];
  194. }
  195. /**
  196. * NaN
  197. *
  198. * Returns the error value #NUM!
  199. *
  200. * @access public
  201. * @category Error Returns
  202. * @return string #NUM!
  203. */
  204. public static function NaN()
  205. {
  206. return self::$errorCodes['num'];
  207. }
  208. /**
  209. * NAME
  210. *
  211. * Returns the error value #NAME?
  212. *
  213. * @access public
  214. * @category Error Returns
  215. * @return string #NAME?
  216. */
  217. public static function NAME()
  218. {
  219. return self::$errorCodes['name'];
  220. }
  221. /**
  222. * REF
  223. *
  224. * Returns the error value #REF!
  225. *
  226. * @access public
  227. * @category Error Returns
  228. * @return string #REF!
  229. */
  230. public static function REF()
  231. {
  232. return self::$errorCodes['reference'];
  233. }
  234. /**
  235. * NULL
  236. *
  237. * Returns the error value #NULL!
  238. *
  239. * @access public
  240. * @category Error Returns
  241. * @return string #NULL!
  242. */
  243. public static function NULL()
  244. {
  245. return self::$errorCodes['null'];
  246. }
  247. /**
  248. * VALUE
  249. *
  250. * Returns the error value #VALUE!
  251. *
  252. * @access public
  253. * @category Error Returns
  254. * @return string #VALUE!
  255. */
  256. public static function VALUE()
  257. {
  258. return self::$errorCodes['value'];
  259. }
  260. public static function isMatrixValue($idx)
  261. {
  262. return ((substr_count($idx, '.') <= 1) || (preg_match('/\.[A-Z]/', $idx) > 0));
  263. }
  264. public static function isValue($idx)
  265. {
  266. return (substr_count($idx, '.') == 0);
  267. }
  268. public static function isCellValue($idx)
  269. {
  270. return (substr_count($idx, '.') > 1);
  271. }
  272. public static function ifCondition($condition)
  273. {
  274. $condition = PHPExcel_Calculation_Functions::flattenSingleValue($condition);
  275. if (!isset($condition{0})) {
  276. $condition = '=""';
  277. }
  278. if (!in_array($condition{0}, array('>', '<', '='))) {
  279. if (!is_numeric($condition)) {
  280. $condition = PHPExcel_Calculation::wrapResult(strtoupper($condition));
  281. }
  282. return '=' . $condition;
  283. } else {
  284. preg_match('/([<>=]+)(.*)/', $condition, $matches);
  285. list(, $operator, $operand) = $matches;
  286. if (!is_numeric($operand)) {
  287. $operand = str_replace('"', '""', $operand);
  288. $operand = PHPExcel_Calculation::wrapResult(strtoupper($operand));
  289. }
  290. return $operator.$operand;
  291. }
  292. }
  293. /**
  294. * ERROR_TYPE
  295. *
  296. * @param mixed $value Value to check
  297. * @return boolean
  298. */
  299. public static function ERROR_TYPE($value = '')
  300. {
  301. $value = self::flattenSingleValue($value);
  302. $i = 1;
  303. foreach (self::$errorCodes as $errorCode) {
  304. if ($value === $errorCode) {
  305. return $i;
  306. }
  307. ++$i;
  308. }
  309. return self::NA();
  310. }
  311. /**
  312. * IS_BLANK
  313. *
  314. * @param mixed $value Value to check
  315. * @return boolean
  316. */
  317. public static function IS_BLANK($value = null)
  318. {
  319. if (!is_null($value)) {
  320. $value = self::flattenSingleValue($value);
  321. }
  322. return is_null($value);
  323. }
  324. /**
  325. * IS_ERR
  326. *
  327. * @param mixed $value Value to check
  328. * @return boolean
  329. */
  330. public static function IS_ERR($value = '')
  331. {
  332. $value = self::flattenSingleValue($value);
  333. return self::IS_ERROR($value) && (!self::IS_NA($value));
  334. }
  335. /**
  336. * IS_ERROR
  337. *
  338. * @param mixed $value Value to check
  339. * @return boolean
  340. */
  341. public static function IS_ERROR($value = '')
  342. {
  343. $value = self::flattenSingleValue($value);
  344. if (!is_string($value)) {
  345. return false;
  346. }
  347. return in_array($value, array_values(self::$errorCodes));
  348. }
  349. /**
  350. * IS_NA
  351. *
  352. * @param mixed $value Value to check
  353. * @return boolean
  354. */
  355. public static function IS_NA($value = '')
  356. {
  357. $value = self::flattenSingleValue($value);
  358. return ($value === self::NA());
  359. }
  360. /**
  361. * IS_EVEN
  362. *
  363. * @param mixed $value Value to check
  364. * @return boolean
  365. */
  366. public static function IS_EVEN($value = null)
  367. {
  368. $value = self::flattenSingleValue($value);
  369. if ($value === null) {
  370. return self::NAME();
  371. } elseif ((is_bool($value)) || ((is_string($value)) && (!is_numeric($value)))) {
  372. return self::VALUE();
  373. }
  374. return ($value % 2 == 0);
  375. }
  376. /**
  377. * IS_ODD
  378. *
  379. * @param mixed $value Value to check
  380. * @return boolean
  381. */
  382. public static function IS_ODD($value = null)
  383. {
  384. $value = self::flattenSingleValue($value);
  385. if ($value === null) {
  386. return self::NAME();
  387. } elseif ((is_bool($value)) || ((is_string($value)) && (!is_numeric($value)))) {
  388. return self::VALUE();
  389. }
  390. return (abs($value) % 2 == 1);
  391. }
  392. /**
  393. * IS_NUMBER
  394. *
  395. * @param mixed $value Value to check
  396. * @return boolean
  397. */
  398. public static function IS_NUMBER($value = null)
  399. {
  400. $value = self::flattenSingleValue($value);
  401. if (is_string($value)) {
  402. return false;
  403. }
  404. return is_numeric($value);
  405. }
  406. /**
  407. * IS_LOGICAL
  408. *
  409. * @param mixed $value Value to check
  410. * @return boolean
  411. */
  412. public static function IS_LOGICAL($value = null)
  413. {
  414. $value = self::flattenSingleValue($value);
  415. return is_bool($value);
  416. }
  417. /**
  418. * IS_TEXT
  419. *
  420. * @param mixed $value Value to check
  421. * @return boolean
  422. */
  423. public static function IS_TEXT($value = null)
  424. {
  425. $value = self::flattenSingleValue($value);
  426. return (is_string($value) && !self::IS_ERROR($value));
  427. }
  428. /**
  429. * IS_NONTEXT
  430. *
  431. * @param mixed $value Value to check
  432. * @return boolean
  433. */
  434. public static function IS_NONTEXT($value = null)
  435. {
  436. return !self::IS_TEXT($value);
  437. }
  438. /**
  439. * VERSION
  440. *
  441. * @return string Version information
  442. */
  443. public static function VERSION() {
  444. return 'PHPExcel 1.8.2, 2018-11-22';
  445. } // function VERSION()
  446. /**
  447. * N
  448. *
  449. * Returns a value converted to a number
  450. *
  451. * @param value The value you want converted
  452. * @return number N converts values listed in the following table
  453. * If value is or refers to N returns
  454. * A number That number
  455. * A date The serial number of that date
  456. * TRUE 1
  457. * FALSE 0
  458. * An error value The error value
  459. * Anything else 0
  460. */
  461. public static function N($value = null)
  462. {
  463. while (is_array($value)) {
  464. $value = array_shift($value);
  465. }
  466. switch (gettype($value)) {
  467. case 'double':
  468. case 'float':
  469. case 'integer':
  470. return $value;
  471. case 'boolean':
  472. return (integer) $value;
  473. case 'string':
  474. // Errors
  475. if ((strlen($value) > 0) && ($value{0} == '#')) {
  476. return $value;
  477. }
  478. break;
  479. }
  480. return 0;
  481. }
  482. /**
  483. * TYPE
  484. *
  485. * Returns a number that identifies the type of a value
  486. *
  487. * @param value The value you want tested
  488. * @return number N converts values listed in the following table
  489. * If value is or refers to N returns
  490. * A number 1
  491. * Text 2
  492. * Logical Value 4
  493. * An error value 16
  494. * Array or Matrix 64
  495. */
  496. public static function TYPE($value = null)
  497. {
  498. $value = self::flattenArrayIndexed($value);
  499. if (is_array($value) && (count($value) > 1)) {
  500. end($value);
  501. $a = key($value);
  502. // Range of cells is an error
  503. if (self::isCellValue($a)) {
  504. return 16;
  505. // Test for Matrix
  506. } elseif (self::isMatrixValue($a)) {
  507. return 64;
  508. }
  509. } elseif (empty($value)) {
  510. // Empty Cell
  511. return 1;
  512. }
  513. $value = self::flattenSingleValue($value);
  514. if (($value === null) || (is_float($value)) || (is_int($value))) {
  515. return 1;
  516. } elseif (is_bool($value)) {
  517. return 4;
  518. } elseif (is_array($value)) {
  519. return 64;
  520. } elseif (is_string($value)) {
  521. // Errors
  522. if ((strlen($value) > 0) && ($value{0} == '#')) {
  523. return 16;
  524. }
  525. return 2;
  526. }
  527. return 0;
  528. }
  529. /**
  530. * Convert a multi-dimensional array to a simple 1-dimensional array
  531. *
  532. * @param array $array Array to be flattened
  533. * @return array Flattened array
  534. */
  535. public static function flattenArray($array)
  536. {
  537. if (!is_array($array)) {
  538. return (array) $array;
  539. }
  540. $arrayValues = array();
  541. foreach ($array as $value) {
  542. if (is_array($value)) {
  543. foreach ($value as $val) {
  544. if (is_array($val)) {
  545. foreach ($val as $v) {
  546. $arrayValues[] = $v;
  547. }
  548. } else {
  549. $arrayValues[] = $val;
  550. }
  551. }
  552. } else {
  553. $arrayValues[] = $value;
  554. }
  555. }
  556. return $arrayValues;
  557. }
  558. /**
  559. * Convert a multi-dimensional array to a simple 1-dimensional array, but retain an element of indexing
  560. *
  561. * @param array $array Array to be flattened
  562. * @return array Flattened array
  563. */
  564. public static function flattenArrayIndexed($array)
  565. {
  566. if (!is_array($array)) {
  567. return (array) $array;
  568. }
  569. $arrayValues = array();
  570. foreach ($array as $k1 => $value) {
  571. if (is_array($value)) {
  572. foreach ($value as $k2 => $val) {
  573. if (is_array($val)) {
  574. foreach ($val as $k3 => $v) {
  575. $arrayValues[$k1.'.'.$k2.'.'.$k3] = $v;
  576. }
  577. } else {
  578. $arrayValues[$k1.'.'.$k2] = $val;
  579. }
  580. }
  581. } else {
  582. $arrayValues[$k1] = $value;
  583. }
  584. }
  585. return $arrayValues;
  586. }
  587. /**
  588. * Convert an array to a single scalar value by extracting the first element
  589. *
  590. * @param mixed $value Array or scalar value
  591. * @return mixed
  592. */
  593. public static function flattenSingleValue($value = '')
  594. {
  595. while (is_array($value)) {
  596. $value = array_pop($value);
  597. }
  598. return $value;
  599. }
  600. }
  601. //
  602. // There are a few mathematical functions that aren't available on all versions of PHP for all platforms
  603. // These functions aren't available in Windows implementations of PHP prior to version 5.3.0
  604. // So we test if they do exist for this version of PHP/operating platform; and if not we create them
  605. //
  606. if (!function_exists('acosh')) {
  607. function acosh($x)
  608. {
  609. return 2 * log(sqrt(($x + 1) / 2) + sqrt(($x - 1) / 2));
  610. } // function acosh()
  611. }
  612. if (!function_exists('asinh')) {
  613. function asinh($x)
  614. {
  615. return log($x + sqrt(1 + $x * $x));
  616. } // function asinh()
  617. }
  618. if (!function_exists('atanh')) {
  619. function atanh($x)
  620. {
  621. return (log(1 + $x) - log(1 - $x)) / 2;
  622. } // function atanh()
  623. }
  624. //
  625. // Strangely, PHP doesn't have a mb_str_replace multibyte function
  626. // As we'll only ever use this function with UTF-8 characters, we can simply "hard-code" the character set
  627. //
  628. if ((!function_exists('mb_str_replace')) &&
  629. (function_exists('mb_substr')) && (function_exists('mb_strlen')) && (function_exists('mb_strpos'))) {
  630. function mb_str_replace($search, $replace, $subject)
  631. {
  632. if (is_array($subject)) {
  633. $ret = array();
  634. foreach ($subject as $key => $val) {
  635. $ret[$key] = mb_str_replace($search, $replace, $val);
  636. }
  637. return $ret;
  638. }
  639. foreach ((array) $search as $key => $s) {
  640. if ($s == '' && $s !== 0) {
  641. continue;
  642. }
  643. $r = !is_array($replace) ? $replace : (array_key_exists($key, $replace) ? $replace[$key] : '');
  644. $pos = mb_strpos($subject, $s, 0, 'UTF-8');
  645. while ($pos !== false) {
  646. $subject = mb_substr($subject, 0, $pos, 'UTF-8') . $r . mb_substr($subject, $pos + mb_strlen($s, 'UTF-8'), 65535, 'UTF-8');
  647. $pos = mb_strpos($subject, $s, $pos + mb_strlen($r, 'UTF-8'), 'UTF-8');
  648. }
  649. }
  650. return $subject;
  651. }
  652. }