Пример #1
0
 /**
  * VLOOKUP
  * The VLOOKUP function searches for value in the left-most column of lookup_array and returns the value in the same row based on the index_number.
  * @param	lookup_value	The value that you want to match in lookup_array
  * @param	lookup_array	The range of cells being searched
  * @param	index_number	The column number in table_array from which the matching value must be returned. The first column is 1.
  * @param	not_exact_match	Determines if you are looking for an exact match based on lookup_value.
  * @return	mixed			The value of the found cell
  */
 public static function VLOOKUP($lookup_value, $lookup_array, $index_number, $not_exact_match = true)
 {
     $lookup_value = PHPExcel_Calculation_Functions::flattenSingleValue($lookup_value);
     $index_number = PHPExcel_Calculation_Functions::flattenSingleValue($index_number);
     $not_exact_match = PHPExcel_Calculation_Functions::flattenSingleValue($not_exact_match);
     // index_number must be greater than or equal to 1
     if ($index_number < 1) {
         return PHPExcel_Calculation_Functions::VALUE();
     }
     // index_number must be less than or equal to the number of columns in lookup_array
     if (!is_array($lookup_array) || count($lookup_array) < 1) {
         return PHPExcel_Calculation_Functions::REF();
     } else {
         $f = array_keys($lookup_array);
         $firstRow = array_pop($f);
         if (!is_array($lookup_array[$firstRow]) || $index_number > count($lookup_array[$firstRow])) {
             return PHPExcel_Calculation_Functions::REF();
         } else {
             $columnKeys = array_keys($lookup_array[$firstRow]);
             $returnColumn = $columnKeys[--$index_number];
             $firstColumn = array_shift($columnKeys);
         }
     }
     if (!$not_exact_match) {
         uasort($lookup_array, array('self', '_vlookupSort'));
     }
     $rowNumber = $rowValue = False;
     foreach ($lookup_array as $rowKey => $rowData) {
         if (strtolower($rowData[$firstColumn]) > strtolower($lookup_value)) {
             break;
         }
         $rowNumber = $rowKey;
         $rowValue = $rowData[$firstColumn];
     }
     if ($rowNumber !== false) {
         if (!$not_exact_match && $rowValue != $lookup_value) {
             //	if an exact match is required, we have what we need to return an appropriate response
             return PHPExcel_Calculation_Functions::NA();
         } else {
             //	otherwise return the appropriate value
             return $lookup_array[$rowNumber][$returnColumn];
         }
     }
     return PHPExcel_Calculation_Functions::NA();
 }
