Excel UDFs can take arrays as input, and return arrays as result, but this has to be done explicitly. There no support for automatically extending a scalar function to work over arrays.
If you change the signature of your MultiplyThem
function to be
public static object MuliplyThem(object x, object y) {…}
your function will receive the full array when called as {=MultiplyThem({1,2,3,4},5)}
.
You then need to add a type check in the function to make sure you handle the different options correctly. (When you declare the parameter as ‘double’ Excel will try to convert the input, and return #VALUE
if incompatible. Here you have to deal with the type checking and conversion.)
An exhaustive example of all the values you can get for an ‘object’ parameter looks like this:
[ExcelFunction(Description="Describes the value passed to the function.")]
public static string Describe(object arg)
{
if (arg is double)
return "Double: " + (double)arg;
else if (arg is string)
return "String: " + (string)arg;
else if (arg is bool)
return "Boolean: " + (bool)arg;
else if (arg is ExcelError)
return "ExcelError: " + arg.ToString();
else if (arg is object[,])
// The object array returned here may contain a mixture of different types,
// reflecting the different cell contents.
return string.Format("Array[{0},{1}]", ((object[,])arg).GetLength(0), ((object[,])arg).GetLength(1));
else if (arg is ExcelMissing)
return "<<Missing>>"; // Would have been System.Reflection.Missing in previous versions of ExcelDna
else if (arg is ExcelEmpty)
return "<<Empty>>"; // Would have been null
else
return "!? Unheard Of ?!";
}
In your case you’d handle object[,]
arrays in a special way, and return a double[,]
array or object[,]
array when appropriate. It the inputs are not of types you can process, you can return an error, most likely ExcelError.ExcelErrorValue
.