⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stringformulaparser.java

📁 jxtl API Java中Excel的生成与导入解析参考文档
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    return tokens;
  }

  /**
   * Gets the formula as a string.  Uses the parse tree to do this, and
   * does not simply return whatever string was passed in
   */
  public String getFormula()
  {
    if (parsedFormula == null)
    {
      StringBuffer sb = new StringBuffer();
      root.getString(sb);
      parsedFormula = sb.toString();
    }

    return parsedFormula;
  }

  /**
   * Gets the bytes for the formula
   *
   * @return the bytes in RPN
   */
  public byte[] getBytes()
  {
    byte[] bytes = root.getBytes();
    
    if (root.isVolatile())
    {
      byte[] newBytes = new byte[bytes.length + 4];
      System.arraycopy(bytes, 0, newBytes, 4, bytes.length);
      newBytes[0] = Token.ATTRIBUTE.getCode();
      newBytes[1] = (byte) 0x1;
      bytes = newBytes;
    }

    return bytes;
  }

  /**
   * Handles the case when parsing a string when a token is a function
   *
   * @param sf the string function
   * @param i  the token iterator
   * @param stack the parse tree stack
   * @exception FormulaException if an error occurs
   */
  private void handleFunction(StringFunction sf, Iterator i, 
                              Stack stack)
    throws FormulaException
  {
    ParseItem pi2 = parseCurrent(i); 

    // If the function is unknown, then throw an error
    if (sf.getFunction(settings) == Function.UNKNOWN)
    {
      throw new FormulaException(FormulaException.UNRECOGNIZED_FUNCTION);
    }

    // First check for possible optimized functions and possible
    // use of the Attribute token
    if (sf.getFunction(settings) == Function.SUM && arguments == null)
    {
      // this is handled by an attribute
      Attribute a = new Attribute(sf, settings);
      a.add(pi2);
      stack.push(a);
      return;
    }

    if (sf.getFunction(settings) == Function.IF)
    {
      // this is handled by an attribute
      Attribute a = new Attribute(sf, settings);
          
      // Add in the if conditions as a var arg function in
      // the correct order
      VariableArgFunction vaf = new VariableArgFunction(settings);
      int numargs = arguments.size();
      for (int j = 0 ; j < numargs; j++)
      {
        ParseItem pi3 = (ParseItem) arguments.get(j);
        vaf.add(pi3);
      }
          
      a.setIfConditions(vaf);
      stack.push(a);
      return;
    }

    // Function cannot be optimized.  See if it is a variable argument 
    // function or not
    if (sf.getFunction(settings).getNumArgs() == 0xff)
    {
      // If the arg stack has not been initialized, it means
      // that there was only one argument, which is the
      // returned parse item
      if (arguments == null)
      {
        int numArgs = pi2 != null? 1:0;
        VariableArgFunction vaf = new VariableArgFunction
          (sf.getFunction(settings), numArgs, settings);

        if (pi2 != null)
        {
          vaf.add(pi2);
        }

        stack.push(vaf);
      }
      else
      {
        // Add the args to the function in the correct order
        int numargs = arguments.size();
        VariableArgFunction vaf = new VariableArgFunction
          (sf.getFunction(settings), numargs, settings);
        
        ParseItem[] args = new ParseItem[numargs];
        for (int j = 0 ; j < numargs; j++)
        {
          ParseItem pi3 = (ParseItem) arguments.pop();
          args[numargs-j-1] = pi3;
        }

        for (int j = 0 ; j < args.length ; j++)
        {
          vaf.add(args[j]);
        }
        stack.push(vaf);
        arguments.clear();
        arguments = null;
      }
      return;
    }

    // Function is a standard built in function
    BuiltInFunction bif = new BuiltInFunction(sf.getFunction(settings), 
                                              settings);
      
    int numargs = sf.getFunction(settings).getNumArgs();
    if (numargs == 1)
    {
      // only one item which is the returned ParseItem
      bif.add(pi2);
    }
    else
    {
      if ((arguments == null && numargs != 0) ||
          (arguments != null && numargs != arguments.size()))
      {
        throw new FormulaException(FormulaException.INCORRECT_ARGUMENTS);
      }
      // multiple arguments so go to the arguments stack.  
      // Unlike the variable argument function, the args are
      // stored in reverse order
      for (int j = 0; j < numargs ; j++)
      {
        ParseItem pi3 = (ParseItem) arguments.get(j);
        bif.add(pi3);
      }
    }
    stack.push(bif);
  }

  /**
   * Default behaviour is to do nothing
   *
   * @param colAdjust the amount to add on to each relative cell reference
   * @param rowAdjust the amount to add on to each relative row reference
   */
  public void adjustRelativeCellReferences(int colAdjust, int rowAdjust)
  {
    root.adjustRelativeCellReferences(colAdjust, rowAdjust);
  }

  /**
   * Called when a column is inserted on the specified sheet.  Tells
   * the formula  parser to update all of its cell references beyond this
   * column
   *
   * @param sheetIndex the sheet on which the column was inserted
   * @param col the column number which was inserted
   * @param currentSheet TRUE if this formula is on the sheet in which the
   * column was inserted, FALSE otherwise
   */
  public void columnInserted(int sheetIndex, int col, boolean currentSheet)
  {
    root.columnInserted(sheetIndex, col, currentSheet);
  }


  /**
   * Called when a column is inserted on the specified sheet.  Tells
   * the formula  parser to update all of its cell references beyond this
   * column
   *
   * @param sheetIndex the sheet on which the column was removed
   * @param col the column number which was removed
   * @param currentSheet TRUE if this formula is on the sheet in which the
   * column was inserted, FALSE otherwise
   */
  public void columnRemoved(int sheetIndex, int col, boolean currentSheet)
  {
    root.columnRemoved(sheetIndex, col, currentSheet);
  }

  /**
   * Called when a column is inserted on the specified sheet.  Tells
   * the formula  parser to update all of its cell references beyond this
   * column
   *
   * @param sheetIndex the sheet on which the column was inserted
   * @param row the column number which was inserted
   * @param currentSheet TRUE if this formula is on the sheet in which the
   * column was inserted, FALSE otherwise
   */
  public void rowInserted(int sheetIndex, int row, boolean currentSheet)
  {
    root.rowInserted(sheetIndex, row, currentSheet);
  }

  /**
   * Called when a column is inserted on the specified sheet.  Tells
   * the formula  parser to update all of its cell references beyond this
   * column
   *
   * @param sheetIndex the sheet on which the column was removed
   * @param row the column number which was removed
   * @param currentSheet TRUE if this formula is on the sheet in which the
   * column was inserted, FALSE otherwise
   */
  public void rowRemoved(int sheetIndex, int row, boolean currentSheet)
  {
    root.rowRemoved(sheetIndex, row, currentSheet);
  }

  /**
   * Handles operands by pushing them onto the stack
   *
   * @param o operand
   * @param stack stack
   */
  private void handleOperand(Operand o, Stack stack)
  {
    if (!(o instanceof IntegerValue))
    {
      stack.push(o);
      return;
    }

    if (o instanceof IntegerValue)
    {
      IntegerValue iv = (IntegerValue) o;
      if (!iv.isOutOfRange())
      {
        stack.push(iv);
      }
      else
      {
        // convert to a double
        DoubleValue dv = new  DoubleValue(iv.getValue());
        stack.push(dv);
      }
    }
  }

  /**
   * If this formula was on an imported sheet, check that
   * cell references to another sheet are warned appropriately
   *
   * @return TRUE if the formula is valid import, FALSE otherwise
   */
  public boolean handleImportedCellReferences()
  {
    root.handleImportedCellReferences();
    return root.isValid();
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -