简体   繁体   中英

Chain function calls using Excel-Dna

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, eg "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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM