public void AverageAArray() { // For arrays, AverageA completely ignores booleans. It divides by strings and numbers, but only // numbers are added to the total. Real dates cannot be specified and string dates are not parsed. AverageA average = new AverageA(); var date = new DateTime(2013, 1, 15); double[] values = { 0, 2000, 0, 0, 0 }; var result = average.Execute(new FunctionArgument[] { new FunctionArgument(new FunctionArgument[] { new FunctionArgument(1000.ToString("n")), new FunctionArgument(2000), new FunctionArgument(6000.ToString("n")), new FunctionArgument(true), new FunctionArgument(date.ToString("d")), new FunctionArgument("test") }) }, ParsingContext.Create()); Assert.AreEqual(values.Average(), result.Result); }
public void AverageACellReferences() { // For cell references, AverageA divides by all cells, but only adds actual numbers, dates, and booleans. ExcelPackage package = new ExcelPackage(); var worksheet = package.Workbook.Worksheets.Add("Test"); double[] values = { 0, 2000, 0, 1, new DateTime(2013, 1, 5).ToOADate(), 0 }; ExcelRange range1 = worksheet.Cells[1, 1]; range1.Formula = "\"1000\""; range1.Calculate(); var range2 = worksheet.Cells[1, 2]; range2.Value = 2000; var range3 = worksheet.Cells[1, 3]; range3.Formula = $"\"{new DateTime(2013, 1, 5).ToString("d")}\""; range3.Calculate(); var range4 = worksheet.Cells[1, 4]; range4.Value = true; var range5 = worksheet.Cells[1, 5]; range5.Value = new DateTime(2013, 1, 5); var range6 = worksheet.Cells[1, 6]; range6.Value = "Test"; AverageA average = new AverageA(); var rangeInfo1 = new EpplusExcelDataProvider.RangeInfo(worksheet, 1, 1, 1, 3); var rangeInfo2 = new EpplusExcelDataProvider.RangeInfo(worksheet, 1, 4, 1, 4); var rangeInfo3 = new EpplusExcelDataProvider.RangeInfo(worksheet, 1, 5, 1, 6); var context = ParsingContext.Create(); var address = new OfficeOpenXml.FormulaParsing.ExcelUtilities.RangeAddress(); address.FromRow = address.ToRow = address.FromCol = address.ToCol = 2; context.Scopes.NewScope(address); var result = average.Execute(new FunctionArgument[] { new FunctionArgument(rangeInfo1), new FunctionArgument(rangeInfo2), new FunctionArgument(rangeInfo3) }, context); Assert.AreEqual(values.Average(), result.Result); }
public BuiltInFunctions() { // Text Functions["len"] = new Len(); Functions["lower"] = new Lower(); Functions["upper"] = new Upper(); Functions["left"] = new Left(); Functions["right"] = new Right(); Functions["mid"] = new Mid(); Functions["replace"] = new Replace(); Functions["rept"] = new Rept(); Functions["substitute"] = new Substitute(); Functions["concatenate"] = new Concatenate(); Functions["char"] = new CharFunction(); Functions["exact"] = new Exact(); Functions["find"] = new Find(); Functions["fixed"] = new Fixed(); Functions["proper"] = new Proper(); Functions["text"] = new Text.Text(); Functions["t"] = new T(); Functions["hyperlink"] = new Hyperlink(); // Numbers Functions["int"] = new CInt(); // Math Functions["abs"] = new Abs(); Functions["asin"] = new Asin(); Functions["asinh"] = new Asinh(); Functions["cos"] = new Cos(); Functions["cosh"] = new Cosh(); Functions["power"] = new Power(); Functions["sign"] = new Sign(); Functions["sqrt"] = new Sqrt(); Functions["sqrtpi"] = new SqrtPi(); Functions["pi"] = new Pi(); Functions["product"] = new Product(); Functions["ceiling"] = new Ceiling(); Functions["count"] = new Count(); Functions["counta"] = new CountA(); Functions["countblank"] = new CountBlank(); Functions["countif"] = new CountIf(); Functions["countifs"] = new CountIfs(); Functions["fact"] = new Fact(); Functions["floor"] = new Floor(); Functions["sin"] = new Sin(); Functions["sinh"] = new Sinh(); Functions["sum"] = new Sum(); Functions["sumif"] = new SumIf(); Functions["sumifs"] = new SumIfs(); Functions["sumproduct"] = new SumProduct(); Functions["sumsq"] = new Sumsq(); Functions["stdev"] = new Stdev(); Functions["stdevp"] = new StdevP(); Functions["stdev.s"] = new Stdev(); Functions["stdev.p"] = new StdevP(); Functions["subtotal"] = new Subtotal(); Functions["exp"] = new Exp(); Functions["log"] = new Log(); Functions["log10"] = new Log10(); Functions["ln"] = new Ln(); Functions["max"] = new Max(); Functions["maxa"] = new Maxa(); Functions["median"] = new Median(); Functions["min"] = new Min(); Functions["mina"] = new Mina(); Functions["mod"] = new Mod(); Functions["average"] = new Average(); Functions["averagea"] = new AverageA(); Functions["averageif"] = new AverageIf(); Functions["averageifs"] = new AverageIfs(); Functions["round"] = new Round(); Functions["rounddown"] = new Rounddown(); Functions["roundup"] = new Roundup(); Functions["rand"] = new Rand(); Functions["randbetween"] = new RandBetween(); Functions["quotient"] = new Quotient(); Functions["trunc"] = new Trunc(); Functions["tan"] = new Tan(); Functions["tanh"] = new Tanh(); Functions["atan"] = new Atan(); Functions["atan2"] = new Atan2(); Functions["atanh"] = new Atanh(); Functions["acos"] = new Acos(); Functions["acosh"] = new Acosh(); Functions["var"] = new Var(); Functions["varp"] = new VarP(); Functions["large"] = new Large(); Functions["small"] = new Small(); Functions["degrees"] = new Degrees(); // Information Functions["isblank"] = new IsBlank(); Functions["isnumber"] = new IsNumber(); Functions["istext"] = new IsText(); Functions["isnontext"] = new IsNonText(); Functions["iserror"] = new IsError(); Functions["iserr"] = new IsErr(); Functions["error.type"] = new ErrorType(); Functions["iseven"] = new IsEven(); Functions["isodd"] = new IsOdd(); Functions["islogical"] = new IsLogical(); Functions["isna"] = new IsNa(); Functions["na"] = new Na(); Functions["n"] = new N(); // Logical Functions["if"] = new If(); Functions["iferror"] = new IfError(); Functions["ifna"] = new IfNa(); Functions["not"] = new Not(); Functions["and"] = new And(); Functions["or"] = new Or(); Functions["true"] = new True(); Functions["false"] = new False(); // Reference and lookup Functions["address"] = new Address(); Functions["hlookup"] = new HLookup(); Functions["vlookup"] = new VLookup(); Functions["lookup"] = new Lookup(); Functions["match"] = new Match(); Functions["row"] = new Row(){SkipArgumentEvaluation = true}; Functions["rows"] = new Rows(){SkipArgumentEvaluation = true}; Functions["column"] = new Column(){SkipArgumentEvaluation = true}; Functions["columns"] = new Columns(){SkipArgumentEvaluation = true}; Functions["choose"] = new Choose(); Functions["index"] = new Index(); Functions["indirect"] = new Indirect(); Functions["offset"] = new Offset(){SkipArgumentEvaluation = true}; // Date Functions["date"] = new Date(); Functions["today"] = new Today(); Functions["now"] = new Now(); Functions["day"] = new Day(); Functions["month"] = new Month(); Functions["year"] = new Year(); Functions["time"] = new Time(); Functions["hour"] = new Hour(); Functions["minute"] = new Minute(); Functions["second"] = new Second(); Functions["weeknum"] = new Weeknum(); Functions["weekday"] = new Weekday(); Functions["days360"] = new Days360(); Functions["yearfrac"] = new Yearfrac(); Functions["edate"] = new Edate(); Functions["eomonth"] = new Eomonth(); Functions["isoweeknum"] = new IsoWeekNum(); Functions["workday"] = new Workday(); // Database Functions["dget"] = new Dget(); Functions["dcount"] = new Dcount(); Functions["dcounta"] = new DcountA(); Functions["dmax"] = new Dmax(); Functions["dmin"] = new Dmin(); Functions["dsum"] = new Dsum(); Functions["daverage"] = new Daverage(); Functions["dvar"] = new Dvar(); Functions["dvarp"] = new Dvarp(); }
public void AverageALiterals() { // For literals, AverageA always parses and include numeric strings, date strings, bools, etc. // The only exception is unparsable string literals, which cause a #VALUE. AverageA average = new AverageA(); var date1 = new DateTime(2013, 1, 5); var date2 = new DateTime(2013, 1, 15); double value1 = 1000; double value2 = 2000; double value3 = 6000; double value4 = 1; double value5 = date1.ToOADate(); double value6 = date2.ToOADate(); var result = average.Execute(new FunctionArgument[] { new FunctionArgument(value1.ToString("n")), new FunctionArgument(value2), new FunctionArgument(value3.ToString("n")), new FunctionArgument(true), new FunctionArgument(date1), new FunctionArgument(date2.ToString("d")) }, ParsingContext.Create()); Assert.AreEqual((value1 + value2 + value3 + value4 + value5 + value6) / 6, result.Result); }
public void AverageAUnparsableLiteral() { // In the case of literals, any unparsable string literal results in a #VALUE. AverageA average = new AverageA(); var result = average.Execute(new FunctionArgument[] { new FunctionArgument(1000), new FunctionArgument("Test") }, ParsingContext.Create()); }
public void AverageAShouldThrowValueExceptionIfNonNumericTextIsSupplied() { var func = new AverageA(); var args = FunctionsHelper.CreateArgs(4d, 2d, 5d, 2d, "ABC"); var result = func.Execute(args, _parsingContext); }
public void AverageAShouldIncludeTrueAs1() { var expectedResult = (4d + 2d + 5d + 2d + 1d) / 5d; var func = new AverageA(); var args = FunctionsHelper.CreateArgs(4d, 2d, 5d, 2d, true); var result = func.Execute(args, _parsingContext); Assert.AreEqual(expectedResult, result.Result); }
public void AverageAShouldCountValueAs0IfNonNumericTextIsSuppliedInArray() { var func = new AverageA(); var args = FunctionsHelper.CreateArgs(FunctionsHelper.CreateArgs(1d, 2d, 3d, "ABC")); var result = func.Execute(args, _parsingContext); Assert.AreEqual(1.5d,result.Result); }
public void AverageAShouldCountNumericStringWithValue() { var func = new AverageA(); var args = FunctionsHelper.CreateArgs(4d, 2d, "9"); var result = func.Execute(args, _parsingContext); Assert.AreEqual(5d, result.Result); }
public void AverageAShouldCalculateCorrectResult() { var expectedResult = (4d + 2d + 5d + 2d) / 4d; var func = new AverageA(); var args = FunctionsHelper.CreateArgs(4d, 2d, 5d, 2d); var result = func.Execute(args, _parsingContext); Assert.AreEqual(expectedResult, result.Result); }