/** * Calculate the value of a cell formula * * @access public * @param Cell $pCell Cell to calculate * @param Boolean $resetLog Flag indicating whether the debug log should be reset or not * @return mixed * @throws Exception */ public function calculateCellValue(Cell $pCell = null, $resetLog = true) { if ($resetLog) { // Initialise the logging settings if requested $this->formulaError = null; $this->debugLog = $this->debugLogStack = array(); $this->_cyclicFormulaCount = 1; $returnArrayAsType = self::$returnArrayAsType; self::$returnArrayAsType = self::RETURN_ARRAY_AS_ARRAY; } // Read the formula from the cell if (is_null($pCell)) { return null; } if ($resetLog) { self::$returnArrayAsType = $returnArrayAsType; } // Execute the calculation for the cell formula try { $result = self::_unwrapResult($this->_calculateFormulaValue($pCell->getValue(), $pCell->getCoordinate(), $pCell)); } catch (Exception $e) { throw new Exception($e->getMessage()); } if (is_array($result) && self::$returnArrayAsType != self::RETURN_ARRAY_AS_ARRAY) { $testResult = Calculation_Functions::flattenArray($result); if (self::$returnArrayAsType == self::RETURN_ARRAY_AS_ERROR) { return Calculation_Functions::VALUE(); } // If there's only a single cell in the array, then we allow it if (count($testResult) != 1) { // If keys are numeric, then it's a matrix result rather than a cell range result, so we permit it $r = array_keys($result); $r = array_shift($r); if (!is_numeric($r)) { return Calculation_Functions::VALUE(); } if (is_array($result[$r])) { $c = array_keys($result[$r]); $c = array_shift($c); if (!is_numeric($c)) { return Calculation_Functions::VALUE(); } } } $result = array_shift($testResult); } if (is_null($result)) { return 0; } elseif (is_float($result) && (is_nan($result) || is_infinite($result))) { return Calculation_Functions::NaN(); } return $result; }