Articles in this section

How to use custom function in XlsIO

Syncfusion does not support Add-in functions in CalcEngine but provides support for adding Custom Functions as an alternative solution.

The following complete code snippet explains how to add a custom function.

CalcEngine engine;
private void button1_Click(object sender, EventArgs e)
{
    using (ExcelEngine excelEngine = new ExcelEngine())
    {
        IApplication application = excelEngine.Excel;
        IWorkbook workbook = application.Workbooks.Open("../../Data/Sample.xlsx");
        IWorksheet worksheet = workbook.Worksheets[0];
 
        // Class derived from ICalcData,
        CalcData calcData = new CalcData();
        // Set value from A1 cell to variable "A1"
        calcData.SetValueRowCol(worksheet["A1"].Value, 1, 1);
        // Set value from B1 cell to variable "B1"
        calcData.SetValueRowCol(worksheet["B1"].Value, 1, 2);
 
        engine = new CalcEngine(calcData);
        engine.AddFunction("CustomSum", new CalcEngine.LibraryFunction(CustomSum));
 
        worksheet["C1"].Value = engine.ParseAndComputeFormula("=CustomSum(A1,A1)");
        worksheet["D1"].Value = engine.ParseAndComputeFormula("=CustomSum(A1,B1)");
 
        MessageBox.Show(worksheet["C1"].Value);
        MessageBox.Show(worksheet["D1"].Value);
 
        workbook.SaveAs("../../Output/Output.xlsx");
    }
}
 
public string CustomSum(string args)
{
    double sum = 0;
    double d;
    var splitArgs = args.Split(new char[] { CalcEngine.ParseArgumentSeparator });
    foreach (string s in splitArgs)
    {
        var s1 = engine.GetValueFromArg(s);
        if (double.TryParse(s1, NumberStyles.Number | NumberStyles.AllowExponent, null, out d))
            sum += d;
    }
    return sum.ToString();
}
 
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));
    }
}

A complete working sample can be downloaded from AddCustmFunction.Zip.


Take a moment to peruse the documentation, where you can find basic worksheet data manipulation options along with features like Conditional Formatting, worksheet calculations through formulas, adding charts in worksheets or workbooks, organizing and analyzing data through tables and pivot tables, appending multiple records to a worksheet using template markers, and most importantly, PDF and image conversions, etc., with code examples.

Refer here to explore the rich set of Syncfusion Essential XlsIO features.

 

Note:

Starting with v16.2.0.x, if you reference Syncfusion assemblies from a trial setup or from the NuGet feed, include a license key in your projects. Refer to this link to learn about generating and registering the Syncfusion license key in your application to use the components without a trial message.

 

Conclusion

I hope you enjoyed learning about how to use custom function in XlsIO.

You can refer to our XIsIO’s feature tour page to learn about its other groundbreaking features. Explore our UG documentation and online demos to understand how to manipulate data in Excel documents. 

If you are an existing user, you can access our latest components from the License and Downloads page. For new users, you can try our 30-day free trial to check out XlsIO and other Syncfusion components.

If you have any queries or require clarification, please let us know in the comments below or contact us through our support forums or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Access denied
Access denied