From 7de6f71eff421918920cf4eecf5351b710bb4884 Mon Sep 17 00:00:00 2001 From: KB Bot Date: Mon, 28 Jul 2025 10:24:35 +0000 Subject: [PATCH 1/2] Added new kb article sumproduct-function-nested-array-formulas-telerik-spreadprocessing --- ...array-formulas-telerik-spreadprocessing.md | 143 ++++++++++++++++++ 1 file changed, 143 insertions(+) create mode 100644 knowledge-base/sumproduct-function-nested-array-formulas-telerik-spreadprocessing.md diff --git a/knowledge-base/sumproduct-function-nested-array-formulas-telerik-spreadprocessing.md b/knowledge-base/sumproduct-function-nested-array-formulas-telerik-spreadprocessing.md new file mode 100644 index 00000000..cb7ddc8e --- /dev/null +++ b/knowledge-base/sumproduct-function-nested-array-formulas-telerik-spreadprocessing.md @@ -0,0 +1,143 @@ +--- +title: Implementing SUMPRODUCT Function in SpreadProcessing +description: Explains how to implement a custom SUMPRODUCT function in Telerik SpreadProcessing for results similar to Excel. +type: how-to +page_title: SUMPRODUCT Function Implementation in Telerik SpreadProcessing +meta_title: SUMPRODUCT Function Implementation in Telerik SpreadProcessing +slug: sumproduct-function-nested-array-formulas-telerik-spreadprocessing +tags: spreadprocessing, formula, custom function, sumproduct, array formulas +res_type: kb +ticketid: 1694608 +--- + +## Environment + +| Version | Product | Author | +| ---- | ---- | ---- | +| 2025.2.520| RadSpreadProcessing |[Desislava Yordanova](https://www.telerik.com/blogs/author/desislava-yordanova)| + +## Description + +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. + +## Solution + +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: + +1. Register your custom SUMPRODUCT function using the `FunctionManager.RegisterFunction()` method. + +2. Implement your custom function as shown below. Ensure it handles simple array inputs correctly. + +```csharp + internal class Program + { + static void Main(string[] args) + { + FunctionManager.RegisterFunction(new SumProduct()); + + Workbook workbook = new Workbook(); + workbook.Worksheets.Add(); // Sheet1 + Worksheet worksheet = workbook.Worksheets[0]; + worksheet.Cells[ 0,0].SetValue("=SUMPRODUCT({ 1,2,3}, { 4,5,6})"); //A1=32 + CellSelection cell = worksheet.Cells[0, 0]; + ICellValue cellValue = cell.GetValue().Value; + CellValueFormat cellFormat = cell.GetFormat().Value; + string formattedValue = cellValue.GetResultValueAsString(cellFormat); + Debug.WriteLine(formattedValue); + + + string fileName = "SampleFile.xlsx"; + System.IO.File.Delete(fileName); + Telerik.Windows.Documents.Spreadsheet.FormatProviders.IWorkbookFormatProvider formatProvider = new Telerik.Windows.Documents.Spreadsheet.FormatProviders.OpenXml.Xlsx.XlsxFormatProvider(); + + using (Stream output = new FileStream(fileName, FileMode.Create)) + { + formatProvider.Export(workbook, output, TimeSpan.FromSeconds(10)); + } + Process.Start(new ProcessStartInfo() { FileName = fileName, UseShellExecute = true }); + } + } + + public class SumProduct : FunctionWithArguments + { + private static readonly string _name = "SUMPRODUCT"; + private static readonly FunctionInfo _info; + + static SumProduct() + { + string description = "The SUMPRODUCT function returns the sum of the products of corresponding ranges or arrays."; + string descriptionLocalizationKey = "Spreadsheet_Functions_SumProduct_Info"; + IEnumerable requiredArguments = new ArgumentInfo[] + { + 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"), + }; + + IEnumerable optionalArguments = new ArgumentInfo[] + { + 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"), + }; + + _info = new FunctionInfo(_name, FunctionCategory.Statistical, description, requiredArguments, optionalArguments, 254, false, descriptionLocalizationKey); + } + + public override string Name => _name; + + public override FunctionInfo FunctionInfo => _info; + + protected override RadExpression EvaluateOverride(FunctionEvaluationContext context) + { + double result = 0; + try + { + List arrayExpressions = new List(); + foreach (ArrayExpression array in context.Arguments) + { + arrayExpressions.Add(array); + } + + int nbElements = arrayExpressions.First().Count(); + if (!arrayExpressions.Any(a => a.Count() != nbElements)) + { + List values = new List(nbElements); + for (int i = 0; i < nbElements; i++) + { + values.Add(1); + } + + for (int i = 0; i < values.Count; i++) + { + for (int j = 0; j < arrayExpressions.Count; j++) + { + string value = arrayExpressions[j].Value.ElementAt(i).ToString(); + if (double.TryParse(value, out double doubleValue)) + { + values[i] *= doubleValue; + } + else + { + values[i] = 0; + } + } + } + + result = values.Sum(); + } + } + catch + { + // Do nothing + } + + return new NumberExpression(result); + } + } +``` + + +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. + +## See Also + +- [Telerik Document Processing](https://docs.telerik.com/devtools/document-processing/introduction) +- [SpreadProcessing: Implement the SUMPRODUCT Function](https://feedback.telerik.com/document-processing/1625149-spreadprocessing-implement-the-sumproduct-function) +- [SUMPRODUCT Function in Excel](https://support.microsoft.com/en-us/office/sumproduct-function-16753e75-9f68-4874-94ac-4d2145a2fd2e) From c9c99a25593dda84cfbe926ce60c147cee0d854b Mon Sep 17 00:00:00 2001 From: Desislava Yordanova Date: Wed, 6 Aug 2025 15:09:51 +0300 Subject: [PATCH 2/2] new KB --- ...array-formulas-telerik-spreadprocessing.md | 21 +++++-------------- .../features/formulas/custom-functions.md | 1 + .../features/formulas/functions.md | 5 +++++ 3 files changed, 11 insertions(+), 16 deletions(-) diff --git a/knowledge-base/sumproduct-function-nested-array-formulas-telerik-spreadprocessing.md b/knowledge-base/sumproduct-function-nested-array-formulas-telerik-spreadprocessing.md index cb7ddc8e..ecacd979 100644 --- a/knowledge-base/sumproduct-function-nested-array-formulas-telerik-spreadprocessing.md +++ b/knowledge-base/sumproduct-function-nested-array-formulas-telerik-spreadprocessing.md @@ -5,7 +5,7 @@ type: how-to page_title: SUMPRODUCT Function Implementation in Telerik SpreadProcessing meta_title: SUMPRODUCT Function Implementation in Telerik SpreadProcessing slug: sumproduct-function-nested-array-formulas-telerik-spreadprocessing -tags: spreadprocessing, formula, custom function, sumproduct, array formulas +tags: spread, processing, formula, custom, function, sumproduct, array res_type: kb ticketid: 1694608 --- @@ -18,11 +18,11 @@ ticketid: 1694608 ## Description -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. +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%}). ## Solution -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: +Follow the steps: 1. Register your custom SUMPRODUCT function using the `FunctionManager.RegisterFunction()` method. @@ -87,8 +87,6 @@ SpreadProcessing does not natively support full array formula evaluation or Exce protected override RadExpression EvaluateOverride(FunctionEvaluationContext context) { double result = 0; - try - { List arrayExpressions = new List(); foreach (ArrayExpression array in context.Arguments) { @@ -122,22 +120,13 @@ SpreadProcessing does not natively support full array formula evaluation or Exce result = values.Sum(); } - } - catch - { - // Do nothing - } return new NumberExpression(result); } } ``` - -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. - ## See Also -- [Telerik Document Processing](https://docs.telerik.com/devtools/document-processing/introduction) -- [SpreadProcessing: Implement the SUMPRODUCT Function](https://feedback.telerik.com/document-processing/1625149-spreadprocessing-implement-the-sumproduct-function) -- [SUMPRODUCT Function in Excel](https://support.microsoft.com/en-us/office/sumproduct-function-16753e75-9f68-4874-94ac-4d2145a2fd2e) +- [Functions]({%slug radspreadprocessing-features-formulas-functions%}) +- [Custom Functions]({%slug radspreadprocessing-features-formulas-custom-functions%}) diff --git a/libraries/radspreadprocessing/features/formulas/custom-functions.md b/libraries/radspreadprocessing/features/formulas/custom-functions.md index 99bc9751..7834fa14 100644 --- a/libraries/radspreadprocessing/features/formulas/custom-functions.md +++ b/libraries/radspreadprocessing/features/formulas/custom-functions.md @@ -338,3 +338,4 @@ __Example 5__ shows how to create the 'E' function. * [ArgumentInterpretation](https://docs.telerik.com/devtools/document-processing/api/Telerik.Windows.Documents.Spreadsheet.Expressions.Functions.ArgumentInterpretation.html) * [ArrayArgumentInterpretation](https://docs.telerik.com/devtools/document-processing/api/Telerik.Windows.Documents.Spreadsheet.Expressions.Functions.ArrayArgumentInterpretation.html) * [CustomFunctions SDK](https://github.com/telerik/xaml-sdk/tree/master/Spreadsheet/WPF/CustomFunctions) + * [Implementing SUMPRODUCT Function in SpreadProcessing]({%slug sumproduct-function-nested-array-formulas-telerik-spreadprocessing%}) diff --git a/libraries/radspreadprocessing/features/formulas/functions.md b/libraries/radspreadprocessing/features/formulas/functions.md index c439f9f4..35779974 100644 --- a/libraries/radspreadprocessing/features/formulas/functions.md +++ b/libraries/radspreadprocessing/features/formulas/functions.md @@ -973,3 +973,8 @@ Removes duplicate spaces, and spaces at the start and end of a text string. The UPPER Converts text to uppercase + +## See Also + + * [CustomFunctions SDK](https://github.com/telerik/xaml-sdk/tree/master/Spreadsheet/WPF/CustomFunctions) + * [Implementing SUMPRODUCT Function in SpreadProcessing]({%slug sumproduct-function-nested-array-formulas-telerik-spreadprocessing%})