I'm trying to throw together an Excel add-in with ExcelDNA. I think I set up the ExcelDNA.registration "correctly". And I'd like to make a UDF that can handle one or more inputs/arguments as static values, single cell references, or ranges of cells. Ultimately, what I would like to get to is this:
I tried using "params double[,]" to input multiple 2D "arrays" of excel ranges...
But that gives a Compiler Error CS0225 (The params parameter must be a single dimensional array).
Is there a way to pass an optional 2 dimensional params array argument?
=C_RangeSum(A1:D1,A2:D2) = (1+3+5+7) + (2+4+6+8) = 36
I tried using "params double[,]" to input multiple 2D "arrays" of excel ranges...
public static double C_RangeSum(params double[,] values)
But that gives a Compiler Error CS0225 (The params parameter must be a single dimensional array).
Is there a way to pass an optional 2 dimensional params array argument?
Excel UDF Code:
// ==================== START of Function ====================
// Description for IntelliSense Tool Tip
[ExcelFunction(Description = "Test function...first test for params (varying number of arguments in C#). Adds together all arguments.")]
public static double C_MySum(params double[] values)
{
return values.Sum();
} // -------------------- END of Function --------------------
// ==================== START of Function ====================
// Description for IntelliSense Tool Tip
[ExcelFunction(Description = "Test function...test to Add together all arguments in range.")]
public static double C_RangeSum(
[ExcelArgument(AllowReference = true)]
double[,] values) // params object[,] values)
{
int rows = values.GetLength(0);
int cols = values.GetLength(1);
double value = 0;
for (int i = 0; i < rows; i++)
{
for(int j = 0; j < cols; j++)
{
value = value + values[i, j];
}
}
return value;
} // -------------------- END of Function --------------------