PHPExcel_Calculation
[ class tree: PHPExcel_Calculation ] [ index: PHPExcel_Calculation ] [ all elements ]

Source for file Logical.php

Documentation is available at Logical.php

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

Documentation generated on Sun, 27 Feb 2011 16:32:30 -0800 by phpDocumentor 1.4.3