在Application.Evaluate中使用多个UDF函数

时间:2019-08-25 17:55:22

标签: excel excel-dna

我已经在C#中构建了Excel-Dna Addin。在某些情况下,我需要运行几个UDF函数。 我的单元格包含具有不同参数的UDF,在此为简化版本。

我正在为此使用Application.Evaluate函数。

我的UDF:

[ExcelFunction(Name = "Test.TestMe", Description = "Some test", IsHidden = false)]
public static int TestMe(int i)
{
    return i + 1;
}

如果我致电Application.Evaluate("=Test.TestMe(1)"),我会得到结果2。

如果我尝试执行以下操作:Application.Evaluate("=Test.TestMe(1)+Test.TestMe(2)")没有调用我的UDF函数。

Evaluate函数返回:-2146826259 = #Name?

我尝试了以下操作:

  1. 已删除函数名称=前的"Test.TestMe(1)+Test.TestMe(2)"
  2. 以全限定名呼叫。 "myexcel.xlsx!Test.TestMe(1)+Test.TestMe(2)"
  3. 我知道Application.Evaluate不能超过255个字符。

我想避免解析公式并一一调用我的UDF。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

Charles Williams(https://fastexcel.wordpress.com/2011/11/02/evaluate-functions-and-formulas-fun-how-to-make-excels-evaluate-method-twice-as-fast/)在描述此限制时指出了一篇(现已丢失的)知识库文章:

  

如果字符串公式同时包含对UDF及其名称的引用   如果名称引用在之后发生,将失败并显示错误2029。   UDF参考:

     
      
  • 如果.wav是命名范围,而import types import pandas as pd from botocore.client import Config import ibm_boto3 def __iter__(self): return 0 os_client= ibm_boto3.client(service_name='s3', ibm_api_key_id='<IBM_API_KEY_ID>', ibm_auth_endpoint="<IBM_AUTH_ENDPOINT>", config=Config(signature_version='oauth'), endpoint_url='<ENDPOINT>') # Your data file was loaded into a botocore.response.StreamingBody object. # Please read the documentation of ibm_boto3 and pandas to learn more about the possibilities to load the data. # ibm_boto3 documentation: https://ibm.github.io/ibm-cos-sdk-python/ # pandas documentation: http://pandas.pydata.org/ streaming_body_1 = os_client.get_object(Bucket='<BUCKET>', Key='myfile.wav')['Body'] # add missing __iter__ method, so pandas accepts body as file-like object if not hasattr(streaming_body_1, "__iter__"): streaming_body_1.__iter__ = types.MethodType( __iter__, streaming_body_1 ) 是用户定义的   VBA函数,然后此语句返回错误2029 [即fred] xyz()
  •   
  • 此语句返回正确的值:#NAME?
  •   
  • Microsoft KB文章823604 [我找不到] 可以识别此问题,但不能正确诊断导致该问题的情况。
  •   

由于您的UDF名称在Excel中也是“名称”,因此它可能会受到相同的怪癖的影响,从而给您带来Application.Evaluate("=xyz(b1)+fred")错误。

因此,我认为您遇到了一个长期存在的Excel怪癖,您可能希望使用 File-> Feedback->'Send a Frown'按钮将此报告给Microsoft。 / p>