【发布时间】:2019-07-01 15:02:01
【问题描述】:
所以,我正在开发一个使用 RibbonBar 和一些外部 DLL 的 Excel 插件(普通 VSTO 项目)。 因为需要在 Excel 中导入一些用户定义的函数(UDF),所以我读到你只能通过 COM 注册它来做到这一点,我是通过函数接口等来做到的。 我做了注册,因为它显示在https://theofficecontext.com/2013/06/08/update-creating-excel-udfs-in-c/ 注册工作正常,我导入的函数可以在 Excel 中调用。 我使用一些对话框让用户设置一些用于 UDF 的变量,我发现这两个东西是在单独的 AddIn 对象中管理的。
这就是问题所在。因为它们是两个不同的对象,所以 UDF 无法访问我在对话框中输入的所有内容。
在通过与工作表交互加载 UDF 时,从 Excel 内部调用 OnConnection()。
namespace myExcelAddin
{
public partial class ThisAddIn
{
public static int iUser = 0;
// .. some other static variables
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
// some startup work
}
}
}
namespace myExcelAddin
{
[ComVisible(true)]
public interface IFunctions
{
// ... some functions
}
[ComVisible(true)]
[GuidAttribute("1D3001F4-5307-49A6-98F2-B3B76B3D0AA3"),
ProgId("myExcelAddin.Functions"),
ClassInterface(ClassInterfaceType.None),
ComDefaultInterface(typeof(IFunctions))]
public partial class Connect : Object, Extensibility.IDTExtensibility2, IFunctions
{
// ... implementation of interface functions
}
public void OnConnection(object application, Extensibility.ext_ConnectMode connectMode, object addInInst, ref System.Array custom)
{
// get a reference to the instance of the add-in
Application = application as Excel.Application;
thisAddIn = addInInst;
}
[ComRegisterFunctionAttribute]
public static void RegisterFunction(Type type)
{
//... registration work
}
[ComUnregisterFunctionAttribute]
public static void UnregisterFunction(Type type)
{
// ... unregistration work
}
}
那么是否有可能获取用户输入的内容? 因为两者的全局变量也有不同的处理方式......
【问题讨论】:
标签: c# .net excel user-defined-functions excel-interop