it builds, ship it.
This commit is contained in:
1 parent
0b15570d00
commit
9570be8bfa
26 files changed
+334
-906
No files matched your search
@@ -0,0 +1,26 @@
|
||||
|
||||
using spark_excel.grammar;
|
||||
|
||||
namespace bts.excel
|
||||
{
|
||||
public interface IExpression
|
||||
{
|
||||
object evaluate();
|
||||
}
|
||||
internal class Expression<T> : IExpression
|
||||
{
|
||||
private readonly ExcelFormulaParser.ExprContext _context;
|
||||
private readonly IExcelFormulaVisitor<T> _visitor;
|
||||
|
||||
public Expression(ExcelFormulaParser.ExprContext context, IExcelFormulaVisitor<T> visitor)
|
||||
{
|
||||
_context = context;
|
||||
_visitor = visitor;
|
||||
}
|
||||
|
||||
public object evaluate()
|
||||
{
|
||||
return _context.Accept(_visitor);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -95,16 +95,11 @@ namespace bts.excel
|
||||
return ErrorValue.Name;
|
||||
|
||||
var function = _library.functions[name];
|
||||
|
||||
// Prepare the function arguments.
|
||||
var args = new FunctionArgs();
|
||||
var expr = context.expr();
|
||||
args.Parameters = new IExpression[expr.Length];
|
||||
var args = new IExpression[expr.Length];
|
||||
for (var i = 0; i < expr.Length; i++)
|
||||
{
|
||||
args.Parameters[i] = new Expression(expr[i], this);
|
||||
}
|
||||
return out;
|
||||
args[i] = new Expression<object>(expr[i], this);
|
||||
return function.Delegate(args);
|
||||
}
|
||||
|
||||
public override object VisitParenthesis(ExcelFormulaParser.ParenthesisContext context)
|
||||
|
||||
@@ -5,29 +5,11 @@ using bts.excel.functions;
|
||||
|
||||
namespace bts.excel.functions.lib
|
||||
{
|
||||
public interface IFunctionCaller
|
||||
{
|
||||
string sheet { get; }
|
||||
int row { get; }
|
||||
int column { get; }
|
||||
}
|
||||
public interface IExpression
|
||||
{
|
||||
object evaluate();
|
||||
}
|
||||
public interface IFunctionArgs
|
||||
{
|
||||
IExpression[] parameters { get; set; }
|
||||
bool has_result { get; }
|
||||
object result { get; set; }
|
||||
string code { get; set; }
|
||||
}
|
||||
|
||||
public delegate void FunctionDelegate(IFunctionArgs args, IFunctionCaller caller);
|
||||
public delegate object FunctionDelegate(IExpression[] args);
|
||||
public class Function
|
||||
{
|
||||
public FunctionDelegate Delegate { get; set; }
|
||||
public bool IsThreadSafe { get; set; }
|
||||
public bool thread_safe { get; set; }
|
||||
}
|
||||
public class FunctionLibrary
|
||||
{
|
||||
@@ -52,7 +34,7 @@ namespace bts.excel.functions.lib
|
||||
|
||||
public void add_function(string name, FunctionDelegate function, bool isThreadSafe = true)
|
||||
{
|
||||
_functions.Add(name, new Function { Delegate = function, IsThreadSafe = isThreadSafe });
|
||||
_functions.Add(name, new Function { Delegate = function, thread_safe = isThreadSafe });
|
||||
}
|
||||
|
||||
public void add_functions(Dictionary<string, Function> delegates)
|
||||
|
||||
@@ -11,10 +11,10 @@ namespace bts.excel.functions.lib
|
||||
{
|
||||
functions = new Dictionary<string, Function>(StringComparer.OrdinalIgnoreCase)
|
||||
{
|
||||
{"AND", new Function {Delegate = new And().Function, IsThreadSafe = true}},
|
||||
{"IF", new Function {Delegate = new If().Function, IsThreadSafe = true}},
|
||||
{"IFERROR", new Function {Delegate = new IfError().Function, IsThreadSafe = true}},
|
||||
{"OR", new Function {Delegate = new Or().Function, IsThreadSafe = true}},
|
||||
{"AND", new Function {Delegate = new And().function, thread_safe = true}},
|
||||
{"IF", new Function {Delegate = new If().function, thread_safe = true}},
|
||||
{"IFERROR", new Function {Delegate = new IfError().function, thread_safe = true}},
|
||||
{"OR", new Function {Delegate = new Or().function, thread_safe = true}},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,21 +7,16 @@ namespace bts.excel.functions.lib
|
||||
{
|
||||
public static Dictionary<string, Function> functions { get; }
|
||||
|
||||
static fathematics()
|
||||
static Mathematics()
|
||||
{
|
||||
functions = new Dictionary<string, Function>(StringComparer.OrdinalIgnoreCase)
|
||||
{
|
||||
{"LN", new Function {Delegate = new Ln().Function, IsThreadSafe = true}},
|
||||
{"LOG", new Function {Delegate = new Log().Function, IsThreadSafe = true}},
|
||||
{"POWER", new Function {Delegate = new Power().Function, IsThreadSafe = true}},
|
||||
{"PI", new Function {Delegate = new Pi().Function, IsThreadSafe = true}},
|
||||
{"RADIANS", new Function {Delegate = new Radians().Function, IsThreadSafe = true}},
|
||||
{"ROUND", new Function {Delegate = new Round().Function, IsThreadSafe = true}},
|
||||
{"ROUNDUP", new Function {Delegate = new RoundUp().Function, IsThreadSafe = true}},
|
||||
{"ROUNDDOWN", new Function {Delegate = new RoundDown().Function, IsThreadSafe = true}},
|
||||
{"SQRT", new Function {Delegate = new Sqrt().Function, IsThreadSafe = true}},
|
||||
{"SUM", new Function {Delegate = new Sum().Function, IsThreadSafe = true}},
|
||||
{"STDEV", new Function {Delegate = new Stdev().Function, IsThreadSafe = true}},
|
||||
{"LN", new Function {Delegate = new Ln().function, thread_safe = true}},
|
||||
{"LOG", new Function {Delegate = new Log().function, thread_safe = true}},
|
||||
{"POWER", new Function {Delegate = new Power().function, thread_safe = true}},
|
||||
{"PI", new Function {Delegate = new Pi().function, thread_safe = true}},
|
||||
{"SQRT", new Function {Delegate = new Sqrt().function, thread_safe = true}},
|
||||
{"SUM", new Function {Delegate = new Sum().function, thread_safe = true}},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,11 +11,9 @@ namespace bts.excel.functions.lib
|
||||
{
|
||||
functions = new Dictionary<string, Function>(StringComparer.OrdinalIgnoreCase)
|
||||
{
|
||||
{"AVERAGE", new Function {Delegate = new Average().Function, IsThreadSafe = true}},
|
||||
{"MAX", new Function {Delegate = new Max().Function, IsThreadSafe = true}},
|
||||
{"MIN", new Function {Delegate = new Min().Function, IsThreadSafe = true}},
|
||||
{"COUNT", new Function {Delegate = new Count().Function, IsThreadSafe = true}},
|
||||
{"MEDIAN", new Function {Delegate = new Median().Function, IsThreadSafe = true}},
|
||||
{"AVERAGE", new Function {Delegate = new Average().function, thread_safe= true}},
|
||||
{"MAX", new Function {Delegate = new Max().function, thread_safe= true}},
|
||||
{"MIN", new Function {Delegate = new Min().function, thread_safe = true}},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,49 +1,32 @@
|
||||
using System;
|
||||
|
||||
namespace bts.excel.functions
|
||||
namespace bts.excel.functions.lib
|
||||
{
|
||||
public class And
|
||||
{
|
||||
public void Function(IFunctionArgs args, IFunctionCaller caller)
|
||||
{
|
||||
// Syntax: AND(logical1, [logical2], ...)
|
||||
// https://support.office.com/en-gb/article/AND-function-5f19b2e8-e1df-4408-897a-ce285a19e9d9
|
||||
public class And
|
||||
{
|
||||
public object function(IExpression[] args)
|
||||
{
|
||||
// Syntax: AND(logical1, [logical2], ...)
|
||||
// https://support.office.com/en-gb/article/AND-function-5f19b2e8-e1df-4408-897a-ce285a19e9d9
|
||||
|
||||
if (args.parameters.Length < 2)
|
||||
{
|
||||
throw new ArgumentException(
|
||||
$"AND function requires at least 2 arguments, got {args.parameters.Length}",
|
||||
nameof(args));
|
||||
}
|
||||
if (args.Length < 2)
|
||||
{
|
||||
throw new ArgumentException(
|
||||
$"AND function requires at least 2 arguments, got {args.Length}",
|
||||
nameof(args));
|
||||
}
|
||||
|
||||
var result = true;
|
||||
string code = string.Empty;
|
||||
foreach (var fx in args)
|
||||
{
|
||||
var arg = fx.evaluate();
|
||||
|
||||
foreach (var fx in args.parameters)
|
||||
{
|
||||
var arg = fx.evaluate();
|
||||
|
||||
if (arg is ErrorValue)
|
||||
{
|
||||
args.result = arg;
|
||||
return;
|
||||
}
|
||||
|
||||
if (!(arg is bool))
|
||||
{
|
||||
args.Result = ErrorValue.Num;
|
||||
return;
|
||||
}
|
||||
|
||||
if ((bool)arg)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
result = false;
|
||||
break;
|
||||
}
|
||||
|
||||
args.Result = result;
|
||||
}
|
||||
}
|
||||
if (arg is ErrorValue)
|
||||
return arg;
|
||||
code += arg + " && \n";
|
||||
}
|
||||
code += "true";
|
||||
return code;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,44 +2,36 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace bts.excel.functions
|
||||
namespace bts.excel.functions.lib
|
||||
{
|
||||
public class Average
|
||||
{
|
||||
public void Function(IFunctionArgs args, IFunctionCaller caller)
|
||||
{
|
||||
// Syntax: AVERAGE(number1, [number2], ...)
|
||||
// https://support.office.com/en-GB/article/AVERAGE-function-047bac88-d466-426c-a32b-8f33eb960cf6
|
||||
public class Average
|
||||
{
|
||||
public object function(IExpression[] args)
|
||||
{
|
||||
// Syntax: AVERAGE(number1, [number2], ...)
|
||||
// https://support.office.com/en-GB/article/AVERAGE-function-047bac88-d466-426c-a32b-8f33eb960cf6
|
||||
|
||||
if (args.Parameters.Length < 1)
|
||||
{
|
||||
throw new ArgumentException(
|
||||
$"AVERAGE function takes at least 1 arguments, got {args.Parameters.Length}",
|
||||
nameof(args));
|
||||
}
|
||||
if (args.Length < 1)
|
||||
{
|
||||
throw new ArgumentException(
|
||||
$"AVERAGE function takes at least 1 arguments, got {args.Length}",
|
||||
nameof(args));
|
||||
}
|
||||
|
||||
var numbers = new List<double>();
|
||||
|
||||
foreach (var argsParameter in args.Parameters)
|
||||
{
|
||||
var arg = argsParameter.Evaluate();
|
||||
if (arg is ErrorValue)
|
||||
{
|
||||
args.Result = arg;
|
||||
return;
|
||||
}
|
||||
|
||||
var val = Conversion.ToDoubleOrErrorValue(arg);
|
||||
if (val is ErrorValue)
|
||||
{
|
||||
args.Result = val;
|
||||
return;
|
||||
}
|
||||
|
||||
numbers.Add((double)val);
|
||||
}
|
||||
|
||||
args.Result = numbers.Average();
|
||||
}
|
||||
}
|
||||
var numbers = new List<double>();
|
||||
|
||||
string code = "auto avg = 0.0; \n";
|
||||
int count = 0;
|
||||
foreach (var arg in args)
|
||||
{
|
||||
var argv = arg.evaluate();
|
||||
if (argv is ErrorValue)
|
||||
return argv;
|
||||
code += $"avg += {argv};\n";
|
||||
}
|
||||
if(count > 0)
|
||||
code += "avg /= {count};\n";
|
||||
return code;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,45 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using LibWorkbooks.LibDep;
|
||||
|
||||
namespace LibWorkbooks.StandardFunctions
|
||||
{
|
||||
public class Count
|
||||
{
|
||||
public void Function(IFunctionArgs args, IFunctionCaller caller)
|
||||
{
|
||||
// Syntax: COUNT(value1, [value2], ...)
|
||||
// https://support.office.com/en-gb/article/COUNT-function-a59cd7fc-b623-4d93-87a4-d23bf411294c
|
||||
|
||||
if (args.Parameters.Length < 1)
|
||||
{
|
||||
throw new ArgumentException(
|
||||
$"COUNT function takes at least 1 arguments, got {args.Parameters.Length}",
|
||||
nameof(args));
|
||||
}
|
||||
|
||||
var numbers = new List<double>();
|
||||
|
||||
foreach (var argsParameter in args.Parameters)
|
||||
{
|
||||
var arg = argsParameter.Evaluate();
|
||||
if (arg is ErrorValue)
|
||||
{
|
||||
args.Result = arg;
|
||||
return;
|
||||
}
|
||||
|
||||
var val = Conversion.ToDoubleOrErrorValue(arg);
|
||||
if (val is ErrorValue)
|
||||
{
|
||||
args.Result = val;
|
||||
return;
|
||||
}
|
||||
|
||||
numbers.Add((double)val);
|
||||
}
|
||||
|
||||
args.Result = (double)numbers.Count;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,41 +1,35 @@
|
||||
using System;
|
||||
using LibWorkbooks.LibDep;
|
||||
|
||||
namespace LibWorkbooks.StandardFunctions
|
||||
namespace bts.excel.functions.lib
|
||||
{
|
||||
public class If
|
||||
{
|
||||
public void Function(IFunctionArgs args, IFunctionCaller caller)
|
||||
{
|
||||
// Syntax: IF(logical_test, value_if_true, [value_if_false])
|
||||
// https://support.office.com/en-gb/article/IF-function-69aed7c9-4e8a-4755-a9bc-aa8bbff73be2
|
||||
public class If
|
||||
{
|
||||
public object function(IExpression[] args)
|
||||
{
|
||||
// Syntax: IF(logical_test, value_if_true, [value_if_false])
|
||||
// https://support.office.com/en-gb/article/IF-function-69aed7c9-4e8a-4755-a9bc-aa8bbff73be2
|
||||
|
||||
if (args.Parameters.Length != 2 && args.Parameters.Length != 3)
|
||||
{
|
||||
throw new ArgumentException(
|
||||
"IF function takes 2 or 3 arguments",
|
||||
nameof(args));
|
||||
}
|
||||
if (args.Length != 2 && args.Length != 3)
|
||||
{
|
||||
throw new ArgumentException(
|
||||
"IF function takes 2 or 3 arguments",
|
||||
nameof(args));
|
||||
}
|
||||
|
||||
var arg = args.Parameters[0].Evaluate();
|
||||
if (arg is ErrorValue)
|
||||
{
|
||||
args.Result = arg;
|
||||
return;
|
||||
}
|
||||
var arg = args[0].evaluate();
|
||||
if (arg is ErrorValue)
|
||||
return arg;
|
||||
|
||||
if (args.Parameters.Length == 3)
|
||||
{
|
||||
args.Result = Convert.ToBoolean(arg)
|
||||
? args.Parameters[1].Evaluate()
|
||||
: args.Parameters[2].Evaluate();
|
||||
}
|
||||
else
|
||||
{
|
||||
args.Result = Convert.ToBoolean(arg)
|
||||
? args.Parameters[1].Evaluate()
|
||||
: false;
|
||||
}
|
||||
}
|
||||
}
|
||||
string code = string.Empty;
|
||||
if (args.Length == 3)
|
||||
{
|
||||
code += $"( {arg} ? { args[1].evaluate() } : {args[2].evaluate() };";
|
||||
}
|
||||
else
|
||||
{
|
||||
code += $"( {arg} ? { args[1].evaluate() } : false;";
|
||||
}
|
||||
return code;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,16 +1,14 @@
|
||||
using System;
|
||||
using LibWorkbooks.LibDep;
|
||||
|
||||
namespace LibWorkbooks.StandardFunctions
|
||||
namespace bts.excel.functions.lib
|
||||
{
|
||||
public class IfError
|
||||
{
|
||||
public void Function(IFunctionArgs args, IFunctionCaller caller)
|
||||
public object function(IExpression[] args)
|
||||
{
|
||||
// Syntax: IFERROR(value, value_if_error)
|
||||
// https://support.office.com/en-gb/article/IFERROR-function-c526fd07-caeb-47b8-8bb6-63f3e417f611
|
||||
|
||||
if (args.Parameters.Length != 2)
|
||||
if (args.Length != 2)
|
||||
{
|
||||
throw new ArgumentException(
|
||||
"IFERROR function takes exactly 2 arguments",
|
||||
@@ -19,19 +17,16 @@ namespace LibWorkbooks.StandardFunctions
|
||||
|
||||
try
|
||||
{
|
||||
var arg = args.Parameters[0].Evaluate();
|
||||
var arg = args[0].evaluate();
|
||||
if (arg is ErrorValue)
|
||||
{
|
||||
args.Result = args.Parameters[1].Evaluate();
|
||||
return;
|
||||
}
|
||||
return arg;
|
||||
|
||||
args.Result = arg;
|
||||
return arg;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
args.Result = args.Parameters[1].Evaluate();
|
||||
return args[1].evaluate();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,36 +1,26 @@
|
||||
using System;
|
||||
using LibWorkbooks.LibDep;
|
||||
|
||||
namespace LibWorkbooks.StandardFunctions
|
||||
namespace bts.excel.functions.lib
|
||||
{
|
||||
public class Ln
|
||||
{
|
||||
public void Function(IFunctionArgs args, IFunctionCaller caller)
|
||||
public object function(IExpression[] args)
|
||||
{
|
||||
// Syntax: LN(number)
|
||||
// https://support.office.com/en-gb/article/LN-function-81fe1ed7-dac9-4acd-ba1d-07a142c6118f
|
||||
|
||||
if (args.Parameters.Length != 1)
|
||||
if (args.Length != 1)
|
||||
{
|
||||
throw new ArgumentException(
|
||||
$"LN function takes only 1 argument, got {args.Parameters.Length}");
|
||||
$"LN function takes only 1 argument, got {args.Length}");
|
||||
}
|
||||
|
||||
var arg = args.Parameters[0].Evaluate();
|
||||
var arg = args[0].evaluate();
|
||||
if (arg is ErrorValue)
|
||||
{
|
||||
args.Result = arg;
|
||||
return;
|
||||
}
|
||||
return arg;
|
||||
|
||||
var val = Conversion.ToDoubleOrErrorValue(arg);
|
||||
if (val is ErrorValue)
|
||||
{
|
||||
args.Result = val;
|
||||
return;
|
||||
}
|
||||
|
||||
args.Result = Math.Log((double)val);
|
||||
string code = $"std::log({arg});\n";
|
||||
return code;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,81 +1,40 @@
|
||||
using System;
|
||||
using LibWorkbooks.LibDep;
|
||||
|
||||
namespace LibWorkbooks.StandardFunctions
|
||||
namespace bts.excel.functions.lib
|
||||
{
|
||||
public class Log
|
||||
{
|
||||
public void Function(IFunctionArgs args, IFunctionCaller caller)
|
||||
{
|
||||
// Syntax: LOG(number, [base])
|
||||
// https://support.office.com/en-gb/article/LOG-function-4e82f196-1ca9-4747-8fb0-6c4a3abb3280
|
||||
public class Log
|
||||
{
|
||||
public object function(IExpression[] args)
|
||||
{
|
||||
// Syntax: LOG(number, [base])
|
||||
// https://support.office.com/en-gb/article/LOG-function-4e82f196-1ca9-4747-8fb0-6c4a3abb3280
|
||||
|
||||
switch (args.Parameters.Length)
|
||||
{
|
||||
case 1:
|
||||
var arg = args.Parameters[0].Evaluate();
|
||||
if (arg is ErrorValue)
|
||||
{
|
||||
args.Result = arg;
|
||||
return;
|
||||
}
|
||||
switch (args.Length)
|
||||
{
|
||||
case 1:
|
||||
var arg = args[0].evaluate();
|
||||
if (arg is ErrorValue)
|
||||
return arg;
|
||||
|
||||
var val = Conversion.ToDoubleOrErrorValue(arg);
|
||||
if (val is ErrorValue)
|
||||
{
|
||||
args.Result = val;
|
||||
return;
|
||||
}
|
||||
string code = $"std::log10({arg});\n";
|
||||
return code;
|
||||
|
||||
args.Result = Math.Log((double)val, 10);
|
||||
break;
|
||||
case 2:
|
||||
var arg1 = args[0].evaluate();
|
||||
if (arg1 is ErrorValue)
|
||||
return arg1;
|
||||
|
||||
case 2:
|
||||
var arg1 = args.Parameters[0].Evaluate();
|
||||
if (arg1 is ErrorValue)
|
||||
{
|
||||
args.Result = arg1;
|
||||
return;
|
||||
}
|
||||
var arg2 = args[1].evaluate();
|
||||
if (arg2 is ErrorValue)
|
||||
return arg2;
|
||||
|
||||
var arg2 = args.Parameters[1].Evaluate();
|
||||
if (arg2 is ErrorValue)
|
||||
{
|
||||
args.Result = arg2;
|
||||
return;
|
||||
}
|
||||
string code2 = $"std::log({arg1},{arg2});\n";
|
||||
return code2;
|
||||
|
||||
var val1 = Conversion.ToDoubleOrErrorValue(arg1);
|
||||
if (val1 is ErrorValue)
|
||||
{
|
||||
args.Result = val1;
|
||||
return;
|
||||
}
|
||||
|
||||
var val2 = Conversion.ToDoubleOrErrorValue(arg2);
|
||||
if (val2 is ErrorValue)
|
||||
{
|
||||
args.Result = val2;
|
||||
return;
|
||||
}
|
||||
|
||||
var logE = Math.Log((double)val1);
|
||||
var base1 = (double)val2;
|
||||
if (base1 == Math.E)
|
||||
{
|
||||
args.Result = logE;
|
||||
}
|
||||
else
|
||||
{
|
||||
args.Result = logE / Math.Log(base1);
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new ArgumentException(
|
||||
$"LOG function takes only 1 or 2 arguments, got {args.Parameters.Length}",
|
||||
nameof(args));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
default:
|
||||
throw new ArgumentException(
|
||||
$"LOG function takes only 1 or 2 arguments, got {args.Length}",
|
||||
nameof(args));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,46 +1,35 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using LibWorkbooks.LibDep;
|
||||
|
||||
namespace LibWorkbooks.StandardFunctions
|
||||
namespace bts.excel.functions.lib
|
||||
{
|
||||
public class Max
|
||||
{
|
||||
public void Function(IFunctionArgs args, IFunctionCaller caller)
|
||||
{
|
||||
// Syntax: MAX(number1, [number2], ...)
|
||||
// https://support.office.com/en-GB/article/MAX-function-e0012414-9ac8-4b34-9a47-73e662c08098
|
||||
public class Max
|
||||
{
|
||||
public object function(IExpression[] args)
|
||||
{
|
||||
// Syntax: MAX(number1, [number2], ...)
|
||||
// https://support.office.com/en-GB/article/MAX-function-e0012414-9ac8-4b34-9a47-73e662c08098
|
||||
|
||||
if (args.Parameters.Length < 1)
|
||||
{
|
||||
throw new ArgumentException(
|
||||
$"MAX function takes at least 1 arguments, got {args.Parameters.Length}",
|
||||
nameof(args));
|
||||
}
|
||||
if (args.Length < 1)
|
||||
{
|
||||
throw new ArgumentException(
|
||||
$"MAX function takes at least 1 arguments, got {args.Length}",
|
||||
nameof(args));
|
||||
}
|
||||
|
||||
string code = $"std::max(\n";
|
||||
foreach (var arg in args)
|
||||
{
|
||||
var argv = arg.evaluate();
|
||||
if (argv is ErrorValue)
|
||||
return argv;
|
||||
|
||||
code += $"{argv},";
|
||||
}
|
||||
code += $"{ args[args.Length-1].evaluate()} );\n";
|
||||
|
||||
var numbers = new List<double>();
|
||||
|
||||
foreach (var argsParameter in args.Parameters)
|
||||
{
|
||||
var arg = argsParameter.Evaluate();
|
||||
if (arg is ErrorValue)
|
||||
{
|
||||
args.Result = arg;
|
||||
return;
|
||||
}
|
||||
|
||||
var val = Conversion.ToDoubleOrErrorValue(arg);
|
||||
if (val is ErrorValue)
|
||||
{
|
||||
args.Result = val;
|
||||
return;
|
||||
}
|
||||
|
||||
numbers.Add((double)val);
|
||||
}
|
||||
|
||||
args.Result = numbers.Max();
|
||||
}
|
||||
}
|
||||
}
|
||||
return code;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,59 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using LibWorkbooks.LibDep;
|
||||
|
||||
namespace LibWorkbooks.StandardFunctions
|
||||
{
|
||||
public class Median
|
||||
{
|
||||
public void Function(IFunctionArgs args, IFunctionCaller caller)
|
||||
{
|
||||
// Syntax: MEDIAN(number1, [number2], ...)
|
||||
// https://support.office.com/en-gb/article/median-function-d0916313-4753-414c-8537-ce85bdd967d2
|
||||
|
||||
if (args.Parameters.Length < 1)
|
||||
{
|
||||
throw new ArgumentException(
|
||||
$"MEDIAN function takes at least 1 arguments, got {args.Parameters.Length}",
|
||||
nameof(args));
|
||||
}
|
||||
|
||||
var numbers = new List<double>();
|
||||
|
||||
foreach (var argsParameter in args.Parameters)
|
||||
{
|
||||
var arg = argsParameter.Evaluate();
|
||||
if (arg is ErrorValue)
|
||||
{
|
||||
args.Result = arg;
|
||||
return;
|
||||
}
|
||||
|
||||
var val = Conversion.ToDoubleOrErrorValue(arg);
|
||||
if (val is ErrorValue)
|
||||
{
|
||||
args.Result = val;
|
||||
return;
|
||||
}
|
||||
|
||||
numbers.Add((double)val);
|
||||
}
|
||||
|
||||
double F(double[] assessments)
|
||||
{
|
||||
Array.Sort(assessments);
|
||||
|
||||
var arrayLength = assessments.Length;
|
||||
|
||||
if (arrayLength % 2 == 0)
|
||||
{
|
||||
return ((assessments[(arrayLength / 2) - 1] + assessments[arrayLength / 2]) / 2);
|
||||
}
|
||||
|
||||
return assessments[(int)Math.Floor((double)arrayLength / 2)];
|
||||
}
|
||||
|
||||
args.Result = F(numbers.ToArray());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,46 +1,35 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using LibWorkbooks.LibDep;
|
||||
|
||||
namespace LibWorkbooks.StandardFunctions
|
||||
namespace bts.excel.functions.lib
|
||||
{
|
||||
public class Min
|
||||
{
|
||||
public void Function(IFunctionArgs args, IFunctionCaller caller)
|
||||
{
|
||||
// Syntax: MIN(number1, [number2], ...)
|
||||
// https://support.office.com/en-GB/article/MIN-function-61635d12-920f-4ce2-a70f-96f202dcc152
|
||||
public class Min
|
||||
{
|
||||
public object function(IExpression[] args)
|
||||
{
|
||||
// Syntax: MIN(number1, [number2], ...)
|
||||
// https://support.office.com/en-GB/article/MIN-function-61635d12-920f-4ce2-a70f-96f202dcc152
|
||||
|
||||
if (args.Parameters.Length < 1)
|
||||
{
|
||||
throw new ArgumentException(
|
||||
$"MIN function takes at least 1 arguments, got {args.Parameters.Length}",
|
||||
nameof(args));
|
||||
}
|
||||
if (args.Length < 1)
|
||||
{
|
||||
throw new ArgumentException(
|
||||
$"MIN function takes at least 1 arguments, got {args.Length}",
|
||||
nameof(args));
|
||||
}
|
||||
|
||||
var numbers = new List<double>();
|
||||
string code = $"std::min(\n";
|
||||
foreach (var arg in args)
|
||||
{
|
||||
var argv = arg.evaluate();
|
||||
if (argv is ErrorValue)
|
||||
return argv;
|
||||
|
||||
foreach (var argsParameter in args.Parameters)
|
||||
{
|
||||
var arg = argsParameter.Evaluate();
|
||||
if (arg is ErrorValue)
|
||||
{
|
||||
args.Result = arg;
|
||||
return;
|
||||
}
|
||||
code += $"{argv},";
|
||||
}
|
||||
code += $"{ args[args.Length - 1].evaluate()} );\n";
|
||||
|
||||
var val = Conversion.ToDoubleOrErrorValue(arg);
|
||||
if (val is ErrorValue)
|
||||
{
|
||||
args.Result = val;
|
||||
return;
|
||||
}
|
||||
|
||||
numbers.Add((double)val);
|
||||
}
|
||||
|
||||
args.Result = numbers.Min();
|
||||
}
|
||||
}
|
||||
}
|
||||
return code;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,41 +1,25 @@
|
||||
using LibWorkbooks.LibDep;
|
||||
using System;
|
||||
|
||||
namespace LibWorkbooks.StandardFunctions
|
||||
namespace bts.excel.functions.lib
|
||||
{
|
||||
public class Or
|
||||
{
|
||||
public void Function(IFunctionArgs args, IFunctionCaller caller)
|
||||
{
|
||||
// Syntax: OR(logical1, [logical2], ...)
|
||||
// https://support.office.com/en-gb/article/OR-function-7d17ad14-8700-4281-b308-00b131e22af0
|
||||
public class Or
|
||||
{
|
||||
public object function(IExpression[] args)
|
||||
{
|
||||
// Syntax: OR(logical1, [logical2], ...)
|
||||
// https://support.office.com/en-gb/article/OR-function-7d17ad14-8700-4281-b308-00b131e22af0
|
||||
|
||||
var result = false;
|
||||
string code = string.Empty;
|
||||
foreach (var fx in args)
|
||||
{
|
||||
var arg = fx.evaluate();
|
||||
|
||||
foreach (var fx in args.Parameters)
|
||||
{
|
||||
var arg = fx.Evaluate();
|
||||
if (arg is ErrorValue)
|
||||
{
|
||||
args.Result = arg;
|
||||
return;
|
||||
}
|
||||
|
||||
if (!(arg is bool))
|
||||
{
|
||||
args.Result = ErrorValue.Num;
|
||||
return;
|
||||
}
|
||||
|
||||
if (!(bool)arg)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
result = true;
|
||||
break;
|
||||
}
|
||||
|
||||
args.Result = result;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (arg is ErrorValue)
|
||||
return arg;
|
||||
code += arg + " || \n";
|
||||
}
|
||||
code += "false";
|
||||
return code;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,23 +1,22 @@
|
||||
using System;
|
||||
using LibWorkbooks.LibDep;
|
||||
|
||||
namespace LibWorkbooks.StandardFunctions
|
||||
namespace bts.excel.functions.lib
|
||||
{
|
||||
public class Pi
|
||||
{
|
||||
public void Function(IFunctionArgs args, IFunctionCaller caller)
|
||||
{
|
||||
// Syntax: PI()
|
||||
// https://support.office.com/en-gb/article/PI-function-264199d0-a3ba-46b8-975a-c4a04608989b
|
||||
public class Pi
|
||||
{
|
||||
public object function(IExpression[] args)
|
||||
{
|
||||
// Syntax: PI()
|
||||
// https://support.office.com/en-gb/article/PI-function-264199d0-a3ba-46b8-975a-c4a04608989b
|
||||
|
||||
if (args.Parameters.Length != 0)
|
||||
{
|
||||
throw new ArgumentException(
|
||||
$"PI function takes no arguments, got {args.Parameters.Length}",
|
||||
nameof(args));
|
||||
}
|
||||
if (args.Length != 0)
|
||||
{
|
||||
throw new ArgumentException(
|
||||
$"PI function takes no arguments, got {args.Length}",
|
||||
nameof(args));
|
||||
}
|
||||
|
||||
args.Result = Math.PI;
|
||||
}
|
||||
}
|
||||
return Math.PI.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,51 +1,30 @@
|
||||
using System;
|
||||
using LibWorkbooks.LibDep;
|
||||
|
||||
namespace LibWorkbooks.StandardFunctions
|
||||
namespace bts.excel.functions.lib
|
||||
{
|
||||
public class Power
|
||||
{
|
||||
public void Function(IFunctionArgs args, IFunctionCaller caller)
|
||||
{
|
||||
// Syntax: POWER(number, power)
|
||||
// https://support.office.com/en-gb/article/POWER-function-d3f2908b-56f4-4c3f-895a-07fb519c362a
|
||||
public class Power
|
||||
{
|
||||
public object function(IExpression[] args)
|
||||
{
|
||||
// Syntax: POWER(number, power)
|
||||
// https://support.office.com/en-gb/article/POWER-function-d3f2908b-56f4-4c3f-895a-07fb519c362a
|
||||
|
||||
if (args.Parameters.Length != 2)
|
||||
{
|
||||
throw new ArgumentException(
|
||||
$"POWER function takes 2 arguments, got {args.Parameters.Length}",
|
||||
nameof(args));
|
||||
}
|
||||
if (args.Length != 2)
|
||||
{
|
||||
throw new ArgumentException(
|
||||
$"POWER function takes 2 arguments, got {args.Length}",
|
||||
nameof(args));
|
||||
}
|
||||
|
||||
var arg1 = args.Parameters[0].Evaluate();
|
||||
if (arg1 is ErrorValue)
|
||||
{
|
||||
args.Result = arg1;
|
||||
return;
|
||||
}
|
||||
var arg1 = args[0].evaluate();
|
||||
if (arg1 is ErrorValue)
|
||||
return arg1;
|
||||
|
||||
var arg2 = args.Parameters[1].Evaluate();
|
||||
if (arg2 is ErrorValue)
|
||||
{
|
||||
args.Result = arg2;
|
||||
return;
|
||||
}
|
||||
var arg2 = args[1].evaluate();
|
||||
if (arg2 is ErrorValue)
|
||||
return arg2;
|
||||
|
||||
var val1 = Conversion.ToDoubleOrErrorValue(arg1);
|
||||
if (val1 is ErrorValue)
|
||||
{
|
||||
args.Result = val1;
|
||||
return;
|
||||
}
|
||||
|
||||
var val2 = Conversion.ToDoubleOrErrorValue(arg2);
|
||||
if (val2 is ErrorValue)
|
||||
{
|
||||
args.Result = val2;
|
||||
return;
|
||||
}
|
||||
|
||||
args.Result = Math.Pow((double)val1, (double)val2);
|
||||
}
|
||||
}
|
||||
}
|
||||
return $"std::pow({arg1}, {arg2})";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,37 +0,0 @@
|
||||
using System;
|
||||
using LibWorkbooks.LibDep;
|
||||
|
||||
namespace LibWorkbooks.StandardFunctions
|
||||
{
|
||||
public class Radians
|
||||
{
|
||||
public void Function(IFunctionArgs args, IFunctionCaller caller)
|
||||
{
|
||||
// Syntax: RADIANS(angle)
|
||||
// https://support.office.com/en-gb/article/RADIANS-function-ac409508-3d48-45f5-ac02-1497c92de5bf
|
||||
|
||||
if (args.Parameters.Length != 1)
|
||||
{
|
||||
throw new ArgumentException(
|
||||
$"RADIANS function takes only 1 argument, got {args.Parameters.Length}");
|
||||
}
|
||||
|
||||
var arg = args.Parameters[0].Evaluate();
|
||||
if (arg is ErrorValue)
|
||||
{
|
||||
args.Result = arg;
|
||||
return;
|
||||
}
|
||||
|
||||
var val = Conversion.ToDoubleOrErrorValue(arg);
|
||||
if (val is ErrorValue)
|
||||
{
|
||||
args.Result = val;
|
||||
return;
|
||||
}
|
||||
|
||||
var angle = (double)val;
|
||||
args.Result = Math.PI / 180 * angle;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,99 +0,0 @@
|
||||
using System;
|
||||
using LibWorkbooks.LibDep;
|
||||
|
||||
namespace LibWorkbooks.StandardFunctions
|
||||
{
|
||||
public class Round
|
||||
{
|
||||
public void Function(IFunctionArgs args, IFunctionCaller caller)
|
||||
{
|
||||
// Syntax: ROUND(number, num_digits)
|
||||
// https://support.office.com/en-gb/article/ROUND-function-c018c5d8-40fb-4053-90b1-b3e7f61a213c
|
||||
|
||||
if (args.Parameters.Length < 1)
|
||||
{
|
||||
throw new ArgumentException(
|
||||
$"ROUND function takes at least 1 arguments, got {args.Parameters.Length}",
|
||||
nameof(args));
|
||||
}
|
||||
|
||||
var arg1 = args.Parameters[0].Evaluate();
|
||||
if (arg1 is ErrorValue)
|
||||
{
|
||||
args.Result = arg1;
|
||||
return;
|
||||
}
|
||||
var arg2 = new object();
|
||||
|
||||
if (args.Parameters.Length == 2)
|
||||
{
|
||||
arg2 = args.Parameters[1].Evaluate();
|
||||
if (arg2 is ErrorValue)
|
||||
{
|
||||
args.Result = arg2;
|
||||
return;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
arg2 = 0;
|
||||
}
|
||||
|
||||
var val1 = Conversion.ToDoubleOrErrorValue(arg1);
|
||||
if (val1 is ErrorValue)
|
||||
{
|
||||
args.Result = val1;
|
||||
return;
|
||||
}
|
||||
|
||||
var val2 = Conversion.ToInt32OrErrorValue(arg2);
|
||||
if (val2 is ErrorValue)
|
||||
{
|
||||
args.Result = val2;
|
||||
return;
|
||||
}
|
||||
|
||||
// Based on MathX.round(double n, int p) from NPOI
|
||||
|
||||
var n = (double)val1;
|
||||
var p = (int)val2;
|
||||
|
||||
if (p != 0)
|
||||
{
|
||||
var temp = Math.Pow(10, p);
|
||||
args.Result = Math.Round(n * temp) / temp;
|
||||
return;
|
||||
}
|
||||
|
||||
args.Result = Math.Round(n);
|
||||
}
|
||||
|
||||
/// <remarks>
|
||||
/// This function is used by the RoundUp and RoundDown functions in this file.
|
||||
/// See <a href="http://stackoverflow.com/a/13483008">this link</a> for the original example.
|
||||
/// </remarks>
|
||||
internal static decimal RoundFactor(int places)
|
||||
{
|
||||
var factor = 1m;
|
||||
|
||||
if (places < 0)
|
||||
{
|
||||
places = -places;
|
||||
for (var i = 0; i < places; i++)
|
||||
{
|
||||
factor /= 10m;
|
||||
}
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
for (var i = 0; i < places; i++)
|
||||
{
|
||||
factor *= 10m;
|
||||
}
|
||||
}
|
||||
|
||||
return factor;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,53 +0,0 @@
|
||||
using System;
|
||||
using LibWorkbooks.LibDep;
|
||||
|
||||
namespace LibWorkbooks.StandardFunctions
|
||||
{
|
||||
public class RoundDown
|
||||
{
|
||||
public void Function(IFunctionArgs args, IFunctionCaller caller)
|
||||
{
|
||||
// Syntax: ROUNDDOWN(number, num_digits)
|
||||
// https://support.office.com/en-gb/article/ROUNDDOWN-function-2ec94c73-241f-4b01-8c6f-17e6d7968f53
|
||||
|
||||
if (args.Parameters.Length != 2)
|
||||
{
|
||||
throw new ArgumentException("ROUNDDOWN function takes 2 arguments, " +
|
||||
$"got {args.Parameters.Length}");
|
||||
}
|
||||
|
||||
var arg1 = args.Parameters[0].Evaluate();
|
||||
if (arg1 is ErrorValue)
|
||||
{
|
||||
args.Result = arg1;
|
||||
return;
|
||||
}
|
||||
|
||||
var arg2 = args.Parameters[1].Evaluate();
|
||||
if (arg2 is ErrorValue)
|
||||
{
|
||||
args.Result = arg2;
|
||||
return;
|
||||
}
|
||||
|
||||
var val = Conversion.ToDoubleOrErrorValue(arg1);
|
||||
if (val is ErrorValue)
|
||||
{
|
||||
args.Result = val;
|
||||
return;
|
||||
}
|
||||
|
||||
var negative = (double)val < 0;
|
||||
|
||||
// This code snippet based on example at http://stackoverflow.com/a/13483008
|
||||
var @decimal = new decimal(Math.Abs((double)val));
|
||||
var places = Convert.ToInt32(arg2);
|
||||
var factor = Round.RoundFactor(places);
|
||||
@decimal *= factor;
|
||||
@decimal = Math.Floor(@decimal);
|
||||
@decimal /= factor;
|
||||
|
||||
args.Result = Convert.ToDouble(negative ? -@decimal : @decimal);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,53 +0,0 @@
|
||||
using System;
|
||||
using LibWorkbooks.LibDep;
|
||||
|
||||
namespace LibWorkbooks.StandardFunctions
|
||||
{
|
||||
public class RoundUp
|
||||
{
|
||||
public void Function(IFunctionArgs args, IFunctionCaller caller)
|
||||
{
|
||||
// Syntax: ROUNDUP(number, num_digits)
|
||||
// https://support.office.com/en-gb/article/ROUNDUP-function-f8bc9b23-e795-47db-8703-db171d0c42a7
|
||||
|
||||
if (args.Parameters.Length != 2)
|
||||
{
|
||||
throw new ArgumentException("ROUNDUP function takes 2 arguments, " +
|
||||
$"got {args.Parameters.Length}");
|
||||
}
|
||||
|
||||
var arg1 = args.Parameters[0].Evaluate();
|
||||
if (arg1 is ErrorValue)
|
||||
{
|
||||
args.Result = arg1;
|
||||
return;
|
||||
}
|
||||
|
||||
var arg2 = args.Parameters[1].Evaluate();
|
||||
if (arg2 is ErrorValue)
|
||||
{
|
||||
args.Result = arg2;
|
||||
return;
|
||||
}
|
||||
|
||||
var val = Conversion.ToDoubleOrErrorValue(arg1);
|
||||
if (val is ErrorValue)
|
||||
{
|
||||
args.Result = val;
|
||||
return;
|
||||
}
|
||||
|
||||
var negative = (double)val < 0;
|
||||
|
||||
// This code snippet based on example at http://stackoverflow.com/a/13483008
|
||||
var @decimal = new decimal(Math.Abs((double)val));
|
||||
var places = Convert.ToInt32(arg2);
|
||||
var factor = Round.RoundFactor(places);
|
||||
@decimal *= factor;
|
||||
@decimal = Math.Ceiling(@decimal);
|
||||
@decimal /= factor;
|
||||
|
||||
args.Result = Convert.ToDouble(negative ? -@decimal : @decimal);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,36 +1,25 @@
|
||||
using System;
|
||||
using LibWorkbooks.LibDep;
|
||||
|
||||
namespace LibWorkbooks.StandardFunctions
|
||||
namespace bts.excel.functions.lib
|
||||
{
|
||||
public class Sqrt
|
||||
{
|
||||
public void Function(IFunctionArgs args, IFunctionCaller caller)
|
||||
{
|
||||
// Syntax: SQRT(number)
|
||||
// https://support.office.com/en-gb/article/SQRT-function-654975c2-05c4-4831-9a24-2c65e4040fdf
|
||||
public class Sqrt
|
||||
{
|
||||
public object function(IExpression[] args)
|
||||
{
|
||||
// Syntax: SQRT(number)
|
||||
// https://support.office.com/en-gb/article/SQRT-function-654975c2-05c4-4831-9a24-2c65e4040fdf
|
||||
|
||||
if (args.Parameters.Length != 1)
|
||||
{
|
||||
throw new ArgumentException(
|
||||
$"SQRT function takes only 1 argument, got {args.Parameters.Length}");
|
||||
}
|
||||
if (args.Length != 1)
|
||||
{
|
||||
throw new ArgumentException(
|
||||
$"SQRT function takes only 1 argument, got {args.Length}");
|
||||
}
|
||||
|
||||
var arg = args.Parameters[0].Evaluate();
|
||||
if (arg is ErrorValue)
|
||||
{
|
||||
args.Result = arg;
|
||||
return;
|
||||
}
|
||||
var arg1 = args[0].evaluate();
|
||||
if (arg1 is ErrorValue)
|
||||
return arg1;
|
||||
|
||||
var val = Conversion.ToDoubleOrErrorValue(arg);
|
||||
if (val is ErrorValue)
|
||||
{
|
||||
args.Result = val;
|
||||
return;
|
||||
}
|
||||
|
||||
args.Result = Conversion.ErrorValueOnInvalidDouble(Math.Sqrt((double)val));
|
||||
}
|
||||
}
|
||||
return $"std::sqrt({arg1})";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,52 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using LibWorkbooks.LibDep;
|
||||
|
||||
namespace LibWorkbooks.StandardFunctions
|
||||
{
|
||||
public class Stdev
|
||||
{
|
||||
public void Function(IFunctionArgs args, IFunctionCaller caller)
|
||||
{
|
||||
// Syntax: STDEV(number1,[number2],...)
|
||||
// https://support.office.com/en-gb/article/stdev-function-51fecaaa-231e-4bbb-9230-33650a72c9b0
|
||||
|
||||
if (args.Parameters.Length < 1)
|
||||
{
|
||||
throw new ArgumentException(
|
||||
$"STDEV function takes at least 1 arguments, got {args.Parameters.Length}",
|
||||
nameof(args));
|
||||
}
|
||||
|
||||
var numbers = new List<double>();
|
||||
|
||||
foreach (var argsParameter in args.Parameters)
|
||||
{
|
||||
var arg = argsParameter.Evaluate();
|
||||
if (arg is ErrorValue)
|
||||
{
|
||||
args.Result = arg;
|
||||
return;
|
||||
}
|
||||
|
||||
var val = Conversion.ToDoubleOrErrorValue(arg);
|
||||
if (val is ErrorValue)
|
||||
{
|
||||
args.Result = val;
|
||||
return;
|
||||
}
|
||||
|
||||
numbers.Add((double)val);
|
||||
}
|
||||
var mean = numbers.Average();
|
||||
var sum = 0.0;
|
||||
foreach (var number in numbers)
|
||||
{
|
||||
sum += Math.Pow(number - mean, 2);
|
||||
}
|
||||
|
||||
args.Result = Math.Sqrt(sum / (numbers.Count - 1));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,46 +1,34 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using LibWorkbooks.LibDep;
|
||||
|
||||
namespace LibWorkbooks.StandardFunctions
|
||||
namespace bts.excel.functions.lib
|
||||
{
|
||||
public class Sum
|
||||
{
|
||||
public void Function(IFunctionArgs args, IFunctionCaller caller)
|
||||
{
|
||||
// Syntax: SUM(number1,[number2],...)
|
||||
// https://support.office.com/en-GB/article/SUM-function-043e1c7d-7726-4e80-8f32-07b23e057f89
|
||||
public class Sum
|
||||
{
|
||||
public object function(IExpression[] args)
|
||||
{
|
||||
// Syntax: SUM(number1,[number2],...)
|
||||
// https://support.office.com/en-GB/article/SUM-function-043e1c7d-7726-4e80-8f32-07b23e057f89
|
||||
|
||||
if (args.Parameters.Length < 1)
|
||||
{
|
||||
throw new ArgumentException(
|
||||
$"SUM function takes at least 1 arguments, got {args.Parameters.Length}",
|
||||
nameof(args));
|
||||
}
|
||||
if (args.Length < 1)
|
||||
{
|
||||
throw new ArgumentException(
|
||||
$"SUM function takes at least 1 arguments, got {args.Length}",
|
||||
nameof(args));
|
||||
}
|
||||
|
||||
var numbers = new List<double>();
|
||||
string code = "auto sum = 0.0;\n";
|
||||
|
||||
foreach (var argsParameter in args.Parameters)
|
||||
{
|
||||
var arg = argsParameter.Evaluate();
|
||||
if (arg is ErrorValue)
|
||||
{
|
||||
args.Result = arg;
|
||||
return;
|
||||
}
|
||||
foreach (var arg in args)
|
||||
{
|
||||
var argv = arg.evaluate();
|
||||
if (arg is ErrorValue)
|
||||
return arg;
|
||||
|
||||
var val = Conversion.ToDoubleOrErrorValue(arg);
|
||||
if (val is ErrorValue)
|
||||
{
|
||||
args.Result = val;
|
||||
return;
|
||||
}
|
||||
|
||||
numbers.Add((double)val);
|
||||
}
|
||||
|
||||
args.Result = numbers.Sum();
|
||||
}
|
||||
}
|
||||
}
|
||||
code += $"sum += {argv};\n";
|
||||
}
|
||||
return code;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user