With v1.9.0 we have some simpler ways of configuring the ‘extended registration’ used if you want to customize your functions.
Here is an example that defines an `ExcelAsyncDefault` attribute that you can use on function, and then a `FunctionExecutionHandler` that can customize the function return value, including a selector to decide when to apply the ExecutionHandler.
If you wanted to apply a pre-determined default return value to all your function, you would change the selector to do so.
-Govert
using ExcelDna.Integration;
using ExcelDna.Registration;
namespace TestTaskReturnOverride
{
public static class MyFunctions
{
[ExcelFunction(Description = "My first .NET function")]
public static string SayHello(string name)
{
return "Hello " + name;
}
[ExcelFunction]
[return: ExcelAsyncDefault("<WAIT>")]
public static async Task<string> SayHelloAsync(string name, string more = "More", int delayMs = 1000)
{
// Simulate an asynchronous operation
await Task.Delay(delayMs);
return "Hello (async) " + name + " " + more;
}
[ExcelFunction]
[return: ExcelAsyncDefault()] // Will use ExcelErrorGettingData as busy return value
public static async Task<string> SleepAWhile(int delayMs = 1000)
{
// Simulate an asynchronous operation
await Task.Delay(delayMs);
return "Done sleeping";
}
}
[AttributeUsage(AttributeTargets.ReturnValue, AllowMultiple = false)]
public class ExcelAsyncDefaultAttribute : Attribute
{
internal object DefaultReturnValue;
// This attribute can be used to mark functions that should return a default value when the result is not available
// It can be applied to methods that return Task<T> or IObservable<T>
public ExcelAsyncDefaultAttribute()
{
DefaultReturnValue = ExcelError.ExcelErrorGettingData;
}
public ExcelAsyncDefaultAttribute(object defaultReturnValue)
{
DefaultReturnValue = defaultReturnValue;
}
}
internal class AsyncReturnHandler : FunctionExecutionHandler
{
object _defaultValue;
public AsyncReturnHandler(object defaultValue)
{
_defaultValue = defaultValue;
}
public override void OnSuccess(FunctionExecutionArgs args)
{
if (args.ReturnValue.Equals(ExcelError.ExcelErrorNA))
args.ReturnValue = _defaultValue;
}
// This method can be inside any class
// It is called for every function to optionally return an additional handler for that function
[ExcelFunctionExecutionHandlerSelector]
public static IFunctionExecutionHandler AsyncReturnHandlerSelector(IExcelFunctionInfo functionInfo)
{
// We need to decide whether to apply a FunctionExecutionHandler to this particular function.
// For this example, we only accept functions that are marked with
// [return: ExcelAsyncDefault(...)]
var defaultAtt = (ExcelAsyncDefaultAttribute)functionInfo.Return.CustomAttributes.FirstOrDefault(ca => ca is ExcelAsyncDefaultAttribute);
if (defaultAtt == null)
{
// No attribute to indicate we want the default overridden
// So no ExecutionHandler to add to the function
return null;
}
return new AsyncReturnHandler(defaultAtt.DefaultReturnValue);
--
You received this message because you are subscribed to the Google Groups "Excel-DNA" group.
To unsubscribe from this group and stop receiving emails from it, send an email to exceldna+u...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/exceldna/8f72287b-4e61-433d-b67c-12759ec8b3f3n%40googlegroups.com.