简体   繁体   English

如何将不连续范围的单元格从 Excel 传递到 ExcelDNA 函数

[英]How to pass discontinuous range of cells from Excel to ExcelDNA function

Consider such ExcelDNA function definition:考虑这样的 ExcelDNA 函数定义:

[ExcelFunction(Name = "Fnc1", Description = "Fnc1")]
public static object Fnc1(
    [ExcelArgument(Name = "Arg1", Description = "Arg1", AllowReference = true)]
    object rng)
{
    // ...
}
  • It works fine when called with a single cell like this =Fnc1(A1) or with continuous range of cells like this =Fnc1(A1:A3) .当使用像这样的单个单元=Fnc1(A1)用时它工作正常=Fnc1(A1)或像这样的连续单元格范围=Fnc1(A1:A3)
  • But it doesn't work when called with discontinuous range of cells eg =Fnc1(A1,A5,A10) .但是当用不连续的单元格范围调用时它不起作用,例如=Fnc1(A1,A5,A10) The error #VALUE!错误#VALUE! is returned.被退回。

Is there a way how to call ExcelDNA function with discontinuous range of unknown amount of cells?有没有办法如何调用具有不连续范围的未知单元格数量的 ExcelDNA 函数?

I have tryied to declare the paramter like this params object[] rng but no luck as well.我已经尝试像这个params object[] rng一样声明参数,但也没有运气。

In order to have an Excel-DNA function that allows passing in an unknown number of arguments at run-time, you need to use params object[] in your function arguments.为了让 Excel-DNA 函数允许在运行时传入未知数量的参数,您需要在函数参数中使用params object[]

public static class MyFunctions
{
    [ExcelFunction]
    public static object Hello(params object[] values)
    {
        return "Hello " + DateTime.Now;
    }
}

Then it doesn't matter if you call it with hard-coded values eg =Hello(10, 20) or if you use cell references eg =Hello(A1,A5,A10) .然后,如果您使用硬编码值(例如=Hello(10, 20)或使用单元格引用(例如=Hello(A1,A5,A10)调用它并不重要。

However, variable number of arguments is not supported out-of-the-box by Excel-DNA, and as such you'll have to use the ExcelDna.Registration helper library in order to register your functions.但是,Excel-DNA 不支持可变数量的参数,因此您必须使用ExcelDna.Registration帮助程序库来注册您的函数。

Install the ExcelDna.Registration NuGet package , then inside of your .dna file, mark your add-in assembly reference to use ExplicitRegistration eg:安装ExcelDna.Registration NuGet 包,然后在您的.dna文件中,标记您的加载项程序集引用以使用ExplicitRegistration例如:

<?xml version="1.0" encoding="utf-8"?>
<DnaLibrary Name="My Add-In" (...)>
  <ExternalLibrary Path="MyAddIn.dll" ExplicitRegistration="true" (...) />
</DnaLibrary>

Then, in your AutoOpen , you register the functions with a ProcessParamsRegistrations call... eg然后,在您的AutoOpen ,您使用ProcessParamsRegistrations调用注册函数......例如

public class AddIn : IExcelAddIn
{
    public void AutoOpen()
    {
        ExcelRegistration
            .GetExcelFunctions()
            .ProcessParamsRegistrations()
            .RegisterFunctions();

        // ...
    }

    public void AutoClose()
    {
        // ...
    }
}

Implicit vs Explicit Registration of functions函数的隐式与显式注册

By default, Excel-DNA searches for every public static method in your assembly and registers them as functions with Excel.默认情况下,Excel-DNA 会搜索程序集中的每个public static方法,并将它们注册为 Excel 中的函数。 That's the implicit registration process.这就是隐式注册过程。

ExplicitRegistration="true" turns off the implicit registration and thus nothing gets registered automatically - you have to do it yourself - which is what I'm doing in the AutoOpen above with the ... RegisterFunctions() call. ExplicitRegistration="true"关闭隐式注册,因此不会自动注册任何东西——你必须自己做——这就是我在上面的AutoOpen使用... RegisterFunctions()调用所做的。 If you don't turn off the implicit registration, then functions end-up being registered twice (once by the implicit process, then again by your code) and you get error messages如果您不关闭隐式注册,则函数最终会注册两次(一次由隐式进程注册,然后再次由您的代码注册)并且您会收到错误消息

The others answers are useful if you'd like to allow multiple parameters, and perhaps easiest for an end user to use.如果您想允许多个参数,并且对于最终用户来说可能最容易使用,则其他答案很有用。 But you could also pass the discontinuous ranges directly into the single AllowReference=true parameter you start with, by adding parentheses in the formula:但是您也可以通过在公式中添加括号将不连续范围直接传递到您开始的单个AllowReference=true参数中:

=Fnc1((A1,A5,A10:A12))

The single ExcelReference you get will have multiple InnerReferences for the disjoint parts.您获得的单个ExcelReference将为不相交的部分提供多个InnerReferences

The parentheses disambiguate between the use of the comma as a range union operator and as the parameter separator in a function call.括号消除了将逗号用作范围联合运算符和用作函数调用中的参数分隔符之间的歧义。

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

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