You are here

private function PHPExcel_Reader_Excel5::_createFormulaFromTokens in Loft Data Grids 6.2

Same name and namespace in other branches
  1. 7.2 vendor/phpoffice/phpexcel/Classes/PHPExcel/Reader/Excel5.php \PHPExcel_Reader_Excel5::_createFormulaFromTokens()

* Take array of tokens together with additional data for formula and return human readable formula * *

Parameters

array $tokens: * @param array $additionalData Additional binary data going with the formula * @param string $baseCell Base cell, only needed when formula contains tRefN tokens, e.g. with shared formulas * @return string Human readable formula

1 call to PHPExcel_Reader_Excel5::_createFormulaFromTokens()
PHPExcel_Reader_Excel5::_getFormulaFromData in vendor/phpoffice/phpexcel/Classes/PHPExcel/Reader/Excel5.php
* Take formula data and additional data for formula and return human readable formula * *

File

vendor/phpoffice/phpexcel/Classes/PHPExcel/Reader/Excel5.php, line 5283

Class

PHPExcel_Reader_Excel5
PHPExcel_Reader_Excel5

Code

private function _createFormulaFromTokens($tokens, $additionalData) {

  // empty formula?
  if (empty($tokens)) {
    return '';
  }
  $formulaStrings = array();
  foreach ($tokens as $token) {

    // initialize spaces
    $space0 = isset($space0) ? $space0 : '';

    // spaces before next token, not tParen
    $space1 = isset($space1) ? $space1 : '';

    // carriage returns before next token, not tParen
    $space2 = isset($space2) ? $space2 : '';

    // spaces before opening parenthesis
    $space3 = isset($space3) ? $space3 : '';

    // carriage returns before opening parenthesis
    $space4 = isset($space4) ? $space4 : '';

    // spaces before closing parenthesis
    $space5 = isset($space5) ? $space5 : '';

    // carriage returns before closing parenthesis
    switch ($token['name']) {
      case 'tAdd':

      // addition
      case 'tConcat':

      // addition
      case 'tDiv':

      // division
      case 'tEQ':

      // equality
      case 'tGE':

      // greater than or equal
      case 'tGT':

      // greater than
      case 'tIsect':

      // intersection
      case 'tLE':

      // less than or equal
      case 'tList':

      // less than or equal
      case 'tLT':

      // less than
      case 'tMul':

      // multiplication
      case 'tNE':

      // multiplication
      case 'tPower':

      // power
      case 'tRange':

      // range
      case 'tSub':

        // subtraction
        $op2 = array_pop($formulaStrings);
        $op1 = array_pop($formulaStrings);
        $formulaStrings[] = "{$op1}{$space1}{$space0}{$token['data']}{$op2}";
        unset($space0, $space1);
        break;
      case 'tUplus':

      // unary plus
      case 'tUminus':

        // unary minus
        $op = array_pop($formulaStrings);
        $formulaStrings[] = "{$space1}{$space0}{$token['data']}{$op}";
        unset($space0, $space1);
        break;
      case 'tPercent':

        // percent sign
        $op = array_pop($formulaStrings);
        $formulaStrings[] = "{$op}{$space1}{$space0}{$token['data']}";
        unset($space0, $space1);
        break;
      case 'tAttrVolatile':

      // indicates volatile function
      case 'tAttrIf':
      case 'tAttrSkip':
      case 'tAttrChoose':

        // token is only important for Excel formula evaluator
        // do nothing
        break;
      case 'tAttrSpace':

        // space / carriage return
        // space will be used when next token arrives, do not alter formulaString stack
        switch ($token['data']['spacetype']) {
          case 'type0':
            $space0 = str_repeat(' ', $token['data']['spacecount']);
            break;
          case 'type1':
            $space1 = str_repeat("\n", $token['data']['spacecount']);
            break;
          case 'type2':
            $space2 = str_repeat(' ', $token['data']['spacecount']);
            break;
          case 'type3':
            $space3 = str_repeat("\n", $token['data']['spacecount']);
            break;
          case 'type4':
            $space4 = str_repeat(' ', $token['data']['spacecount']);
            break;
          case 'type5':
            $space5 = str_repeat("\n", $token['data']['spacecount']);
            break;
        }
        break;
      case 'tAttrSum':

        // SUM function with one parameter
        $op = array_pop($formulaStrings);
        $formulaStrings[] = "{$space1}{$space0}SUM({$op})";
        unset($space0, $space1);
        break;
      case 'tFunc':

      // function with fixed number of arguments
      case 'tFuncV':

        // function with variable number of arguments
        if ($token['data']['function'] != '') {

          // normal function
          $ops = array();

          // array of operators
          for ($i = 0; $i < $token['data']['args']; ++$i) {
            $ops[] = array_pop($formulaStrings);
          }
          $ops = array_reverse($ops);
          $formulaStrings[] = "{$space1}{$space0}{$token['data']['function']}(" . implode(',', $ops) . ")";
          unset($space0, $space1);
        }
        else {

          // add-in function
          $ops = array();

          // array of operators
          for ($i = 0; $i < $token['data']['args'] - 1; ++$i) {
            $ops[] = array_pop($formulaStrings);
          }
          $ops = array_reverse($ops);
          $function = array_pop($formulaStrings);
          $formulaStrings[] = "{$space1}{$space0}{$function}(" . implode(',', $ops) . ")";
          unset($space0, $space1);
        }
        break;
      case 'tParen':

        // parenthesis
        $expression = array_pop($formulaStrings);
        $formulaStrings[] = "{$space3}{$space2}({$expression}{$space5}{$space4})";
        unset($space2, $space3, $space4, $space5);
        break;
      case 'tArray':

        // array constant
        $constantArray = self::_readBIFF8ConstantArray($additionalData);
        $formulaStrings[] = $space1 . $space0 . $constantArray['value'];
        $additionalData = substr($additionalData, $constantArray['size']);

        // bite of chunk of additional data
        unset($space0, $space1);
        break;
      case 'tMemArea':

        // bite off chunk of additional data
        $cellRangeAddressList = $this
          ->_readBIFF8CellRangeAddressList($additionalData);
        $additionalData = substr($additionalData, $cellRangeAddressList['size']);
        $formulaStrings[] = "{$space1}{$space0}{$token['data']}";
        unset($space0, $space1);
        break;
      case 'tArea':

      // cell range address
      case 'tBool':

      // boolean
      case 'tErr':

      // error code
      case 'tInt':

      // integer
      case 'tMemErr':
      case 'tMemFunc':
      case 'tMissArg':
      case 'tName':
      case 'tNameX':
      case 'tNum':

      // number
      case 'tRef':

      // single cell reference
      case 'tRef3d':

      // 3d cell reference
      case 'tArea3d':

      // 3d cell range reference
      case 'tRefN':
      case 'tAreaN':
      case 'tStr':

        // string
        $formulaStrings[] = "{$space1}{$space0}{$token['data']}";
        unset($space0, $space1);
        break;
    }
  }
  $formulaString = $formulaStrings[0];

  // for debug: dump the human readable formula

  //echo '----' . "\n";

  //echo 'Formula: ' . $formulaString;
  return $formulaString;
}