[英]Sum n largest numbers EPPlus
I'm trying to sum the n largest numbers of a range. 我正在尝试将范围的n个最大数相加。
My formula is: SUM(LARGE(A10:A15, {1,2}))
我的公式是:
SUM(LARGE(A10:A15, {1,2}))
But I'm getting a #VALUE
as result. 但是我得到一个
#VALUE
的结果。
If I debug the formula execution to a log file I got that: 如果我将公式执行调试到日志文件中,则会得到:
Worksheet: Sheet 1
Address: A9
OfficeOpenXml.FormulaParsing.Exceptions.ExcelErrorValueException: #VALUE!
em OfficeOpenXml.FormulaParsing.Excel.Functions.IntArgumentParser.Parse(Object obj)
em OfficeOpenXml.FormulaParsing.Excel.Functions.ExcelFunction.ArgToInt(IEnumerable`1 arguments, Int32 index)
em OfficeOpenXml.FormulaParsing.Excel.Functions.Math.Large.Execute(IEnumerable`1 arguments, ParsingContext context)
em OfficeOpenXml.FormulaParsing.ExpressionGraph.FunctionCompilers.DefaultCompiler.Compile(IEnumerable`1 children, ParsingContext context)
em OfficeOpenXml.FormulaParsing.ExpressionGraph.FunctionExpression.Compile()
Looks like the Large Function doesn't accept an array as a second argument. 看起来大函数不接受数组作为第二个参数。
How can I get the sum of n largest values formula in EPPlus? 如何获得EPPlus中n个最大值公式的总和?
I have Excel 2016 and EPPlus 4.1.1.0 and I tested your work and it is working perfectly. 我有Excel 2016和EPPlus 4.1.1.0,我测试了您的工作,并且工作正常。
My guess is either that your cells "A10:A15" hold incorrectly formatted values (Maybe strings), or you are using old version of EPPlus/Excel. 我的猜测是您的单元格“ A10:A15”保存了格式错误的值(可能是字符串),或者您使用的是旧版的EPPlus / Excel。
Please check your data by trying the formula on another new column holding numeric values, and try to update your EPPLUS version. 请通过尝试在另一个包含数值的新列上查找公式来检查数据,并尝试更新EPPLUS版本。
Code I used: 我使用的代码:
using (ExcelPackage pkg = new ExcelPackage(new FileInfo(@"D:\testSheet.xlsx")))
{
pkg.Workbook.Worksheets.Add("sheet");
pkg.Workbook.Worksheets.ElementAt(0).Cells["A10:A15"].Value = 2;
pkg.Workbook.Worksheets.ElementAt(0).Cells["C5"].Formula = "SUM(LARGE(A10:A15, {1,2}))";
pkg.Save();
}
Result: 结果:
Looks like this kind of formula only works when save the excel to file. 看起来这种公式仅在将excel保存到文件时有效。 In my case I don't want a file, just the calculated value.
在我的情况下,我不需要文件,只是想要的计算值。
The solution was to extend de ExcelFunction class and create my own implementation: 解决方案是扩展de <de> ExcelFunction类并创建我自己的实现:
public class SumLargest: ExcelFunction
{
public override CompileResult Execute(IEnumerable<FunctionArgument> arguments, ParsingContext context)
{
ValidateArguments(arguments, 2);
//n largest values
var n = ArgToInt(arguments, 1);
var cells = ArgsToDoubleEnumerable(arguments, context);
var values = cells.ToList();
values.RemoveAt(values.Count() - 1);
var result = values
.OrderByDescending(x => x)
.Take(n)
.Sum();
return CreateResult(result, DataType.Decimal);
}
}
Then I added it to my package: 然后我将其添加到我的包中:
using (ExcelPackage excelPackage = new ExcelPackage())
{
excelPackage.Workbook.FormulaParserManager.AddOrReplaceFunction("SUMLARGEST", new SumLargest());
....
}
And I can use it in a much better way then the original formula: 我可以使用比原始公式更好的方式使用它:
Cells["C5"].Formula = "SUMLARGEST(A10:A15, 2)"
Reference: https://github.com/JanKallman/EPPlus/wiki/Implementing-missing-or-new-Excel-functions 参考: https : //github.com/JanKallman/EPPlus/wiki/Implementing-missing-or-new-Excel-functions
The logfile entry provides the information that the Formula parser fails to convert the content of cell A9 on worksheet 1 to an integer. 日志文件条目提供了以下信息:公式解析器无法将工作表1上单元格A9的内容转换为整数。 This is an old thread, just thought that it could be useful to highlight if someone has a similar problem.
这是一个旧线程,只是认为突出显示某人是否有类似问题可能很有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.