Thank your for taking the time to create the sample project. I compiled and ran the sample example from https://www.syncfusion.com/downloads/support/forum/162964/ze/F1629641211196844 At first it seemed like it was working. Sample.xlsx had the following values
A1 = 100
B1 = 200
When I clicked the pop up I saw 300, which is the sum of A1 and B1.
However, on closer inspection, it does not seem like the CustomSum() function is actually getting. If I change the CustomSum function and add 1000 to the sum
worksheet.CalcEngine.AddFunction("Sum", new CalcEngine.LibraryFunction(CustomSum));
worksheet["C1"].Value = worksheet.CalcEngine.ParseAndComputeFormula("=SUM(A1,B1)");
MessageBox.Show(worksheet["C1"].Value);
...
public string CustomSum(string args)
{
double sum = 0;
double d;
var splitArgs = args.Split(new char[] { CalcEngine.ParseArgumentSeparator });
foreach (string s in splitArgs)
{
if (double.TryParse(s, NumberStyles.Number | NumberStyles.AllowExponent, null, out d))
sum += d;
}
sum += 1000; //<--------------- Test to see if this function is getting called
return sum.ToString();
}
I expect to see a pop up that shows 1300. But the pop up still shows 300. So I think that Excel's sum() function is still getting called and the CustomSum() function is getting ignored. So registered the function under a different name that would not conflict with Excel's built in function names.
worksheet.CalcEngine.AddFunction("Sum2", new CalcEngine.LibraryFunction(CustomSum));
worksheet["C1"].Value = worksheet.CalcEngine.ParseAndComputeFormula("=SUM2(A1,B1)");
When I ran it this time 1000 showed in the pop up window instead of the 1300 that I expected to see. CustomSum was getting called but args received "A1,B1" and the double.TryParse() failed since "A1" and "B1" are not double values. I was able to make this work by saving workbook as a class variable
private IWorksheet worksheet;
and then using the worksheet to get the values out of "A1" and "B1" before trying to convert to a double value.
string cellValue = worksheet[s].Value;
if (double.TryParse(cellValue, NumberStyles.Number | NumberStyles.AllowExponent, null, out d)) {
The problem with doing this is that the code containing the custom function needs a reference to the IWorksheet. We wanted to allow our customers to write DLLs containing their own set of custom functions. This means that when our customers write their DLLs they would have to import the SyncFusion libraries so they could import the IWorksheet class. If that is the case would each of our customers need their own SyncFusion license? Or is there another way for the custom funtion to get the value out of cell "A1" and "B1" without access to to the IWorksheet object?