Skip to content

Commit 80f91f3

Browse files
authored
Merge pull request #601 from telerik/new-kb-sumproduct-function-nested-array-formulas-telerik-spreadprocessing-706b8900d7d44ed78087e845815f7969
Added new kb article sumproduct-function-nested-array-formulas-telerik-spreadprocessing
2 parents 88112b3 + c9c99a2 commit 80f91f3

File tree

3 files changed

+138
-0
lines changed

3 files changed

+138
-0
lines changed
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
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: spread, processing, formula, custom, function, sumproduct, array
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](https://support.microsoft.com/en-us/office/sumproduct-function-16753e75-9f68-4874-94ac-4d2145a2fd2e) function in [RadSpreadProcessing]({%slug radspreadprocessing-overview%}).
22+
23+
## Solution
24+
25+
Follow the steps:
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+
List<ArrayExpression> arrayExpressions = new List<ArrayExpression>();
91+
foreach (ArrayExpression array in context.Arguments)
92+
{
93+
arrayExpressions.Add(array);
94+
}
95+
96+
int nbElements = arrayExpressions.First().Count();
97+
if (!arrayExpressions.Any(a => a.Count() != nbElements))
98+
{
99+
List<double> values = new List<double>(nbElements);
100+
for (int i = 0; i < nbElements; i++)
101+
{
102+
values.Add(1);
103+
}
104+
105+
for (int i = 0; i < values.Count; i++)
106+
{
107+
for (int j = 0; j < arrayExpressions.Count; j++)
108+
{
109+
string value = arrayExpressions[j].Value.ElementAt(i).ToString();
110+
if (double.TryParse(value, out double doubleValue))
111+
{
112+
values[i] *= doubleValue;
113+
}
114+
else
115+
{
116+
values[i] = 0;
117+
}
118+
}
119+
}
120+
121+
result = values.Sum();
122+
}
123+
124+
return new NumberExpression(result);
125+
}
126+
}
127+
```
128+
129+
## See Also
130+
131+
- [Functions]({%slug radspreadprocessing-features-formulas-functions%})
132+
- [Custom Functions]({%slug radspreadprocessing-features-formulas-custom-functions%})

libraries/radspreadprocessing/features/formulas/custom-functions.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,3 +338,4 @@ __Example 5__ shows how to create the 'E' function.
338338
* [ArgumentInterpretation](https://docs.telerik.com/devtools/document-processing/api/Telerik.Windows.Documents.Spreadsheet.Expressions.Functions.ArgumentInterpretation.html)
339339
* [ArrayArgumentInterpretation](https://docs.telerik.com/devtools/document-processing/api/Telerik.Windows.Documents.Spreadsheet.Expressions.Functions.ArrayArgumentInterpretation.html)
340340
* [CustomFunctions SDK](https://github.com/telerik/xaml-sdk/tree/master/Spreadsheet/WPF/CustomFunctions)
341+
* [Implementing SUMPRODUCT Function in SpreadProcessing]({%slug sumproduct-function-nested-array-formulas-telerik-spreadprocessing%})

libraries/radspreadprocessing/features/formulas/functions.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -973,3 +973,8 @@ Removes duplicate spaces, and spaces at the start and end of a text string. The
973973
UPPER</td><td>
974974

975975
Converts text to uppercase</td></tr></table>
976+
977+
## See Also
978+
979+
* [CustomFunctions SDK](https://github.com/telerik/xaml-sdk/tree/master/Spreadsheet/WPF/CustomFunctions)
980+
* [Implementing SUMPRODUCT Function in SpreadProcessing]({%slug sumproduct-function-nested-array-formulas-telerik-spreadprocessing%})

0 commit comments

Comments
 (0)