[]
        
(Showing Draft Content)

GrapeCity.Documents.Excel.IWorksheet.Evaluate

Evaluate Method

Evaluate(string)

Evaluates the specified Excel formula or named expression and returns the result.

The formula is evaluated by using non-dynamic array semantics.

Declaration
object Evaluate(string formula)
Function Evaluate(formula As String) As Object
Parameters
Type Name Description
string formula

A string containing an Excel formula, named range, or defined name to evaluate. A leading = is allowed. Must not be null or empty.

Returns
Type Description
object

The evaluated result. If the formula result is a reference, such as =A1, an IRange object is returned.

Examples
worksheet.Range["A1"].Value = 100;
object result = worksheet.Evaluate("=A1*2");
Exceptions
Type Condition
InvalidFormulaException

if formula is null, empty, or not a valid formula expression.

Evaluate(string, IFormulaResolver)

Evaluates the specified Excel formula or named expression by using a custom formula resolver and returns the result.

Use the resolver to supply values for custom names referenced by the formula. The calculation behavior is consistent with Excel 2019 and earlier versions.

Declaration
object Evaluate(string formula, IFormulaResolver resolver)
Function Evaluate(formula As String, resolver As IFormulaResolver) As Object
Parameters
Type Name Description
string formula

A string containing an Excel formula, named range, or defined name to evaluate. A leading = is allowed. Must not be null or empty.

IFormulaResolver resolver

The custom resolver used to resolve names referenced by the formula. If null, the formula is evaluated without a custom resolver.

Returns
Type Description
object

The result of the evaluation. Returns an IRange object when the formula result is a reference, such as "=A1".

Examples
class CustomFormulaResolver : IFormulaResolver
{
    public bool IsCustomName(string text)
    {
        return text == "TaxRate";
    }

    public object Evaluate(string text)
    {
        return text == "TaxRate" ? 0.08 : null;
    }
}

IFormulaResolver resolver = new CustomFormulaResolver();
object result = worksheet.Evaluate("=100*TaxRate", resolver);
Exceptions
Type Condition
InvalidFormulaException

if formula is null, empty, or not a valid formula expression.