API

evaluateExpression()

Returns an int value and has the following parameters:

  • FormulaField expressionField
  • [, CalculationContext calculationContext]

int evaluateExpression(FormulaField expressionField [, CalculationContext calculationContext])

This method evaluates the expressionField formula, which is called an expression because it doesn't refer to any input or formula field. The optional CalculationContext parameter's HashMap of named TableArray objects is used in expressions that reference lookup functions. This parameter's udfClassName property is used for the expressions that reference user defined functions. This method compiles the expression prior to the evaluation. If the evaluation of the expression failes this method returns a non-zero error code and stores it in the expressionField.

The first example shows how to evaluate the following expression:

AVERAGE(12, 10, 35, 28)

import com.crystalprism.ce.formula.HybridFormulaEvaluator;
import com.crystalprism.ce.usermodel.ComputeFieldType;
import com.crystalprism.ce.usermodel.FormulaField;

public class EvaluateExpressionExample1 {
    public static void main(String[] args) {
        String myExpressionText = "AVERAGE(12, 10, 35, 28)";
        FormulaField myExpressionField = new FormulaField(ComputeFieldType.NUMERIC, myExpressionText);;

        HybridFormulaEvaluator.evaluateExpression(myExpressionField);
        System.out.println("Calculated result: " + myExpressionField.getDisplayValue());
    }
}

Calculated result: 21.25

The second example shows how to evaluate the following VLOOKUP expression, which uses a TableArray object named "my_table":

VLOOKUP(20, "my_table", 2, 0) 

import com.crystalprism.ce.formula.HybridFormulaEvaluator;
import com.crystalprism.ce.usermodel.*;

public class EvaluateExpressionExample2 {
    public static void main(String[] args) {
        Object[][] tableArrayBase = {{10, "Apple"},
                                     {20, "Pear"},
                                     {30, "Orange"}};
        TableArray tableArray = new TableArray(tableArrayBase);
        String myExpressionText = "VLOOKUP(20, \"my_table\", 2, 0)";
        FormulaField myExpressionField = new FormulaField(ComputeFieldType.ALPHANUMERIC, myExpressionText);
        CalculationContext calculationContext = new CalculationContext("EvaluateExpressionExample2");

        calculationContext.put("my_table", tableArray);
        HybridFormulaEvaluator.evaluateExpression(myExpressionField, calculationContext);
        System.out.println("Calculated result: " + myExpressionField.getDisplayValue());
    }
}

Calculated result: Pear

The third example intentionally initialises the myExpressionText variable with an incorrect expression text, where the TableArray object name my_table is put in single quotes. This produces an error code 102, corresponding to a single quoted operand condition:

import com.crystalprism.ce.formula.HybridFormulaEvaluator;
import com.crystalprism.ce.usermodel.*;

public class EvaluateExpressionExample3 {
    public static void main(String[] args) {
        Object[][] tableArrayBase = {{10, "Apple"},
                                     {20, "Pear"},
                                     {30, "Orange"}};
        TableArray tableArray = new TableArray(tableArrayBase);
        String myExpressionText = "VLOOKUP(20, 'my_table', 2, 0)";
        FormulaField myExpressionField = new FormulaField(ComputeFieldType.ALPHANUMERIC, myExpressionText);
        CalculationContext calculationContext = new CalculationContext("EvaluateExpressionExample3");

        calculationContext.put("my_table", tableArray);
        int rc = HybridFormulaEvaluator.evaluateExpression(myExpressionField, calculationContext);
        System.out.println("Evaluation return code: " + rc);
    }
}

Evaluation return code: 102