Пример #2
0
 /**
  * Extract range values
  *
  * @param	string				&$pRange	String based range representation
  * @param	PHPExcel_Worksheet	$pSheet		Worksheet
  * @return  mixed				Array of values in range if range contains more than one element. Otherwise, a single value is returned.
  * @param	boolean				$resetLog	Flag indicating whether calculation log should be reset or not
  * @throws	PHPExcel_Calculation_Exception
  */
 public function extractNamedRange(&$pRange = 'A1', PHPExcel_Worksheet $pSheet = NULL, $resetLog = TRUE)
 {
     // Return value
     $returnValue = array();
     //		echo 'extractNamedRange('.$pRange.')<br />';
     if ($pSheet !== NULL) {
         $pSheetName = $pSheet->getTitle();
         //			echo 'Current sheet name is '.$pSheetName.'<br />';
         //			echo 'Range reference is '.$pRange.'<br />';
         if (strpos($pRange, '!') !== false) {
             //				echo '$pRange reference includes sheet reference',PHP_EOL;
             list($pSheetName, $pRange) = PHPExcel_Worksheet::extractSheetTitle($pRange, true);
             //				echo 'New sheet name is '.$pSheetName,PHP_EOL;
             //				echo 'Adjusted Range reference is '.$pRange,PHP_EOL;
             $pSheet = $this->_workbook->getSheetByName($pSheetName);
         }
         // Named range?
         $namedRange = PHPExcel_NamedRange::resolveRange($pRange, $pSheet);
         if ($namedRange !== NULL) {
             $pSheet = $namedRange->getWorksheet();
             //				echo 'Named Range '.$pRange.' (';
             $pRange = $namedRange->getRange();
             $splitRange = PHPExcel_Cell::splitRange($pRange);
             //	Convert row and column references
             if (ctype_alpha($splitRange[0][0])) {
                 $pRange = $splitRange[0][0] . '1:' . $splitRange[0][1] . $namedRange->getWorksheet()->getHighestRow();
             } elseif (ctype_digit($splitRange[0][0])) {
                 $pRange = 'A' . $splitRange[0][0] . ':' . $namedRange->getWorksheet()->getHighestColumn() . $splitRange[0][1];
             }
             //				echo $pRange.') is in sheet '.$namedRange->getWorksheet()->getTitle().'<br />';
             //				if ($pSheet->getTitle() != $namedRange->getWorksheet()->getTitle()) {
             //					if (!$namedRange->getLocalOnly()) {
             //						$pSheet = $namedRange->getWorksheet();
             //					} else {
             //						return $returnValue;
             //					}
             //				}
         } else {
             return PHPExcel_Calculation_Functions::REF();
         }
         // Extract range
         $aReferences = PHPExcel_Cell::extractAllCellReferencesInRange($pRange);
         //			var_dump($aReferences);
         if (!isset($aReferences[1])) {
             //	Single cell (or single column or row) in range
             list($currentCol, $currentRow) = PHPExcel_Cell::coordinateFromString($aReferences[0]);
             $cellValue = NULL;
             if ($pSheet->cellExists($aReferences[0])) {
                 $returnValue[$currentRow][$currentCol] = $pSheet->getCell($aReferences[0])->getCalculatedValue($resetLog);
             } else {
                 $returnValue[$currentRow][$currentCol] = NULL;
             }
         } else {
             // Extract cell data for all cells in the range
             foreach ($aReferences as $reference) {
                 // Extract range
                 list($currentCol, $currentRow) = PHPExcel_Cell::coordinateFromString($reference);
                 //					echo 'NAMED RANGE: $currentCol='.$currentCol.' $currentRow='.$currentRow.'<br />';
                 $cellValue = NULL;
                 if ($pSheet->cellExists($reference)) {
                     $returnValue[$currentRow][$currentCol] = $pSheet->getCell($reference)->getCalculatedValue($resetLog);
                 } else {
                     $returnValue[$currentRow][$currentCol] = NULL;
                 }
             }
         }
         //				print_r($returnValue);
         //			echo '<br />';
     }
     // Return
     return $returnValue;
 }
Пример #3
0
 /**
  * HLOOKUP
  * The HLOOKUP function searches for value in the top-most row of lookup_array and returns the value in the same column based on the index_number.
  * @param    lookup_value    The value that you want to match in lookup_array
  * @param    lookup_array    The range of cells being searched
  * @param    index_number    The row number in table_array from which the matching value must be returned. The first row is 1.
  * @param    not_exact_match Determines if you are looking for an exact match based on lookup_value.
  * @return   mixed           The value of the found cell
  */
 public static function HLOOKUP($lookup_value, $lookup_array, $index_number, $not_exact_match = true)
 {
     $lookup_value = PHPExcel_Calculation_Functions::flattenSingleValue($lookup_value);
     $index_number = PHPExcel_Calculation_Functions::flattenSingleValue($index_number);
     $not_exact_match = PHPExcel_Calculation_Functions::flattenSingleValue($not_exact_match);
     // index_number must be greater than or equal to 1
     if ($index_number < 1) {
         return PHPExcel_Calculation_Functions::VALUE();
     }
     // index_number must be less than or equal to the number of columns in lookup_array
     if (!is_array($lookup_array) || empty($lookup_array)) {
         return PHPExcel_Calculation_Functions::REF();
     } else {
         $f = array_keys($lookup_array);
         $firstRow = array_pop($f);
         if (!is_array($lookup_array[$firstRow]) || $index_number > count($lookup_array[$firstRow])) {
             return PHPExcel_Calculation_Functions::REF();
         } else {
             $columnKeys = array_keys($lookup_array[$firstRow]);
             $firstkey = $f[0] - 1;
             $returnColumn = $firstkey + $index_number;
             $firstColumn = array_shift($f);
         }
     }
     if (!$not_exact_match) {
         $firstRowH = asort($lookup_array[$firstColumn]);
     }
     $rowNumber = $rowValue = False;
     foreach ($lookup_array[$firstColumn] as $rowKey => $rowData) {
         if (is_numeric($lookup_value) && is_numeric($rowData) && $rowData > $lookup_value || !is_numeric($lookup_value) && !is_numeric($rowData) && strtolower($rowData) > strtolower($lookup_value)) {
             break;
         }
         $rowNumber = $rowKey;
         $rowValue = $rowData;
     }
     if ($rowNumber !== false) {
         if (!$not_exact_match && $rowValue != $lookup_value) {
             //  if an exact match is required, we have what we need to return an appropriate response
             return PHPExcel_Calculation_Functions::NA();
         } else {
             //  otherwise return the appropriate value
             $result = $lookup_array[$returnColumn][$rowNumber];
             return $result;
         }
     }
     return PHPExcel_Calculation_Functions::NA();
 }
