简体   繁体   English

Excel模板的Excel用户定义函数问题

[英]Excel User Defined Function issues with Excel Template

I am working with a product based on Excel Document Level Customization. 我正在使用基于Excel Document Level Customization的产品。 The project type is “Excel 2013 Template” in Visual Studio. 在Visual Studio中,项目类型为“ Excel 2013模板”。

Recently I have developed a User Defined Function which will be used to evaluate our own function. 最近,我开发了一个用户定义函数,该函数将用于评估我们自己的函数。 I have developed the User Defined Function based on the article https://blogs.msdn.microsoft.com/eric_carter/2004/12/01/writing-user-defined-functions-for-excel-in-net/ 我根据文章https://blogs.msdn.microsoft.com/eric_carter/2004/12/01/writing-user-defined-functions-for-excel-in-net/开发了用户定义函数

The User Defined Function works fine with the Excel Template of our product but it fails for one test case. 用户定义函数可以与我们产品的Excel模板一起正常使用,但在一个测试用例中失败。

  1. Open a new Excel template of the product . 打开产品的新Excel模板。 [ex: Excel template 1] [例如:Excel模板1]
  2. Add the User Defined Function to a cell. 将用户定义函数添加到单元格。 [Evaluates the result successfully] [成功评估结果]
  3. Open another Excel Template of the product. 打开该产品的另一个Excel模板。 [ex: Excel template 2] [例如:Excel模板2]
  4. Add the User Defined Function to a cell. 将用户定义函数添加到单元格。 [Evaluates the result successfully] [成功评估结果]
  5. Close the template opened in Step 1. [ie: Excel template 1)] 关闭在步骤1中打开的模板。[即:Excel模板1)]
  6. Add the User Defined Function to a cell of other template [ie: Excel template 2] 将用户定义的函数添加到其他模板的单元格中(即Excel模板2)

Now I can notice the added function won't get evaluated and End up with “#NAME?” error. 现在,我注意到添加的函数不会得到评估,并以“ #NAME?”结尾。

I have checked the User Defined Function from the Excel's Add-in list. 我已经从Excel的“加载项”列表中检查了“用户定义的函数”。 It is active. 它是活动的。 Any way I have re-installed the add-in programmatically in the workbook's ActivateEvent, but no use. 我以任何方式通过编程方式在工作簿的ActivateEvent中重新安装了该加载项,但没有用。

This problem will only occur if I close the template that was opened first. 仅当我关闭首先打开的模板时,才会出现此问题。 Further this issue will not be there for saved work books. 此外,保存的工作簿不会出现此问题。

When I debugged, I found one issue. 调试时,发现了一个问题。 In our product we have our own set of commandbars added to the Excel's context menu when our customization get opened. 在我们的产品中,打开自定义设置后,我们在Excel的上下文菜单中添加了自己的一组命令栏。 The below sample code will add a command bar button in to the context menu of the cell. 下面的示例代码将在单元的上下文菜单中添加一个命令栏按钮。

 try
 {
    commandBars = (Office.CommandBars)Application.GetType().InvokeMember("CommandBars", BindingFlags.GetProperty, null, Application, new object[] { });
    commandBarSheetMenu = commandBars["Cell"];                
 }
 catch (Exception ex)
 {
    MessageBox.Show(ex.Message);
 }
 commandBarButtonTest = (Office.CommandBarButton)commandBarSheetMenu.Controls.Add(1, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
 commandBarButtonTest.BeginGroup = true;
 commandBarButtonTest.Caption = "Test Me";
 commandBarButtonTest.Click += new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(commandBarButtonSheetCopy_Click);

When I remove/ comment the above code the User Defined Function start working even If I close the first opened template. 当我删除/注释以上代码时,即使我关闭了第一个打开的模板,用户定义函数也开始工作。

Is it a known problem with the Excel Template and User Defined Function when it is used with Command bars? 与命令栏一起使用时,Excel模板和用户定义函数是否存在已知问题?

It would be great if someone can comment on the issue and suggest a solution to this issue. 如果有人可以对此问题发表评论并提出解决方案,那就太好了。

I can recreate this issue even without command bars. 即使没有命令栏,我也可以重新创建此问题。 The same problem exists for the above test case, If I programmatically set the User Defined Function to any excel cell. 如果我以编程方式将“用户定义的函数”设置为任何excel单元,则上述测试用例存在相同的问题。

I have created two demo solutions that can be used to recreate the problem, by following the above test case. 通过遵循上述测试案例,我创建了两个演示解决方案可用于重现问题。 These demo solutions are 这些演示解决方案是

  1. Programmatically set the User Defined Function to a selected cell by pressing a button. 通过按按钮以编程方式将“用户定义的功能”设置为选定的单元格。
  2. Workbook Template Customization with custom command bars 使用自定义命令栏自定义工作簿模板

Y ou can download the source files from https://1drv.ms/u/s!AKQQ5XmOgV6CgQE 您可以从 https://1drv.ms/u/s 下载源文件! AKQQ5XmOgV6CgQE

I have included the Readme files inside these Solutions separately with the instructions and steps to re-create the issue. 我将这些自述文件分别包含在这些解决方案中,并附有重新创建问题的说明和步骤。

It would be great If some expert in this area can give me a solution to fix this problem 如果这方面的专家能给我解决该问题的方法,那就太好了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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