Logical.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. if (!defined('PHPEXCEL_ROOT')) {
  8. /**
  9. * @ignore
  10. */
  11. define('PHPEXCEL_ROOT', dirname(__FILE__) . '/../../');
  12. require(PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php');
  13. }
  14. /**
  15. * PHPExcel_Calculation_Logical
  16. *
  17. * Copyright (c) 2006 - 2015 PHPExcel
  18. *
  19. * This library is free software; you can redistribute it and/or
  20. * modify it under the terms of the GNU Lesser General Public
  21. * License as published by the Free Software Foundation; either
  22. * version 2.1 of the License, or (at your option) any later version.
  23. *
  24. * This library is distributed in the hope that it will be useful,
  25. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  27. * Lesser General Public License for more details.
  28. *
  29. * You should have received a copy of the GNU Lesser General Public
  30. * License along with this library; if not, write to the Free Software
  31. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  32. *
  33. * @category PHPExcel
  34. * @package PHPExcel_Calculation
  35. * @copyright Copyright (c) 2006 - 2015 PHPExcel (http://www.codeplex.com/PHPExcel)
  36. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  37. * @version ##VERSION##, ##DATE##
  38. */
  39. class PHPExcel_Calculation_Logical
  40. {
  41. /**
  42. * TRUE
  43. *
  44. * Returns the boolean TRUE.
  45. *
  46. * Excel Function:
  47. * =TRUE()
  48. *
  49. * @access public
  50. * @category Logical Functions
  51. * @return boolean True
  52. */
  53. public static function TRUE()
  54. {
  55. return true;
  56. }
  57. /**
  58. * FALSE
  59. *
  60. * Returns the boolean FALSE.
  61. *
  62. * Excel Function:
  63. * =FALSE()
  64. *
  65. * @access public
  66. * @category Logical Functions
  67. * @return boolean False
  68. */
  69. public static function FALSE()
  70. {
  71. return false;
  72. }
  73. /**
  74. * LOGICAL_AND
  75. *
  76. * Returns boolean TRUE if all its arguments are TRUE; returns FALSE if one or more argument is FALSE.
  77. *
  78. * Excel Function:
  79. * =AND(logical1[,logical2[, ...]])
  80. *
  81. * The arguments must evaluate to logical values such as TRUE or FALSE, or the arguments must be arrays
  82. * or references that contain logical values.
  83. *
  84. * Boolean arguments are treated as True or False as appropriate
  85. * Integer or floating point arguments are treated as True, except for 0 or 0.0 which are False
  86. * If any argument value is a string, or a Null, the function returns a #VALUE! error, unless the string holds
  87. * the value TRUE or FALSE, in which case it is evaluated as the corresponding boolean value
  88. *
  89. * @access public
  90. * @category Logical Functions
  91. * @param mixed $arg,... Data values
  92. * @return boolean The logical AND of the arguments.
  93. */
  94. public static function LOGICAL_AND()
  95. {
  96. // Return value
  97. $returnValue = true;
  98. // Loop through the arguments
  99. $aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args());
  100. $argCount = -1;
  101. foreach ($aArgs as $argCount => $arg) {
  102. // Is it a boolean value?
  103. if (is_bool($arg)) {
  104. $returnValue = $returnValue && $arg;
  105. } elseif ((is_numeric($arg)) && (!is_string($arg))) {
  106. $returnValue = $returnValue && ($arg != 0);
  107. } elseif (is_string($arg)) {
  108. $arg = strtoupper($arg);
  109. if (($arg == 'TRUE') || ($arg == PHPExcel_Calculation::getTRUE())) {
  110. $arg = true;
  111. } elseif (($arg == 'FALSE') || ($arg == PHPExcel_Calculation::getFALSE())) {
  112. $arg = false;
  113. } else {
  114. return PHPExcel_Calculation_Functions::VALUE();
  115. }
  116. $returnValue = $returnValue && ($arg != 0);
  117. }
  118. }
  119. // Return
  120. if ($argCount < 0) {
  121. return PHPExcel_Calculation_Functions::VALUE();
  122. }
  123. return $returnValue;
  124. }
  125. /**
  126. * LOGICAL_OR
  127. *
  128. * Returns boolean TRUE if any argument is TRUE; returns FALSE if all arguments are FALSE.
  129. *
  130. * Excel Function:
  131. * =OR(logical1[,logical2[, ...]])
  132. *
  133. * The arguments must evaluate to logical values such as TRUE or FALSE, or the arguments must be arrays
  134. * or references that contain logical values.
  135. *
  136. * Boolean arguments are treated as True or False as appropriate
  137. * Integer or floating point arguments are treated as True, except for 0 or 0.0 which are False
  138. * If any argument value is a string, or a Null, the function returns a #VALUE! error, unless the string holds
  139. * the value TRUE or FALSE, in which case it is evaluated as the corresponding boolean value
  140. *
  141. * @access public
  142. * @category Logical Functions
  143. * @param mixed $arg,... Data values
  144. * @return boolean The logical OR of the arguments.
  145. */
  146. public static function LOGICAL_OR()
  147. {
  148. // Return value
  149. $returnValue = false;
  150. // Loop through the arguments
  151. $aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args());
  152. $argCount = -1;
  153. foreach ($aArgs as $argCount => $arg) {
  154. // Is it a boolean value?
  155. if (is_bool($arg)) {
  156. $returnValue = $returnValue || $arg;
  157. } elseif ((is_numeric($arg)) && (!is_string($arg))) {
  158. $returnValue = $returnValue || ($arg != 0);
  159. } elseif (is_string($arg)) {
  160. $arg = strtoupper($arg);
  161. if (($arg == 'TRUE') || ($arg == PHPExcel_Calculation::getTRUE())) {
  162. $arg = true;
  163. } elseif (($arg == 'FALSE') || ($arg == PHPExcel_Calculation::getFALSE())) {
  164. $arg = false;
  165. } else {
  166. return PHPExcel_Calculation_Functions::VALUE();
  167. }
  168. $returnValue = $returnValue || ($arg != 0);
  169. }
  170. }
  171. // Return
  172. if ($argCount < 0) {
  173. return PHPExcel_Calculation_Functions::VALUE();
  174. }
  175. return $returnValue;
  176. }
  177. /**
  178. * NOT
  179. *
  180. * Returns the boolean inverse of the argument.
  181. *
  182. * Excel Function:
  183. * =NOT(logical)
  184. *
  185. * The argument must evaluate to a logical value such as TRUE or FALSE
  186. *
  187. * Boolean arguments are treated as True or False as appropriate
  188. * Integer or floating point arguments are treated as True, except for 0 or 0.0 which are False
  189. * If any argument value is a string, or a Null, the function returns a #VALUE! error, unless the string holds
  190. * the value TRUE or FALSE, in which case it is evaluated as the corresponding boolean value
  191. *
  192. * @access public
  193. * @category Logical Functions
  194. * @param mixed $logical A value or expression that can be evaluated to TRUE or FALSE
  195. * @return boolean The boolean inverse of the argument.
  196. */
  197. public static function NOT($logical = false)
  198. {
  199. $logical = PHPExcel_Calculation_Functions::flattenSingleValue($logical);
  200. if (is_string($logical)) {
  201. $logical = strtoupper($logical);
  202. if (($logical == 'TRUE') || ($logical == PHPExcel_Calculation::getTRUE())) {
  203. return false;
  204. } elseif (($logical == 'FALSE') || ($logical == PHPExcel_Calculation::getFALSE())) {
  205. return true;
  206. } else {
  207. return PHPExcel_Calculation_Functions::VALUE();
  208. }
  209. }
  210. return !$logical;
  211. }
  212. /**
  213. * STATEMENT_IF
  214. *
  215. * Returns one value if a condition you specify evaluates to TRUE and another value if it evaluates to FALSE.
  216. *
  217. * Excel Function:
  218. * =IF(condition[,returnIfTrue[,returnIfFalse]])
  219. *
  220. * Condition is any value or expression that can be evaluated to TRUE or FALSE.
  221. * For example, A10=100 is a logical expression; if the value in cell A10 is equal to 100,
  222. * the expression evaluates to TRUE. Otherwise, the expression evaluates to FALSE.
  223. * This argument can use any comparison calculation operator.
  224. * ReturnIfTrue is the value that is returned if condition evaluates to TRUE.
  225. * For example, if this argument is the text string "Within budget" and the condition argument evaluates to TRUE,
  226. * then the IF function returns the text "Within budget"
  227. * If condition is TRUE and ReturnIfTrue is blank, this argument returns 0 (zero). To display the word TRUE, use
  228. * the logical value TRUE for this argument.
  229. * ReturnIfTrue can be another formula.
  230. * ReturnIfFalse is the value that is returned if condition evaluates to FALSE.
  231. * For example, if this argument is the text string "Over budget" and the condition argument evaluates to FALSE,
  232. * then the IF function returns the text "Over budget".
  233. * If condition is FALSE and ReturnIfFalse is omitted, then the logical value FALSE is returned.
  234. * If condition is FALSE and ReturnIfFalse is blank, then the value 0 (zero) is returned.
  235. * ReturnIfFalse can be another formula.
  236. *
  237. * @access public
  238. * @category Logical Functions
  239. * @param mixed $condition Condition to evaluate
  240. * @param mixed $returnIfTrue Value to return when condition is true
  241. * @param mixed $returnIfFalse Optional value to return when condition is false
  242. * @return mixed The value of returnIfTrue or returnIfFalse determined by condition
  243. */
  244. public static function STATEMENT_IF($condition = true, $returnIfTrue = 0, $returnIfFalse = false)
  245. {
  246. $condition = (is_null($condition)) ? true : (boolean) PHPExcel_Calculation_Functions::flattenSingleValue($condition);
  247. $returnIfTrue = (is_null($returnIfTrue)) ? 0 : PHPExcel_Calculation_Functions::flattenSingleValue($returnIfTrue);
  248. $returnIfFalse = (is_null($returnIfFalse)) ? false : PHPExcel_Calculation_Functions::flattenSingleValue($returnIfFalse);
  249. return ($condition) ? $returnIfTrue : $returnIfFalse;
  250. }
  251. /**
  252. * IFERROR
  253. *
  254. * Excel Function:
  255. * =IFERROR(testValue,errorpart)
  256. *
  257. * @access public
  258. * @category Logical Functions
  259. * @param mixed $testValue Value to check, is also the value returned when no error
  260. * @param mixed $errorpart Value to return when testValue is an error condition
  261. * @return mixed The value of errorpart or testValue determined by error condition
  262. */
  263. public static function IFERROR($testValue = '', $errorpart = '')
  264. {
  265. $testValue = (is_null($testValue)) ? '' : PHPExcel_Calculation_Functions::flattenSingleValue($testValue);
  266. $errorpart = (is_null($errorpart)) ? '' : PHPExcel_Calculation_Functions::flattenSingleValue($errorpart);
  267. return self::STATEMENT_IF(PHPExcel_Calculation_Functions::IS_ERROR($testValue), $errorpart, $testValue);
  268. }
  269. }