public function formulaToPosfijo($formula){ $formulaLenght = strlen(substr($formula, 1)); if($formulaLenght > 1){ $parse = new FormulaParser($formula); $pila = array(); // Variable auxiliar para transformar a posfijo $pFormula = array(); // resultado, la formula en posfijo. for ($i = 0; $i < $parse->getTokenCount(); $i++) { $token = $parse->getToken($i); switch($token->getTokenType()){ case FormulaToken::TOKEN_TYPE_OPERAND: // Si es un operando: numero o rango. array_push($pFormula, $token->getValue()); break; case FormulaToken::TOKEN_TYPE_SUBEXPRESSION: // Si es un parentesis if($token->getTokenSubType() == FormulaToken::TOKEN_SUBTYPE_START){ // Si es parentesis izquierdo array_push($pila, $token->getValue()); }else{ do{ $signo = array_pop($pila); array_puch($pFormula, $signo); }while(count($pila) >= 1 && $signo != "(" ); } break; case FormulaToken::TOKEN_TYPE_OPERATORINFIX: // Mientras la pila no este vacía y su cima sea un operador de precedencia mayor o igual que la del token while(count($pila) >= 1 && in_array($pila[0], $this->operatorPrecedence) == true && $this->operatorPrecedence[$pila[0]] >= $this->operatorPrecedence[$token->getValue()]){ //$signo = array_pop($pila); array_push($pFormula, array_pop($pila)); } array_push($pila, $token->getValue()); break; } } foreach($pila as $signo){ array_push($pFormula, array_pop($pila)); } $formula = implode($pFormula); } utils::pre($formula); return $formula; }
protected function packFormula($formula) { if ($this->dataType === self::TYPE_USER_LIST) { $formula = str_replace(',', chr(0), $formula); } if ($formula != '') { $formula = $this->formulaParser->getReversePolish($formula); } return pack("vv", strlen($formula), 0x0) . $formula; }
/** * Write a formula to the specified row and column (zero indexed). * The textual representation of the formula is passed to the formula parser * which returns a packed binary string. * * @param integer $row Zero indexed row * @param integer $col Zero indexed column * @param string $formula The formula text string * @param mixed $format The optional XF format * @throws \Exception */ public function writeFormula($row, $col, $formula, $format = null) { $this->addCell($row, $col); // Strip the '=' or '@' sign at the beginning of the formula string if (in_array($formula[0], array('=', '@'), true)) { $formula = substr($formula, 1); } else { throw new \Exception('Invalid formula: should start with = or @'); } $formula = $this->formulaParser->getReversePolish($formula); $this->appendRecord('Formula', array($row, $col, $formula, $format)); }