You are here

public static function PHPExcel_Calculation_Statistical::AVERAGEIF in Loft Data Grids 7.2

Same name and namespace in other branches
  1. 6.2 vendor/phpoffice/phpexcel/Classes/PHPExcel/Calculation/Statistical.php \PHPExcel_Calculation_Statistical::AVERAGEIF()

* AVERAGEIF * * Returns the average value from a range of cells that contain numbers within the list of arguments * * Excel Function: * AVERAGEIF(value1[,value2[, ...]],condition) * * @access public * @category Mathematical and Trigonometric Functions *

Parameters

mixed $arg,... Data values: * @param string $condition The criteria that defines which cells will be checked. * @param mixed[] $averageArgs Data values * @return float

File

vendor/phpoffice/phpexcel/Classes/PHPExcel/Calculation/Statistical.php, line 840

Class

PHPExcel_Calculation_Statistical
PHPExcel_Calculation_Statistical

Code

public static function AVERAGEIF($aArgs, $condition, $averageArgs = array()) {

  // Return value
  $returnValue = 0;
  $aArgs = PHPExcel_Calculation_Functions::flattenArray($aArgs);
  $averageArgs = PHPExcel_Calculation_Functions::flattenArray($averageArgs);
  if (empty($averageArgs)) {
    $averageArgs = $aArgs;
  }
  $condition = PHPExcel_Calculation_Functions::_ifCondition($condition);

  // Loop through arguments
  $aCount = 0;
  foreach ($aArgs as $key => $arg) {
    if (!is_numeric($arg)) {
      $arg = PHPExcel_Calculation::_wrapResult(strtoupper($arg));
    }
    $testCondition = '=' . $arg . $condition;
    if (PHPExcel_Calculation::getInstance()
      ->_calculateFormulaValue($testCondition)) {
      if (is_null($returnValue) || $arg > $returnValue) {
        $returnValue += $arg;
        ++$aCount;
      }
    }
  }

  // Return
  if ($aCount > 0) {
    return $returnValue / $aCount;
  }
  else {
    return PHPExcel_Calculation_Functions::DIV0();
  }
}