Пример #4
0
 public function testREF()
 {
     $result = PHPExcel_Calculation_Functions::REF();
     $this->assertEquals('#REF!', $result);
 }
 /**
  * Extract range values
  *
  * @param	string				&$pRange	String based range representation
  * @param	PHPExcel_Worksheet	$pSheet		Worksheet
  * @return  mixed				Array of values in range if range contains more than one element. Otherwise, a single value is returned.
  * @throws	Exception
  */
 public function extractNamedRange(&$pRange = 'A1', PHPExcel_Worksheet $pSheet = null, $resetLog = true)
 {
     // Return value
     $returnValue = array();
     //		echo 'extractNamedRange('.$pRange.')<br />';
     if (!is_null($pSheet)) {
         //			echo 'Current sheet name is '.$pSheet->getTitle().'<br />';
         //			echo 'Range reference is '.$pRange.'<br />';
         if (strpos($pRange, '!') !== false) {
             //				echo '$pRange reference includes sheet reference<br />';
             $worksheetReference = PHPExcel_Worksheet::extractSheetTitle($pRange, true);
             $pSheet = $pSheet->getParent()->getSheetByName($worksheetReference[0]);
             //				echo 'New sheet name is '.$pSheet->getTitle().'<br />';
             $pRange = $worksheetReference[1];
             //				echo 'Adjusted Range reference is '.$pRange.'<br />';
         }
         // Named range?
         $namedRange = PHPExcel_NamedRange::resolveRange($pRange, $pSheet);
         if (!is_null($namedRange)) {
             $pSheet = $namedRange->getWorksheet();
             ////			echo 'Named Range '.$pRange.' (';
             $pRange = $namedRange->getRange();
             ////				echo $pRange.') is in sheet '.$namedRange->getWorksheet()->getTitle().'<br />';
             //				if ($pSheet->getTitle() != $namedRange->getWorksheet()->getTitle()) {
             //					if (!$namedRange->getLocalOnly()) {
             //						$pSheet = $namedRange->getWorksheet();
             //					} else {
             //						return $returnValue;
             //					}
             //				}
         } else {
             return PHPExcel_Calculation_Functions::REF();
         }
         // Extract range
         $aReferences = PHPExcel_Cell::extractAllCellReferencesInRange($pRange);
         if (count($aReferences) == 1) {
             list($currentCol, $currentRow) = PHPExcel_Cell::coordinateFromString($aReferences[0]);
             if ($pSheet->cellExists($aReferences[0])) {
                 $returnValue[$currentRow][$currentCol] = $pSheet->getCell($aReferences[0])->getCalculatedValue($resetLog);
             } else {
                 $returnValue[$currentRow][$currentCol] = NULL;
             }
         } else {
             // Extract cell data
             foreach ($aReferences as $reference) {
                 // Extract range
                 list($currentCol, $currentRow) = PHPExcel_Cell::coordinateFromString($reference);
                 //					echo 'NAMED RANGE: $currentCol='.$currentCol.' $currentRow='.$currentRow.'<br />';
                 if ($pSheet->cellExists($reference)) {
                     $returnValue[$currentRow][$currentCol] = $pSheet->getCell($reference)->getCalculatedValue($resetLog);
                 } else {
                     $returnValue[$currentRow][$currentCol] = NULL;
                 }
             }
         }
         //				print_r($returnValue);
         //			echo '<br />';
     }
     // Return
     return $returnValue;
 }
 private function _processTokenStack($tokens, $cellID = null, PHPExcel_Cell $pCell = null)
 {
     if ($tokens == false) {
         return false;
     }
     $stack = new PHPExcel_Token_Stack();
     //	Loop through each token in turn
     foreach ($tokens as $token) {
         //			echo '<b>Token is '.$token.'</b><br />';
         // if the token is a binary operator, pop the top two values off the stack, do the operation, and push the result back on the stack
         if (in_array($token, $this->_binaryOperators, true)) {
             //				echo 'Token is a binary operator<br />';
             //	We must have two operands, error if we don't
             if (is_null($operand2 = $stack->pop())) {
                 return $this->_raiseFormulaError('Internal error - Operand value missing from stack');
             }
             if (is_null($operand1 = $stack->pop())) {
                 return $this->_raiseFormulaError('Internal error - Operand value missing from stack');
             }
             //	Log what we're doing
             $this->_writeDebug($cellID, 'Evaluating ' . self::_showValue($operand1) . ' ' . $token . ' ' . self::_showValue($operand2));
             //	Process the operation in the appropriate manner
             switch ($token) {
                 //	Comparison (Boolean) Operators
                 case '>':
                     //	Greater than
                 //	Greater than
                 case '<':
                     //	Less than
                 //	Less than
                 case '>=':
                     //	Greater than or Equal to
                 //	Greater than or Equal to
                 case '<=':
                     //	Less than or Equal to
                 //	Less than or Equal to
                 case '=':
                     //	Equality
                 //	Equality
                 case '<>':
                     //	Inequality
                     $this->_executeBinaryComparisonOperation($cellID, $operand1, $operand2, $token, $stack);
                     break;
                     //	Binary Operators
                 //	Binary Operators
                 case '+':
                     //	Addition
                     $this->_executeNumericBinaryOperation($cellID, $operand1, $operand2, $token, 'plusEquals', $stack);
                     break;
                 case '-':
                     //	Subtraction
                     $this->_executeNumericBinaryOperation($cellID, $operand1, $operand2, $token, 'minusEquals', $stack);
                     break;
                 case '*':
                     //	Multiplication
                     $this->_executeNumericBinaryOperation($cellID, $operand1, $operand2, $token, 'arrayTimesEquals', $stack);
                     break;
                 case '/':
                     //	Division
                     $this->_executeNumericBinaryOperation($cellID, $operand1, $operand2, $token, 'arrayRightDivide', $stack);
                     break;
                 case '^':
                     //	Exponential
                     $this->_executeNumericBinaryOperation($cellID, $operand1, $operand2, $token, 'power', $stack);
                     break;
                 case '&':
                     //	Concatenation
                     //	If either of the operands is a matrix, we need to treat them both as matrices
                     //		(converting the other operand to a matrix if need be); then perform the required
                     //		matrix operation
                     if (is_array($operand1) || is_array($operand2)) {
                         //	Ensure that both operands are arrays/matrices
                         self::_checkMatrixOperands($operand1, $operand2);
                         try {
                             //	Convert operand 1 from a PHP array to a matrix
                             $matrix = new Matrix($operand1);
                             //	Perform the required operation against the operand 1 matrix, passing in operand 2
                             $matrixResult = $matrix->concat($operand2);
                             $result = $matrixResult->getArray();
                         } catch (Exception $ex) {
                             $this->_writeDebug($cellID, 'JAMA Matrix Exception: ' . $ex->getMessage());
                             $result = '#VALUE!';
                         }
                     } else {
                         $result = '"' . str_replace('""', '"', self::_unwrapResult($operand1, '"') . self::_unwrapResult($operand2, '"')) . '"';
                     }
                     $this->_writeDebug($cellID, 'Evaluation Result is ' . self::_showTypeDetails($result));
                     $stack->push($result);
                     break;
             }
             // if the token is a unary operator, pop one value off the stack, do the operation, and push it back on
         } elseif ($token === "_" || $token === "%") {
             //				echo 'Token is a unary operator<br />';
             if (is_null($arg = $stack->pop())) {
                 return $this->_raiseFormulaError('Internal error - Operand value missing from stack');
             }
             if ($token === "_") {
                 //					echo 'Token is a negation operator<br />';
                 $this->_writeDebug($cellID, 'Evaluating Negation of ' . self::_showValue($arg));
                 $multiplier = -1;
             } else {
                 //					echo 'Token is a percentile operator<br />';
                 $this->_writeDebug($cellID, 'Evaluating Percentile of ' . self::_showValue($arg));
                 $multiplier = 0.01;
             }
             if (is_array($arg)) {
                 self::_checkMatrixOperands($arg, $multiplier);
                 try {
                     $matrix1 = new Matrix($arg);
                     $matrixResult = $matrix1->arrayTimesEquals($multiplier);
                     $result = $matrixResult->getArray();
                 } catch (Exception $ex) {
                     $this->_writeDebug($cellID, 'JAMA Matrix Exception: ' . $ex->getMessage());
                     $result = '#VALUE!';
                 }
             } else {
                 $result = $multiplier * $arg;
             }
             $this->_writeDebug($cellID, 'Evaluation Result is ' . self::_showTypeDetails($result));
             $stack->push($result);
         } elseif (preg_match('/^' . self::CALCULATION_REGEXP_CELLREF . '$/i', $token, $matches)) {
             //				echo 'Element '.$token.' is a Cell reference<br />';
             if (isset($matches[8])) {
                 //					echo 'Reference is a Range of cells<br />';
                 if (is_null($pCell)) {
                     //						We can't access the range, so return a REF error
                     $cellValue = PHPExcel_Calculation_Functions::REF();
                 } else {
                     $cellRef = $matches[6] . $matches[7] . ':' . $matches[9] . $matches[10];
                     if ($matches[2] > '') {
                         //							echo '$cellRef='.$cellRef.' in worksheet '.$matches[2].'<br />';
                         $this->_writeDebug($cellID, 'Evaluating Cell Range ' . $cellRef . ' in worksheet ' . $matches[2]);
                         $cellValue = $this->extractCellRange($cellRef, $pCell->getParent()->getParent()->getSheetByName($matches[2]), false);
                         $this->_writeDebug($cellID, 'Evaluation Result for cells ' . $cellRef . ' in worksheet ' . $matches[2] . ' is ' . self::_showTypeDetails($cellValue));
                     } else {
                         //							echo '$cellRef='.$cellRef.' in current worksheet<br />';
                         $this->_writeDebug($cellID, 'Evaluating Cell Range ' . $cellRef . ' in current worksheet');
                         $cellValue = $this->extractCellRange($cellRef, $pCell->getParent(), false);
                         $this->_writeDebug($cellID, 'Evaluation Result for cells ' . $cellRef . ' is ' . self::_showTypeDetails($cellValue));
                     }
                 }
             } else {
                 //					echo 'Reference is a single Cell<br />';
                 if (is_null($pCell)) {
                     //						We can't access the cell, so return a REF error
                     $cellValue = PHPExcel_Calculation_Functions::REF();
                 } else {
                     $cellRef = $matches[6] . $matches[7];
                     if ($matches[2] > '') {
                         //							echo '$cellRef='.$cellRef.' in worksheet '.$matches[2].'<br />';
                         $this->_writeDebug($cellID, 'Evaluating Cell ' . $cellRef . ' in worksheet ' . $matches[2]);
                         if ($pCell->getParent()->cellExists($cellRef)) {
                             $cellValue = $this->extractCellRange($cellRef, $pCell->getParent()->getParent()->getSheetByName($matches[2]), false);
                         } else {
                             $cellValue = null;
                         }
                         $this->_writeDebug($cellID, 'Evaluation Result for cell ' . $cellRef . ' in worksheet ' . $matches[2] . ' is ' . self::_showTypeDetails($cellValue));
                     } else {
                         //							echo '$cellRef='.$cellRef.' in current worksheet<br />';
                         $this->_writeDebug($cellID, 'Evaluating Cell ' . $cellRef . ' in current worksheet');
                         if ($pCell->getParent()->cellExists($cellRef)) {
                             $cellValue = $pCell->getParent()->getCell($cellRef)->getCalculatedValue(false);
                         } else {
                             $cellValue = '';
                         }
                         $this->_writeDebug($cellID, 'Evaluation Result for cell ' . $cellRef . ' is ' . self::_showTypeDetails($cellValue));
                     }
                 }
             }
             $stack->push($cellValue);
             // if the token is a function, pop arguments off the stack, hand them to the function, and push the result back on
         } elseif (preg_match('/^' . self::CALCULATION_REGEXP_FUNCTION . '$/i', $token, $matches)) {
             //				echo 'Token is a function<br />';
             $functionName = $matches[1];
             $argCount = $stack->pop();
             if ($functionName != 'MKMATRIX') {
                 $this->_writeDebug($cellID, 'Evaluating Function ' . $functionName . '() with ' . ($argCount == 0 ? 'no' : $argCount) . ' argument' . ($argCount == 1 ? '' : 's'));
             }
             if (array_key_exists($functionName, $this->_controlFunctions) || array_key_exists($functionName, $this->_PHPExcelFunctions)) {
                 // function
                 if (array_key_exists($functionName, $this->_controlFunctions)) {
                     $functionCall = $this->_controlFunctions[$functionName]['functionCall'];
                 } elseif (array_key_exists($functionName, $this->_PHPExcelFunctions)) {
                     $functionCall = $this->_PHPExcelFunctions[$functionName]['functionCall'];
                 }
                 // get the arguments for this function
                 //					echo 'Function '.$functionName.' expects '.$argCount.' arguments<br />';
                 $args = array();
                 for ($i = $argCount; $i > 0; --$i) {
                     $arg = $stack->pop();
                     //						if (is_null($arg)) return $this->_raiseFormulaError("internal error");
                     $args[$i] = $arg;
                 }
                 //	Reverse the order of the arguments
                 ksort($args);
                 //					echo 'Arguments are: ';
                 //					print_r($args);
                 //					echo '<br />';
                 if ($functionName != 'MKMATRIX') {
                     $argArrayVals = array();
                     foreach ($args as &$arg) {
                         $argArrayVals[] = self::_showValue($arg);
                         $arg = self::_unwrapResult($arg);
                     }
                     unset($arg);
                     $this->_writeDebug($cellID, 'Evaluating ' . $functionName . '( ' . implode(', ', $argArrayVals) . ' )');
                 }
                 //	Process each argument in turn, building the return value as an array
                 //					if (($argCount == 1) && (is_array($args[1])) && ($functionName != 'MKMATRIX')) {
                 //						$operand1 = $args[1];
                 //						$this->_writeDebug($cellID,'Argument is a matrix: '.self::_showValue($operand1));
                 //						$result = array();
                 //						$row = 0;
                 //						foreach($operand1 as $args) {
                 //							if (is_array($args)) {
                 //								foreach($args as $arg) {
                 //									$this->_writeDebug($cellID,'Evaluating '. $functionName.'( '.self::_showValue($arg).' )');
                 //									$r = call_user_func_array($functionCall,$arg);
                 //									$this->_writeDebug($cellID,'Evaluation Result is '.self::_showTypeDetails($r));
                 //									$result[$row][] = $r;
                 //								}
                 //								++$row;
                 //							} else {
                 //								$this->_writeDebug($cellID,'Evaluating '. $functionName.'( '.self::_showValue($args).' )');
                 //								$r = call_user_func_array($functionCall,$args);
                 //								$this->_writeDebug($cellID,'Evaluation Result is '.self::_showTypeDetails($r));
                 //								$result[] = $r;
                 //							}
                 //						}
                 //					} else {
                 //	Process the argument with the appropriate function call
                 $result = call_user_func_array($functionCall, $args);
                 //					}
                 if ($functionName != 'MKMATRIX') {
                     $this->_writeDebug($cellID, 'Evaluation Result is ' . self::_showTypeDetails($result));
                 }
                 $stack->push(self::_wrapResult($result));
             }
         } else {
             // if the token is a number, boolean, string or an Excel error, push it onto the stack
             if (array_key_exists(strtoupper($token), $this->_ExcelConstants)) {
                 $excelConstant = strtoupper($token);
                 //					echo 'Token is a PHPExcel constant: '.$excelConstant.'<br />';
                 $stack->push($this->_ExcelConstants[$excelConstant]);
                 $this->_writeDebug($cellID, 'Evaluating Constant ' . $excelConstant . ' as ' . self::_showTypeDetails($this->_ExcelConstants[$excelConstant]));
             } elseif (is_null($token) || $token == '' || is_bool($token) || is_numeric($token) || $token[0] == '"' || $token[0] == '#') {
                 //					echo 'Token is a number, boolean, string or an Excel error<br />';
                 $stack->push($token);
                 // if the token is a constant, push the constant value on the stack
             } elseif (preg_match('/^' . self::CALCULATION_REGEXP_NAMEDRANGE . '$/i', $token, $matches)) {
                 //					echo 'Token is a named range<br />';
                 $namedRange = $matches[6];
                 //					echo 'Named Range is '.$namedRange.'<br />';
                 $this->_writeDebug($cellID, 'Evaluating Named Range ' . $namedRange);
                 $cellValue = $this->extractNamedRange($namedRange, $pCell->getParent(), false);
                 $this->_writeDebug($cellID, 'Evaluation Result for named range ' . $namedRange . ' is ' . self::_showTypeDetails($cellValue));
                 $stack->push($cellValue);
             } else {
                 return $this->_raiseFormulaError("undefined variable '{$token}'");
             }
         }
     }
     // when we're out of tokens, the stack should have a single element, the final result
     if ($stack->count() != 1) {
         return $this->_raiseFormulaError("internal error");
     }
     $output = $stack->pop();
     if (is_array($output) && self::$returnArrayAsType != self::RETURN_ARRAY_AS_ARRAY) {
         return array_unshift(array_unshift($output));
     }
     return $output;
 }
