Getting Started

This section helps you to get started with Essential Calculate.

Adding Calculate reference

You can add Calculate reference using one of the following methods:

Method 1: Adding Calculate reference from nuget.org

Syncfusion Xamarin components are available in nuget.org. To add Calculate to your project, open the NuGet package manager in Visual Studio, search for Syncfusion.Xamarin.Calculate, and then install it.

Adding Calculate reference from nuget

NOTE

Install the same version of Calculate NuGet in all the projects.

Method 2: Adding Calculate reference from toolbox

Syncfusion also provides Xamarin Toolbox. Using this toolbox, you can drag the Calculate control to the XAML page. It will automatically install the required NuGet packages and add the namespace to the page. To install Syncfusion Xamarin Toolbox, refer to Toolbox.

Method 3: Adding Calculate assemblies manually from the installed location

If you prefer to manually reference the assemblies instead referencing from NuGet, add the following assemblies in respective projects.

Location: {Installed location}/{version}/Xamarin/lib

PCL Syncfusion.Calculate.Portable.dll
Syncfusion.Licensing.dll

NOTE

To know more about obtaining our components, refer to these links for Mac and Windows.

IMPORTANT

Starting with v16.2.0.x, if you reference Syncfusion assemblies from the trial setup or from the NuGet feed, you also have to include a license key in your projects. Please refer to Syncfusion license key to know about registering Syncfusion license key in your Xamarin application to use our components.

Compute formula using CalcQuickBase

The CalcQuickBase will provide options to directly parse and compute a formula, or register variable names that can later be used in more complex formulas involving these variables.
CalcQuickBase is predefined derived class from ICalcData.

Below example shows the computation of formula using ParseAndCompute method of CalcQuickBase.

CalcQuickBase calcQuick = new CalcQuickBase();   

//Computing expressions,

string formula = "(5+25)*2";
string result = calcQuick.ParseAndCompute(formula);

//Computing in built formulas,

string formula = "SUM(5,5)";
string result = calcQuick.ParseAndCompute(formula);

Compute formula using ICalcData

Essential Calculate provides calculation support to arbitrary business objects through ICalcData interface.
The methods and events used in ICalcData interface are

  • GetValueRowCol - A method that is used to get the value from mentioned row and column index.

  • SetValueRowCol - A method that is used to set the value to mentioned row and column index.

  • WireParentObject - A method that wires the ParentObject after the CalcEngine object is created or when a RegisterGridAsSheet call is made.

  • ValueChanged - An event that occurs when the value is changed.

Below example shows the computation of formula using ICalcData interface.

Creating a Class from ICalcData

Create CalcData class derived from ICalcData interface,

public class CalcData : ICalcData
{
    public event ValueChangedEventHandler ValueChanged;

    Dictionary<string, object> values = new Dictionary<string, object>();
    public object GetValueRowCol(int row, int col)
    {
        object value = null;
        var key = RangeInfo.GetAlphaLabel(col) + row;
        this.values.TryGetValue(key, out value);
        return value;
    }

    public void SetValueRowCol(object value, int row, int col)
    {
        var key = RangeInfo.GetAlphaLabel(col) + row;
        if (!values.ContainsKey(key))
            values.Add(key, value);
        else if (values.ContainsKey(key) && values[key] != value)
            values[key] = value;
    }

    public void WireParentObject(){}

    private void OnValueChanged(int row, int col, string value)
    {
        if (ValueChanged != null)
            ValueChanged(this, new ValueChangedEventArgs(row, col, value));
    }
}

Setting Value into ICalcData

The SetValueRowCol method is used to set the value to ICalcData object.

calcData.SetValueRowCol(10, 1, 1);

calcData.SetValueRowCol(20, 1, 2);

Evaluation of formula

The ICalcData object can be integrated into CalcEngine by passing it through constructor. Now, you can compute the expressions or equations using CalcEngine.

The ParseAndComputeFormula method of CalcEngine is used to evaluate the formulas using the values from ICalcData object by cell references.

calcData = new CalcData();

CalcEngine engine = new CalcEngine(calcData);

string formula = SUM (A1, B1);

string result = engine.ParseAndComputeFormula(formula);

Choosing between CalcQuickBase and ICalcData

CalcQuickBase

The simplest way to use Essential Calculate is through an instance of its CalcQuickBase class. This class provides options to directly parse and compute a formula, or register variable names that can later be used in more complex formulas involving these variables. But we cannot change the values of the variables at runtime for computation. Also by default, CalcQuickBase does not try to track any dependencies among the variables you set, hence to enable automatic recalculation of dependent variables, users need to set the AutoCalc property to True.

For more information regarding calculating with CalcQuickBase, refer here

ICalcData

To use Essential Calculate support, we need to derive the class from ICalcData interface which allows the CalcEngine class to communicate with arbitrary data sources.
To add calculation support to classes that represent data in a row/column format like a Data Grid, then you need to derive the classes inherited from ICalcData interface. Since GetValueRowCol and SetValueRowCol methods
of ICalcData interface is used to get and set the values of the variables at runtime for computation. CalcEngine listens to the ValueChanged event of ICalcData interface to tracks the dependencies and compute the formulas.

For more information regarding calculating with ICalcData, refer here

Cross Sheet Reference

CalcEngine provides support to perform the calculation by accessing the values from the different sheets using RegisterGridAsSheet method.
This method registers an ICalcData object so it can be referenced in a formula with another ICalcData object. CreateSheetFamilyID method of CalcEngine is used to create a unique family identifier for the sheet.
In RegisterGridAsSheet method, while registering the ICalcData objects, users need to pass this unique identifier to mark the ICalcData objects are belonging
to this family. Also cross reference of ICalcData objects can be done within the same family.

The class which is derived from ICalcData can be registered as a worksheet for identifying the cell references.

Below code illustrates the registering of two ICalcData objects and use the cell references from two objects for computation of formula.

//Initialization of first ICalcData object in CalcEngine,
calcData = new CalcData();
CalcEngine engine = new CalcEngine(calcData);

//Initialization of second ICalcData object in CalcEngine,
calcData1 = new calcData1();
engine = new CalcEngine(calcData1);

//Create a unique family id,
int i = CalcEngine.CreateSheetFamilyID();

//Register the first ICalcData object as "Sheet1",
engine.RegisterGridAsSheet("Sheet1", calcData, i);

//Register the second ICalcData object as "Sheet2",
engine.RegisterGridAsSheet("Sheet2", calcData1, i);

//Access the two sheet references in the formula,
string formula = SUM(Sheet1!A1, Sheet2!A1);

//Computation of result,
string result = engine.ParseAndComputeFormula(formula);

Compute formulas in different region settings

Essential Calculate have support to compute the formulas in different region settings. The two static members such as ParseArgumentSeparator and ParseDecimalSeparator of CalcEngine class is used to set the separators based on local region settings. By default, the value of ParseArgumentSeparator is comma(,) and the value of ParseDecimalSeparator is dot(.).

//Assign the current culture's decimal separator,
CalcEngine.ParseDecimalSeparator = System.Threading.Thread.CurrentThread.CurrentCulture.NumberFormat.NumberDecimalSeparator.ToCharArray()[0];

//Assign the current culture's argument separator,
CalcEngine.ParseArgumentSeparator = System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ListSeparator.ToCharArray()[0];