Is it possible to chain function calls using Excel-Dna?
I am trying to call something like this from Excel Sheet:
=CreateAccount(CreateUser("John","Doe"))
But the inner function CreateUser
returns ExcelErrorValue
.
Demo C# code looks like this:
namespace ExcelDnaDemo
{
using ExcelDna.Integration;
public static class MyFunctions
{
[ExcelFunction(Description = "C-U")]
public static object CreateUser(string firstName, string lastName)
{
return new User(firstName, lastName);
}
[ExcelFunction(Description = "C-A")]
public static string CreateAccount(
[ExcelArgument(
Name = "Usr",
Description = "User test", AllowReference = true)]
object user)
{
var a = new Account(user);
return $"{a.User.FirstName} {a.User.LastName} {a.Id}";
}
}
public class User
{
public User(string firstName, string lastName)
{
FirstName = firstName;
LastName = lastName;
}
public string FirstName { get; set; }
public string LastName { get; set; }
}
public class Account
{
public object User { get; private set; }
public Guid Id { get; private set; }
public Account(object user)
{
User = user;
Id = Guid.NewGuid();
}
}
}
You might add the new user to an internal Dictionary<string, User>
and return some kind of key, e.g. "User:1" from the CreateUser
function. Then in CreateAccount
you take in the string key, and check if it is present in the dictionary to retrieve the cached User
object.