Пример #7
0
 /**
  * Extract range values
  *
  * @param	string				&$pRange	String based range representation
  * @param	PHPExcel_Worksheet	$pSheet		Worksheet
  * @return  mixed				Array of values in range if range contains more than one element. Otherwise, a single value is returned.
  * @param	boolean				$resetLog	Flag indicating whether calculation log should be reset or not
  * @throws	PHPExcel_Calculation_Exception
  */
 public function extractNamedRange(&$pRange = 'A1', PHPExcel_Worksheet $pSheet = null, $resetLog = true)
 {
     // Return value
     $returnValue = array();
     if ($pSheet !== null) {
         $pSheetName = $pSheet->getTitle();
         if (strpos($pRange, '!') !== false) {
             list($pSheetName, $pRange) = PHPExcel_Worksheet::extractSheetTitle($pRange, true);
             $pSheet = $this->_workbook->getSheetByName($pSheetName);
         }
         // Named range?
         $namedRange = PHPExcel_NamedRange::resolveRange($pRange, $pSheet);
         if ($namedRange !== null) {
             $pSheet = $namedRange->getWorksheet();
             $pRange = $namedRange->getRange();
             $splitRange = PHPExcel_Cell::splitRange($pRange);
             //	Convert row and column references
             if (ctype_alpha($splitRange[0][0])) {
                 $pRange = $splitRange[0][0] . '1:' . $splitRange[0][1] . $namedRange->getWorksheet()->getHighestRow();
             } elseif (ctype_digit($splitRange[0][0])) {
                 $pRange = 'A' . $splitRange[0][0] . ':' . $namedRange->getWorksheet()->getHighestColumn() . $splitRange[0][1];
             }
         } else {
             return PHPExcel_Calculation_Functions::REF();
         }
         // Extract range
         $aReferences = PHPExcel_Cell::extractAllCellReferencesInRange($pRange);
         if (!isset($aReferences[1])) {
             //	Single cell (or single column or row) in range
             list($currentCol, $currentRow) = PHPExcel_Cell::coordinateFromString($aReferences[0]);
             $cellValue = null;
             if ($pSheet->cellExists($aReferences[0])) {
                 $returnValue[$currentRow][$currentCol] = $pSheet->getCell($aReferences[0])->getCalculatedValue($resetLog);
             } else {
                 $returnValue[$currentRow][$currentCol] = null;
             }
         } else {
             // Extract cell data for all cells in the range
             foreach ($aReferences as $reference) {
                 // Extract range
                 list($currentCol, $currentRow) = PHPExcel_Cell::coordinateFromString($reference);
                 $cellValue = null;
                 if ($pSheet->cellExists($reference)) {
                     $returnValue[$currentRow][$currentCol] = $pSheet->getCell($reference)->getCalculatedValue($resetLog);
                 } else {
                     $returnValue[$currentRow][$currentCol] = null;
                 }
             }
         }
     }
     // Return
     return $returnValue;
 }