用户定义对象和ExcelIntegration



我正在编写c# (Visual Studio Express 2012)和使用ExcelIntegration。我想让一个ExcelFunction返回一个用户定义的对象到一个单元格。我进一步想要一个ExcelFunction接受一个用户定义的对象作为输入。

下面是我的代码示例。这不起作用,两个函数(CreateDogGetName)都不能从Excel中看到。

namespace Test
{
    public class Dog
    {
        public Dog(string name)
        {
            Name = name;
        }
        public readonly string Name;
    }
    public class TestClass
    {
        [ExcelFunction]
        public static Dog CreateDog(string name)
        {
            return new Dog(name);
        }
        [ExcelFunction]
        public static string GetName(Dog dog)
        {
            return dog.Name;
        }
    }
}

昨天的答案之后,我加了一本字典。我将代码修改如下这个作品。我现在的问题是如何使它通用。我能不能修改ExcelDNA代码让它自动帮我查字典?

ExcelIntegration

名称空间{公共类狗{public Dog(字符串名称){Name = Name;}

    public readonly string Name;
}
public class TestClass
{
    static Dictionary<string, Dog> DogStore;
    [ExcelFunction]
    public static string CreateDog(string name)
    {
        Dog dog = new Dog(name);
        string key = dog.ToString() + "_" + DateTime.Now.Ticks.ToString();
        try
        {
            if (DogStore.ContainsKey(key) == false) DogStore.Add(key, dog);
        }
        catch (NullReferenceException)
        {
            DogStore = new Dictionary<string, Dog>();
            if (DogStore.ContainsKey(key) == false) DogStore.Add(key, dog);
        }
        return key;
    }
    [ExcelFunction]
    public static string GetName(string dogKey)
    {
        Dog dog = DogStore[dogKey];
        return dog.Name;
    }
}

}

Excel-DNA还没有内置这种自定义封送或对象支持。根据您的需要,它可能像在代码中添加一些包装器和转换函数以及对象字典一样简单,或者您可能需要动态生成所有包装器并在运行时注册新函数。

隔间工具是一个最近的(开源)项目,它在Excel-DNA之上添加了一个广泛的分布式对象和代码模型。但这是一个相当复杂的项目,很难让你理解。

在这个线程中描述的RTD机制之上,有一个非常简单的基于f#的对象处理程序实现。(但是使用当前的Excel-DNA版本,代码可以做得更好。)

如果你只是在寻找一些指导,并且你的"狗"不是太挑剔,那么Excel-DNA谷歌组可能是讨论的最佳场所。

相关内容

  • 没有找到相关文章

最新更新