You are here

public function PHPExcel_Calculation::_calculateFormulaValue in Loft Data Grids 7.2

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

* Parse a cell formula and calculate its value * *

Parameters

string $formula The formula to parse and calculate: * @param string $cellID The ID (e.g. A3) of the cell that we are calculating * @param PHPExcel_Cell $pCell Cell to calculate * @return mixed * @throws PHPExcel_Calculation_Exception

2 calls to PHPExcel_Calculation::_calculateFormulaValue()
PHPExcel_Calculation::calculateCellValue in vendor/phpoffice/phpexcel/Classes/PHPExcel/Calculation.php
* Calculate the value of a cell formula * * @access public *
PHPExcel_Calculation::calculateFormula in vendor/phpoffice/phpexcel/Classes/PHPExcel/Calculation.php
* Calculate the value of a formula * *

File

vendor/phpoffice/phpexcel/Classes/PHPExcel/Calculation.php, line 2375

Class

PHPExcel_Calculation
PHPExcel_Calculation (Multiton)

Code

public function _calculateFormulaValue($formula, $cellID = null, PHPExcel_Cell $pCell = null) {
  $cellValue = null;

  //	Basic validation that this is indeed a formula
  //	We simply return the cell value if not
  $formula = trim($formula);
  if ($formula[0] != '=') {
    return self::_wrapResult($formula);
  }
  $formula = ltrim(substr($formula, 1));
  if (!isset($formula[0])) {
    return self::_wrapResult($formula);
  }
  $pCellParent = $pCell !== NULL ? $pCell
    ->getWorksheet() : NULL;
  $wsTitle = $pCellParent !== NULL ? $pCellParent
    ->getTitle() : "\0Wrk";
  $wsCellReference = $wsTitle . '!' . $cellID;
  if ($cellID !== NULL && $this
    ->getValueFromCache($wsCellReference, $cellValue)) {
    return $cellValue;
  }
  if ($wsTitle[0] !== "\0" && $this->_cyclicReferenceStack
    ->onStack($wsCellReference)) {
    if ($this->cyclicFormulaCount <= 0) {
      $this->_cyclicFormulaCell = '';
      return $this
        ->_raiseFormulaError('Cyclic Reference in Formula');
    }
    elseif ($this->_cyclicFormulaCell === $wsCellReference) {
      ++$this->_cyclicFormulaCount;
      if ($this->_cyclicFormulaCount >= $this->cyclicFormulaCount) {
        $this->_cyclicFormulaCell = '';
        return $cellValue;
      }
    }
    elseif ($this->_cyclicFormulaCell == '') {
      if ($this->_cyclicFormulaCount >= $this->cyclicFormulaCount) {
        return $cellValue;
      }
      $this->_cyclicFormulaCell = $wsCellReference;
    }
  }

  //	Parse the formula onto the token stack and calculate the value
  $this->_cyclicReferenceStack
    ->push($wsCellReference);
  $cellValue = $this
    ->_processTokenStack($this
    ->_parseFormula($formula, $pCell), $cellID, $pCell);
  $this->_cyclicReferenceStack
    ->pop();

  // Save to calculation cache
  if ($cellID !== NULL) {
    $this
      ->saveValueToCache($wsCellReference, $cellValue);
  }

  //	Return the calculated value
  return $cellValue;
}