简体   繁体   English

在UDF之上使用Excel内置函数

[英]Using Excel Built-in Functions on top of the UDF

I don't know why for some reason the Excel built-in function =SUM() can't really sum up the results created by the custom UDF. 我不知道为什么由于某种原因Excel内置函数=SUM()无法真正总结自定义UDF创建的结果。 It appears to always end up with 0. 它似乎总是以0结尾。

For example, I have got a following UDF, called myUDF , which will return the number of items. 例如,我有一个名为myUDF的后续UDF,它将返回项目数。

For the cell A1 : 对于单元格A1
Formula: =myUDF('ItemA') Result: 10 公式: =myUDF('ItemA')结果:10

For the cell B1 : 对于单元格B1
Formula: =myUDF('ItemB') Result: 15 公式: =myUDF('ItemB')结果:15

So When I do =Sum(A1:B1) and put the formula in cell C1 , it won't return 25 but 0 instead. 所以当我做=Sum(A1:B1)并将公式放在单元格C1 ,它不会返回25而是返回0。

I have tried to use some data formatting stuff (converting to numeric) but still no luck there. 我试图使用一些数据格式化的东西(转换为数字),但仍然没有运气。 Has anyone here had similar issue before? 这里有没有人有类似的问题? Any ideas on the cause of it? 有关它的原因的任何想法?

Thanks. 谢谢。

EDIT: Code Sample 编辑:代码示例

        public object MyUDF(string id, string pk, string param1 = "", string param2 = "", string param3 = "", string param4 = "")
        {
            object result = null;
            string strFormula;

            double n = 0;
            DateTime dt;

            try
            {
                strFormula = buildFormula(id, pk, param1, param2, param3, param4);

                result = ws.getServiceResultsDataString(id, objUser, pk, param1, param2, param3, param4);

                if (double.TryParse(result.ToString(), out n)) 
                {
                    result = n;
                }
                else if (DateTime.TryParse(result.ToString(), out dt))
                {
                    result = dt.Date;
                }
                ws.Dispose();
                objUser = null;

            }
            catch (Exception ex)
            {               
            }
            finally
            {   
            }
            return result;
        }

Is your UDF returning a number or a string that happens to contain numeric characters ? 您的UDF是返回一个恰好包含数字字符数字字符串吗?

If I go into Excel and type in '10 (10, formatted as a string) and '15 (15, formatted as a string), and SUM() them, I get 0. 如果我进入Excel并键入'10(10,格式化为字符串)和'15(15,格式化为字符串)和SUM()它们,我得到0。

If I type 10 and 15 (formatted as numbers) and SUM() them, I get 25. 如果我输入10和15(格式化为数字)和SUM()它们,我得到25。

Some example code that reproduces the problem would allow us to answer for sure. 一些重现问题的示例代码将允许我们肯定地回答。

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

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