简体   繁体   English

How to make ExcelDna functions from Nuget.org package visible in the package user's Excel session

[英]How to make ExcelDna functions from Nuget.org package visible in the package user's Excel session

I have created a Nuget.org package, with the following (abridged) code:我创建了一个 Nuget.org package,具有以下(删节)代码:

namespace MyNugetPackageNS
    module MyNugetModule = 
        open ExcelDna.Integration

        // this function is NOT seen in the package user's Excel session
        [<ExcelFunction(Category="Test", Description="Add 1.")>]
        let plusOne ([<ExcelArgument(Description= "Value.")>] value: double) : obj = 
            value + 1.0 |> box

I was hoping that if a user installs the package and adds it into his own library, all the Excel functions defined in the package (eg plusOne) would automatically be visible within Excel, but it isn't the case. I was hoping that if a user installs the package and adds it into his own library, all the Excel functions defined in the package (eg plusOne) would automatically be visible within Excel, but it isn't the case. It seems that the user has to "wrap" the package Excel functions in order to make them visible in Excel, eg :似乎用户必须“包装” package Excel 函数才能使它们在 Excel 中可见,例如:

namespace UserNS
    module UserModule = 
        // need to install the nuget package from Nuget.org
        open MyNugetPackageNS.MyNugetModule
        open ExcelDna.Integration

        // this function is seen in the package user's Excel session
        [<ExcelFunction(Category="Test", Description="Add 1.")>]
        let plusOne2 ([<ExcelArgument(Description= "Value.")>] value: double) : obj = 
            MyNugetModule.plusOne value

My question: Is there a way to make the package's Excel functions automatically "visible" in the package user's Excel session, without wrapping each function before hand? My question: Is there a way to make the package's Excel functions automatically "visible" in the package user's Excel session, without wrapping each function before hand?

================= ==================

EDIT:编辑:

Following Augusto's tip, I added Excel-DNA Registration package to my Nuget project, and based on the examples found here , I added the MakeAddInsVisible snippet which explicitly loads the Nuget project's Excel functions. Following Augusto's tip, I added Excel-DNA Registration package to my Nuget project, and based on the examples found here , I added the MakeAddInsVisible snippet which explicitly loads the Nuget project's Excel functions.

namespace MyNugetPackageNS
open ExcelDna.Integration
open ExcelDna.Registration

type MakeAddInsVisible () =
    interface IExcelAddIn with
        member this.AutoOpen ()  = 
            ExcelRegistration.GetExcelFunctions ()
            |> ExcelRegistration.RegisterFunctions
        member this.AutoClose () = ()

    module MyNugetModule = 
        open ExcelDna.Integration

        // this function is STILL NOT seen in the package user's Excel session
        [<ExcelFunction(Category="Test", Description="Add 1.")>]
        let plusOne ([<ExcelArgument(Description= "Value.")>] value: double) : obj = 
            value + 1.0 |> box

I also added the ExplicitRegistration flag to my Nuget project's.dna file:我还在我的 Nuget 项目的 .dna 文件中添加了ExplicitRegistration标志:

<DnaLibrary Name="MyNugetPackageNS Add-In" RuntimeVersion="v4.0" xmlns="http://schemas.excel-dna.net/addin/2018/05/dnalibrary">
  <ExternalLibrary Path="MyNugetPackageNS.dll" ExplicitExports="true" ExplicitRegistration="true" LoadFromBytes="true" Pack="true" IncludePdb="false" />
</DnaLibrary>

"Locally" (meaning when I use MyNugetPackageNS as a normal package, before exporting it to Nuget.org), this seems to work as expected: Because of the ExplicitRegistration="true" flag in the.dna file, the Excel functions are still registered because of the presence of the MakeAddInsVisible snippet (and no Excel function would be registered when the snippet is commented out). "Locally" (meaning when I use MyNugetPackageNS as a normal package, before exporting it to Nuget.org), this seems to work as expected: Because of the ExplicitRegistration="true" flag in the.dna file, the Excel functions are still由于存在MakeAddInsVisible代码段而注册(当代码段被注释掉时,不会注册 Excel function)。

Now I exported this new code to nuget.org and downloaded the package into the nuget package user's project (without any modifications to the above namespace UserNS... code). Now I exported this new code to nuget.org and downloaded the package into the nuget package user's project (without any modifications to the above namespace UserNS... code).

Unfortunately MyNugetPackageNS Excel functions are still invisible from the nuget package user's Excel session. Unfortunately MyNugetPackageNS Excel functions are still invisible from the nuget package user's Excel session.

What did I miss?我错过了什么?

In order to register new functions at run-time you'll have to use the Registration extension .为了在运行时注册新功能,您必须使用注册扩展 Here is an example of how to use it:以下是如何使用它的示例:

https://stackoverflow.com/a/60079589 https://stackoverflow.com/a/60079589

The extra library that you add to your user's project will not be recognized by the Excel-DNA startup without some indication that this library should be scanned for Excel functions. Excel-DNA 启动将无法识别您添加到用户项目的额外库,除非有一些迹象表明应该扫描此库以查找 Excel 函数。

One way that your user can indicate that your library should be checked (and the functions registered) is for them to add an <ExternalLibrary> entry into their add-in.dna file.您的用户可以指示应检查您的库(并注册函数)的一种方法是让他们将<ExternalLibrary>条目添加到他们的 add-in.dna 文件中。

Another approach is what Augusto describes, where your user can add some code to their add-in to register the function.另一种方法是 Augusto 所描述的,您的用户可以将一些代码添加到他们的插件中以注册 function。 But the code will be in their project.但是代码将在他们的项目中。 If the code is in the library you are providing via NuGet, something must still trigger this code to run, eg some other registration call from the user's library.如果代码在您通过 NuGet 提供的库中,则仍必须触发此代码运行,例如来自用户库的其他注册调用。

Following Govert's first suggestion, I added <ExternalLibrary Path="MyNugetPackageNS.dll" ExplicitExports="true" ExplicitRegistration="true" LoadFromBytes="false" Pack="false" IncludePdb="false" /> to the package user's.dna file, and it worked.按照 Govert 的第一个建议,我将<ExternalLibrary Path="MyNugetPackageNS.dll" ExplicitExports="true" ExplicitRegistration="true" LoadFromBytes="false" Pack="false" IncludePdb="false" />添加到 package 用户的.dna 文件中,并且成功了。

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

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