53 if (!defined(
'PHPEXCEL_ROOT')) {
57 define(
'PHPEXCEL_ROOT', dirname(__FILE__) .
'/../../');
61 require_once PHPEXCEL_ROOT .
'PHPExcel/Calculation/FormulaToken.php';
112 if (is_null($pFormula)) {
113 throw new Exception(
"Invalid parameter passed: formula");
117 $this->_formula = trim($pFormula);
139 if (isset($this->_tokens[$pId])) {
140 return $this->_tokens[$pId];
142 throw new Exception(
"Token with id $pId does not exist.");
152 return count($this->_tokens);
172 $formulaLength = strlen($this->_formula);
173 if ($formulaLength < 2 || $this->_formula{0} !=
'=')
return;
176 $tokens1 = $tokens2 = $stack = array();
177 $inString = $inPath = $inRange = $inError =
false;
178 $token = $previousToken = $nextToken = null;
183 $ERRORS = array(
"#NULL!",
"#DIV/0!",
"#VALUE!",
"#REF!",
"#NAME?",
"#NUM!",
"#N/A");
184 $COMPARATORS_MULTI = array(
">=",
"<=",
"<>");
186 while ($index < $formulaLength) {
203 $value .= $this->_formula{$index};
221 $value .= $this->_formula{$index};
234 $value .= $this->_formula{$index};
242 $value .= $this->_formula{$index};
244 if (in_array($value, $ERRORS)) {
254 if (strlen($value) > 1) {
255 if (preg_match(
"/^[1-9]{1}(\.[0-9]+)?E{1}$/", $this->_formula{$index}) != 0) {
256 $value .= $this->_formula{$index};
267 if (strlen($value > 0)) {
277 if (strlen($value) > 0) {
294 if (strlen($value) > 0) {
306 if (strlen($value) > 0) {
313 $stack[] = clone $tmp;
317 $stack[] = clone $tmp;
324 if (strlen($value) > 0) {
329 $tmp = array_pop($stack);
339 $stack[] = clone $tmp;
346 if (strlen($value) > 0) {
351 $tmp = array_pop($stack);
356 $tmp = array_pop($stack);
367 if (strlen($value) > 0) {
380 if (($index + 2) <= $formulaLength) {
381 if (in_array(substr($this->_formula, $index, 2), $COMPARATORS_MULTI)) {
382 if (strlen($value) > 0) {
394 if (strlen($value) > 0) {
405 if (strlen($value) > 0) {
416 if (strlen($value) > 0) {
419 $stack[] = clone $tmp;
424 $stack[] = clone $tmp;
432 if (strlen($value) > 0) {
437 $tmp = array_pop($stack);
453 if (strlen($value) > 0) {
458 $tmp = array_pop($stack);
468 $value .= $this->_formula{$index};
473 if (strlen($value) > 0) {
478 $tokenCount = count($tokens1);
479 for ($i = 0; $i < $tokenCount; ++$i) {
480 $token = $tokens1[$i];
481 if (isset($tokens1[$i - 1])) {
482 $previousToken = $tokens1[$i - 1];
484 $previousToken = null;
486 if (isset($tokens1[$i + 1])) {
487 $nextToken = $tokens1[$i + 1];
492 if (is_null($token)) {
501 if (is_null($previousToken)) {
513 if (is_null($nextToken)) {
530 $this->_tokens = array();
532 $tokenCount = count($tokens2);
533 for ($i = 0; $i < $tokenCount; ++$i) {
534 $token = $tokens2[$i];
535 if (isset($tokens2[$i - 1])) {
536 $previousToken = $tokens2[$i - 1];
538 $previousToken = null;
540 if (isset($tokens2[$i + 1])) {
541 $nextToken = $tokens2[$i + 1];
546 if (is_null($token)) {
564 $this->_tokens[] = $token;
582 $this->_tokens[] = $token;
587 if (strpos(
"<>=", substr($token->getValue(), 0, 1)) !==
false) {
589 }
else if ($token->getValue() ==
"&") {
595 $this->_tokens[] = $token;
600 if (!is_numeric($token->getValue())) {
601 if (strtoupper($token->getValue()) ==
"TRUE" || strtoupper($token->getValue() ==
"FALSE")) {
610 $this->_tokens[] = $token;
615 if (strlen($token->getValue() > 0)) {
616 if (substr($token->getValue(), 0, 1) ==
"@") {
617 $token->setValue(substr($token->getValue(), 1));
622 $this->_tokens[] = $token;