Skip to content

Commit 7de6f71

Browse files
author
KB Bot
committed
Added new kb article sumproduct-function-nested-array-formulas-telerik-spreadprocessing
1 parent 49b248b commit 7de6f71

File tree

1 file changed

+143
-0
lines changed

1 file changed

+143
-0
lines changed
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
---
2+
title: Implementing SUMPRODUCT Function in SpreadProcessing
3+
description: Explains how to implement a custom SUMPRODUCT function in Telerik SpreadProcessing for results similar to Excel.
4+
type: how-to
5+
page_title: SUMPRODUCT Function Implementation in Telerik SpreadProcessing
6+
meta_title: SUMPRODUCT Function Implementation in Telerik SpreadProcessing
7+
slug: sumproduct-function-nested-array-formulas-telerik-spreadprocessing
8+
tags: spreadprocessing, formula, custom function, sumproduct, array formulas
9+
res_type: kb
10+
ticketid: 1694608
11+
---
12+
13+
## Environment
14+
15+
| Version | Product | Author |
16+
| ---- | ---- | ---- |
17+
| 2025.2.520| RadSpreadProcessing |[Desislava Yordanova](https://www.telerik.com/blogs/author/desislava-yordanova)|
18+
19+
## Description
20+
21+
Learn how to implement a custom SUMPRODUCT function that exists in Excel but not in Telerik SpreadProcessing. The function works for basic cases but returns incorrect results for formulas with nested array functions.
22+
23+
## Solution
24+
25+
SpreadProcessing does not natively support full array formula evaluation or Excel-style boolean coercion inside custom functions. To achieve Excel-like results, manually evaluate nested logic before passing data to the custom function, or extend your implementation to handle nested expressions explicitly. Follow the steps below:
26+
27+
1. Register your custom SUMPRODUCT function using the `FunctionManager.RegisterFunction()` method.
28+
29+
2. Implement your custom function as shown below. Ensure it handles simple array inputs correctly.
30+
31+
```csharp
32+
internal class Program
33+
{
34+
static void Main(string[] args)
35+
{
36+
FunctionManager.RegisterFunction(new SumProduct());
37+
38+
Workbook workbook = new Workbook();
39+
workbook.Worksheets.Add(); // Sheet1
40+
Worksheet worksheet = workbook.Worksheets[0];
41+
worksheet.Cells[ 0,0].SetValue("=SUMPRODUCT({ 1,2,3}, { 4,5,6})"); //A1=32
42+
CellSelection cell = worksheet.Cells[0, 0];
43+
ICellValue cellValue = cell.GetValue().Value;
44+
CellValueFormat cellFormat = cell.GetFormat().Value;
45+
string formattedValue = cellValue.GetResultValueAsString(cellFormat);
46+
Debug.WriteLine(formattedValue);
47+
48+
49+
string fileName = "SampleFile.xlsx";
50+
System.IO.File.Delete(fileName);
51+
Telerik.Windows.Documents.Spreadsheet.FormatProviders.IWorkbookFormatProvider formatProvider = new Telerik.Windows.Documents.Spreadsheet.FormatProviders.OpenXml.Xlsx.XlsxFormatProvider();
52+
53+
using (Stream output = new FileStream(fileName, FileMode.Create))
54+
{
55+
formatProvider.Export(workbook, output, TimeSpan.FromSeconds(10));
56+
}
57+
Process.Start(new ProcessStartInfo() { FileName = fileName, UseShellExecute = true });
58+
}
59+
}
60+
61+
public class SumProduct : FunctionWithArguments
62+
{
63+
private static readonly string _name = "SUMPRODUCT";
64+
private static readonly FunctionInfo _info;
65+
66+
static SumProduct()
67+
{
68+
string description = "The SUMPRODUCT function returns the sum of the products of corresponding ranges or arrays.";
69+
string descriptionLocalizationKey = "Spreadsheet_Functions_SumProduct_Info";
70+
IEnumerable<ArgumentInfo> requiredArguments = new ArgumentInfo[]
71+
{
72+
new ArgumentInfo("Array1", "The first array argument whose components you want to multiply and then add.", ArgumentType.Array, isRequired: true, "Spreadsheet_Functions_Args_Array", "Spreadsheet_Functions_SumProduct_Array"),
73+
};
74+
75+
IEnumerable<ArgumentInfo> optionalArguments = new ArgumentInfo[]
76+
{
77+
new ArgumentInfo("ArrayX", "The array argument whose components you want to multiply and then add.", ArgumentType.Array, isRequired: true, "Spreadsheet_Functions_Args_Array", "Spreadsheet_Functions_SumProduct_Array"),
78+
};
79+
80+
_info = new FunctionInfo(_name, FunctionCategory.Statistical, description, requiredArguments, optionalArguments, 254, false, descriptionLocalizationKey);
81+
}
82+
83+
public override string Name => _name;
84+
85+
public override FunctionInfo FunctionInfo => _info;
86+
87+
protected override RadExpression EvaluateOverride(FunctionEvaluationContext<object> context)
88+
{
89+
double result = 0;
90+
try
91+
{
92+
List<ArrayExpression> arrayExpressions = new List<ArrayExpression>();
93+
foreach (ArrayExpression array in context.Arguments)
94+
{
95+
arrayExpressions.Add(array);
96+
}
97+
98+
int nbElements = arrayExpressions.First().Count();
99+
if (!arrayExpressions.Any(a => a.Count() != nbElements))
100+
{
101+
List<double> values = new List<double>(nbElements);
102+
for (int i = 0; i < nbElements; i++)
103+
{
104+
values.Add(1);
105+
}
106+
107+
for (int i = 0; i < values.Count; i++)
108+
{
109+
for (int j = 0; j < arrayExpressions.Count; j++)
110+
{
111+
string value = arrayExpressions[j].Value.ElementAt(i).ToString();
112+
if (double.TryParse(value, out double doubleValue))
113+
{
114+
values[i] *= doubleValue;
115+
}
116+
else
117+
{
118+
values[i] = 0;
119+
}
120+
}
121+
}
122+
123+
result = values.Sum();
124+
}
125+
}
126+
catch
127+
{
128+
// Do nothing
129+
}
130+
131+
return new NumberExpression(result);
132+
}
133+
}
134+
```
135+
136+
137+
Avoid using nested array formulas inside the spreadsheet. Instead, preprocess arrays in your code and provide numeric arrays as direct input to the custom function.
138+
139+
## See Also
140+
141+
- [Telerik Document Processing](https://docs.telerik.com/devtools/document-processing/introduction)
142+
- [SpreadProcessing: Implement the SUMPRODUCT Function](https://feedback.telerik.com/document-processing/1625149-spreadprocessing-implement-the-sumproduct-function)
143+
- [SUMPRODUCT Function in Excel](https://support.microsoft.com/en-us/office/sumproduct-function-16753e75-9f68-4874-94ac-4d2145a2fd2e)

0 commit comments

Comments
 (0)