private double GetValue(ValueEval arg) { if (arg is NumberEval) { return(((NumberEval)arg).NumberValue); } if (arg is BlankEval) { return(0); } if (arg is RefEval) { RefEval refEval = (RefEval)arg; if (refEval.NumberOfSheets > 1) { // Multi-Sheet references are not supported throw new EvaluationException(ErrorEval.VALUE_INVALID); } ValueEval innerValueEval = refEval.GetInnerValueEval(refEval.FirstSheetIndex); if (innerValueEval is NumberEval) { return(((NumberEval)innerValueEval).NumberValue); } if (innerValueEval is BlankEval) { return(0); } } throw new EvaluationException(ErrorEval.VALUE_INVALID); }
public void TestUnusualArgs() { CultureShim.SetCurrentCulture("en-US"); // startPos with fractional digits ConfirmMid(new StringEval("galactic"), new NumberEval(3.1), new NumberEval(4), "lact"); // string startPos ConfirmMid(new StringEval("galactic"), new StringEval("3"), new NumberEval(4), "lact"); // text (first) arg type is number, other args are strings with fractional digits ConfirmMid(new NumberEval(123456), new StringEval("3.1"), new StringEval("2.9"), "34"); // startPos is 1x1 area ref, numChars is cell ref AreaEval aeStart = EvalFactory.CreateAreaEval("A1:A1", new ValueEval[] { new NumberEval(2), }); RefEval reNumChars = EvalFactory.CreateRefEval("B1", new NumberEval(3)); ConfirmMid(new StringEval("galactic"), aeStart, reNumChars, "ala"); ConfirmMid(new StringEval("galactic"), new NumberEval(3.1), BlankEval.instance, ""); ConfirmMid(new StringEval("galactic"), new NumberEval(3), BoolEval.FALSE, ""); ConfirmMid(new StringEval("galactic"), new NumberEval(3), BoolEval.TRUE, "l"); ConfirmMid(BlankEval.instance, new NumberEval(3), BoolEval.TRUE, ""); }
/** * Dereferences a single value from any AreaEval or RefEval evaluation result. * If the supplied evaluationResult is just a plain value, it is returned as-is. * @return a <tt>NumberEval</tt>, <tt>StringEval</tt>, <tt>BoolEval</tt>, * <tt>BlankEval</tt> or <tt>ErrorEval</tt>. Never <c>null</c>. */ private static ValueEval DereferenceValue(ValueEval evaluationResult, int srcRowNum, int srcColNum) { if (evaluationResult is RefEval) { RefEval rv = (RefEval)evaluationResult; return(rv.InnerValueEval); } if (evaluationResult is AreaEval) { AreaEval ae = (AreaEval)evaluationResult; if (ae.IsRow) { if (ae.IsColumn) { return(ae.GetRelativeValue(0, 0)); } return(ae.GetValueAt(ae.FirstRow, srcColNum)); } if (ae.IsColumn) { return(ae.GetValueAt(srcRowNum, ae.FirstColumn)); } return(ErrorEval.VALUE_INVALID); } return(evaluationResult); }
public override ValueEval Evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0) { ValueEval arg = arg0; if (arg is RefEval) { // always use the first sheet RefEval re = (RefEval)arg; arg = re.GetInnerValueEval(re.FirstSheetIndex); } else if (arg is AreaEval) { // when the arg is an area, choose the top left cell arg = ((AreaEval)arg).GetRelativeValue(0, 0); } if (arg is StringEval) { // Text values are returned unmodified return(arg); } if (arg is ErrorEval) { // Error values also returned unmodified return(arg); } // for all other argument types the result is empty string return(StringEval.EMPTY_INSTANCE); }
/** * Collects values from a single argument */ private void CollectValues(ValueEval operand, DoubleList temp) { if (operand is TwoDEval) { TwoDEval ae = (TwoDEval)operand; int width = ae.Width; int height = ae.Height; for (int rrIx = 0; rrIx < height; rrIx++) { for (int rcIx = 0; rcIx < width; rcIx++) { ValueEval ve = ae.GetValue(rrIx, rcIx); if (!IsSubtotalCounted && ae.IsSubTotal(rrIx, rcIx)) { continue; } CollectValue(ve, true, temp); } } return; } if (operand is RefEval) { RefEval re = (RefEval)operand; CollectValue(re.InnerValueEval, true, temp); return; } CollectValue((ValueEval)operand, false, temp); }
private ValueEval InternalResolveEval(Eval eval) { ValueEval retval; if (eval is StringValueEval) { retval = (StringValueEval)eval; } else if (eval is RefEval) { RefEval re = (RefEval)eval; ValueEval tve = re.InnerValueEval; if (tve is StringValueEval || tve is BlankEval) { retval = tve; } else { retval = ErrorEval.NAME_INVALID; } } else if (eval is BlankEval) { retval = (BlankEval)eval; } else { retval = ErrorEval.NAME_INVALID; } return(retval); }
private static void CollectValues(ValueEval arg, IList temp) { if (arg is AreaEval) { AreaEval ae = (AreaEval)arg; int width = ae.Width; int height = ae.Height; for (int rrIx = 0; rrIx < height; rrIx++) { for (int rcIx = 0; rcIx < width; rcIx++) { ValueEval ve1 = ae.GetRelativeValue(rrIx, rcIx); CollectValue(ve1, temp, false); } } return; } if (arg is RefEval) { RefEval re = (RefEval)arg; CollectValue(re.InnerValueEval, temp, true); return; } CollectValue(arg, temp, true); }
/** * uses the relevant flags to decode the supplied RefVal * @param eval */ private ValueEval XlateRefEval(RefEval reval) { ValueEval eval = reval.InnerValueEval; // most common case - least worries :) if (eval is NumberEval) { return(eval); } if (eval is BoolEval) { return(((flags & REF_BOOL_IS_PARSED) > 0) ? (ValueEval)eval : BlankEval.instance); } if (eval is StringEval) { return(XlateRefStringEval((StringEval)eval)); } if (eval is ErrorEval) { return(eval); } if (eval is BlankEval) { return(XlateBlankEval(REF_BLANK_IS_PARSED)); } throw new Exception("Invalid ValueEval type passed for conversion: (" + eval.GetType().Name + ")"); }
/** * Collects values from a single argument */ private void CollectValues(ValueEval operand, DoubleList temp) { if (operand is AreaEval) { AreaEval ae = (AreaEval)operand; int width = ae.Width; int height = ae.Height; for (int rrIx = 0; rrIx < height; rrIx++) { for (int rcIx = 0; rcIx < width; rcIx++) { ValueEval ve = ae.GetRelativeValue(rrIx, rcIx); CollectValue(ve, true, temp); } } return; } if (operand is RefEval) { RefEval re = (RefEval)operand; CollectValue(re.InnerValueEval, true, temp); return; } CollectValue((ValueEval)operand, false, temp); }
private static void CollectValues(ValueEval arg, IList temp) { if (arg is TwoDEval) { TwoDEval ae = (TwoDEval)arg; int width = ae.Width; int height = ae.Height; for (int rrIx = 0; rrIx < height; rrIx++) { for (int rcIx = 0; rcIx < width; rcIx++) { ValueEval ve1 = ae.GetValue(rrIx, rcIx); CollectValue(ve1, temp, false); } } return; } if (arg is RefEval) { RefEval re = (RefEval)arg; for (int sIx = re.FirstSheetIndex; sIx <= re.LastSheetIndex; sIx++) { CollectValue(re.GetInnerValueEval(sIx), temp, true); } return; } CollectValue(arg, temp, true); }
/** * @return 1 if the evaluated cell matches the specified criteria */ public static int CountMatchingCell(RefEval refEval, IMatchPredicate criteriaPredicate) { if (criteriaPredicate.Matches(refEval.InnerValueEval)) { return(1); } return(0); }
private static AreaEval ResolveRange(RefEval reA, RefEval reB) { int height = reB.Row - reA.Row; int width = reB.Column - reA.Column; return reA.Offset(0, height, 0, width); }
/** * @return 1 if the evaluated cell matches the specified criteria */ public static int CountMatchingCell(RefEval refEval, I_MatchPredicate criteriaPredicate) { if (criteriaPredicate.Matches(refEval.InnerValueEval)) { return 1; } return 0; }
public BaseRef(RefEval re) { _refEval = re; _areaEval = null; _firstRowIndex = re.Row; _firstColumnIndex = re.Column; _height = 1; _width = 1; }
public BaseRef(AreaEval ae) { _refEval = null; _areaEval = ae; _firstRowIndex = ae.FirstRow; _firstColumnIndex = ae.FirstColumn; _height = ae.LastRow - ae.FirstRow + 1; _width = ae.LastColumn - ae.FirstColumn + 1; }
private ValueEval UnwrapEval(ValueEval eval) { ValueEval comp = eval; while (comp is RefEval) { RefEval reference = (RefEval)comp; comp = reference.GetInnerValueEval(reference.FirstSheetIndex); } return(comp); }
/** * recursively Evaluate any RefEvals * @param reval */ protected ValueEval XlateRefEval(RefEval reval) { ValueEval retval = (ValueEval)reval.InnerValueEval; if (retval is RefEval) { RefEval re = (RefEval)retval; retval = XlateRefEval(re); } return retval; }
public void TestScalarSimple() { RefEval refEval = EvalFactory.CreateRefEval("A1", new NumberEval(3)); ValueEval[] args = { refEval, new NumberEval(2), }; ValueEval result = InvokeSumproduct(args); ConfirmDouble(6D, result); }
/** * @return the number of evaluated cells in the range that match the specified criteria */ public static int CountMatchingCellsInRef(RefEval refEval, IMatchPredicate criteriaPredicate) { int result = 0; for (int sIx = refEval.FirstSheetIndex; sIx <= refEval.LastSheetIndex; sIx++) { ValueEval ve = refEval.GetInnerValueEval(sIx); if (criteriaPredicate.Matches(ve)) { result++; } } return(result); }
/** * @return the number of evaluated cells in the range that match the specified criteria */ public static int CountMatchingCellsInRef(RefEval refEval, IMatchPredicate criteriaPredicate) { int result = 0; for (int sIx = refEval.FirstSheetIndex; sIx <= refEval.LastSheetIndex; sIx++) { ValueEval ve = refEval.GetInnerValueEval(sIx); if (criteriaPredicate.Matches(ve)) { result++; } } return result; }
/** * Resolve reference(-chains) until we have a normal value. * * @param field a ValueEval which can be a RefEval. * @return a ValueEval which is guaranteed not to be a RefEval * @If a multi-sheet reference was found along the way. */ private static ValueEval solveReference(ValueEval field) { if (field is RefEval) { RefEval refEval = (RefEval)field; if (refEval.NumberOfSheets > 1) { throw new EvaluationException(ErrorEval.VALUE_INVALID); } return(solveReference(refEval.GetInnerValueEval(refEval.FirstSheetIndex))); } else { return(field); } }
private static bool IsNumberEval(Eval eval) { bool retval = false; if (eval is NumberEval) { retval = true; } else if (eval is RefEval) { RefEval re = (RefEval)eval; ValueEval ve = re.InnerValueEval; retval = (ve is NumberEval); } return(retval); }
/** * The second argument (table_array) should be an area ref, but can actually be a cell ref, in * which case it Is interpreted as a 1x1 area ref. Other scalar values cause #VALUE! error. */ public static AreaEval ResolveTableArrayArg(ValueEval eval) { if (eval is AreaEval) { return((AreaEval)eval); } if (eval is RefEval) { RefEval refEval = (RefEval)eval; // Make this cell ref look like a 1x1 area ref. // It doesn't matter if eval is a 2D or 3D ref, because that detail is never asked of AreaEval. return(refEval.Offset(0, 0, 0, 0)); } throw EvaluationException.InvalidValue(); }
private static ValueVector EvaluateLookupRange(ValueEval eval) { if (eval is RefEval) { RefEval re = (RefEval)eval; if (re.NumberOfSheets == 1) { return(new SingleValueVector(re.GetInnerValueEval(re.FirstSheetIndex))); } else { return(LookupUtils.CreateVector(re)); } } if (eval is TwoDEval) { ValueVector result = LookupUtils.CreateVector((TwoDEval)eval); if (result == null) { throw new EvaluationException(ErrorEval.NA); } return(result); } // Error handling for lookup_range arg Is also Unusual if (eval is NumericValueEval) { throw new EvaluationException(ErrorEval.NA); } if (eval is StringEval) { StringEval se = (StringEval)eval; double d = OperandResolver.ParseDouble(se.StringValue); if (double.IsNaN(d)) { // plain string throw new EvaluationException(ErrorEval.VALUE_INVALID); } // else looks like a number throw new EvaluationException(ErrorEval.NA); } throw new Exception("Unexpected eval type (" + eval.GetType().Name + ")"); }
private static double GetScalarValue(ValueEval arg) { ValueEval eval; if (arg is RefEval) { RefEval re = (RefEval)arg; if (re.NumberOfSheets > 1) { throw new EvaluationException(ErrorEval.VALUE_INVALID); } eval = re.GetInnerValueEval(re.FirstSheetIndex); } else { eval = arg; } if (eval == null) { throw new ArgumentException("parameter may not be null"); } if (eval is AreaEval) { AreaEval ae = (AreaEval)eval; // an area ref can work as a scalar value if it is 1x1 if (!ae.IsColumn || !ae.IsRow) { throw new EvaluationException(ErrorEval.VALUE_INVALID); } eval = ae.GetRelativeValue(0, 0); } if (!(eval is ValueEval)) { throw new ArgumentException("Unexpected value eval class (" + eval.GetType().Name + ")"); } return(GetProductTerm((ValueEval)eval, true)); }
private static double GetDoubleValue(Eval eval) { double retval = 0; if (eval is NumberEval) { NumberEval ne = (NumberEval)eval; retval = ne.NumberValue; } else if (eval is RefEval) { RefEval re = (RefEval)eval; ValueEval ve = re.InnerValueEval; retval = (ve is NumberEval) ? ((NumberEval)ve).NumberValue : double.NaN; } else if (eval is ErrorEval) { retval = double.NaN; } return(retval); }
public override ValueEval Evaluate(int srcRowIndex, int srcColumnIndex, ValueEval numberVE) { String hex; if (numberVE is RefEval) { RefEval re = (RefEval)numberVE; hex = OperandResolver.CoerceValueToString(re.GetInnerValueEval(re.FirstSheetIndex)); } else { hex = OperandResolver.CoerceValueToString(numberVE); } try { return(new NumberEval(BaseNumberUtils.ConvertToDecimal(hex, HEXADECIMAL_BASE, MAX_NUMBER_OF_PLACES))); } catch (ArgumentException) { return(ErrorEval.NUM_ERROR); } }
public ValueEval Evaluate(ValueEval[] args, int srcCellRow, int srcCellCol) { switch (args.Length) { case 2: // expected break; default: // TODO - it doesn't seem to be possible to enter COUNTIF() into Excel with the wrong arg Count // perhaps this should be an exception return(ErrorEval.VALUE_INVALID); } ValueEval range = (ValueEval)args[0]; ValueEval criteriaArg = args[1]; if (criteriaArg is RefEval) { // criteria Is not a literal value, but a cell reference // for example COUNTIF(B2:D4, E1) RefEval re = (RefEval)criteriaArg; criteriaArg = re.InnerValueEval; } else { // other non literal tokens such as function calls, have been fully Evaluated // for example COUNTIF(B2:D4, COLUMN(E1)) } if (criteriaArg is BlankEval) { return(NumberEval.ZERO); } I_MatchPredicate mp = CreateCriteriaPredicate(criteriaArg); return(CountMatchingCellsInArea(range, mp)); }
public RefValueArray(RefEval ref1) : base(1) { _ref = ref1; }
public SheetVector(RefEval re) { _size = re.NumberOfSheets; _re = re; }
public static ValueVector CreateVector(RefEval re) { return new SheetVector(re); }
public static ValueVector CreateVector(RefEval re) { return(new SheetVector(re)); }
public override ValueEval Evaluate(int srcRowIndex, int srcColumnIndex, ValueEval numberVE) { String number; if (numberVE is RefEval) { RefEval re = (RefEval)numberVE; number = OperandResolver.CoerceValueToString(re.GetInnerValueEval(re.FirstSheetIndex)); } else { number = OperandResolver.CoerceValueToString(numberVE); } if (number.Length > 10) { return(ErrorEval.NUM_ERROR); } String unsigned; //If the leftmost bit is 0 -- number is positive. bool isPositive; if (number.Length < 10) { unsigned = number; isPositive = true; } else { unsigned = number.Substring(1); isPositive = number.StartsWith("0"); } String value; try { if (isPositive) { //bit9*2^8 + bit8*2^7 + bit7*2^6 + bit6*2^5 + bit5*2^4+ bit3*2^2+ bit2*2^1+ bit1*2^0 int sum = getDecimalValue(unsigned); value = sum.ToString(); } else { //The leftmost bit is 1 -- this is negative number //Inverse bits [1-9] String inverted = toggleBits(unsigned); // Calculate decimal number int sum = getDecimalValue(inverted); //Add 1 to obtained number sum++; value = "-" + sum.ToString(); } } catch (FormatException) { return(ErrorEval.NUM_ERROR); } return(new NumberEval(long.Parse(value))); }
private bool Calculate(ValueEval[] args) { bool result = InitialResultValue; bool atleastOneNonBlank = false; /* * Note: no short-circuit bool loop exit because any ErrorEvals will override the result */ foreach (ValueEval arg in args) { bool?tempVe; if (arg is TwoDEval) { TwoDEval ae = (TwoDEval)arg; int height = ae.Height; int width = ae.Width; for (int rrIx = 0; rrIx < height; rrIx++) { for (int rcIx = 0; rcIx < width; rcIx++) { ValueEval ve = ae.GetValue(rrIx, rcIx); tempVe = OperandResolver.CoerceValueToBoolean(ve, true); if (tempVe != null) { result = PartialEvaluate(result, Convert.ToBoolean(tempVe, CultureInfo.InvariantCulture)); atleastOneNonBlank = true; } } } continue; } if (arg is RefEval) { RefEval re = (RefEval)arg; int firstSheetIndex = re.FirstSheetIndex; int lastSheetIndex = re.LastSheetIndex; for (int sIx = firstSheetIndex; sIx <= lastSheetIndex; sIx++) { ValueEval ve = re.GetInnerValueEval(sIx); tempVe = OperandResolver.CoerceValueToBoolean(ve, true); if (tempVe != null) { result = PartialEvaluate(result, tempVe.Value); atleastOneNonBlank = true; } } continue; } //else if (arg is ValueEval) //{ // ValueEval ve = (ValueEval)arg; // tempVe = OperandResolver.CoerceValueToBoolean(ve, false); //} if (arg == MissingArgEval.instance) { tempVe = null; // you can leave out parameters, they are simply ignored } else { tempVe = OperandResolver.CoerceValueToBoolean(arg, false); } if (tempVe != null) { result = PartialEvaluate(result, Convert.ToBoolean(tempVe, CultureInfo.InvariantCulture)); atleastOneNonBlank = true; } } if (!atleastOneNonBlank) { throw new EvaluationException(ErrorEval.VALUE_INVALID); } return(result); }
public RefValueArray(RefEval ref1) : base(ref1.NumberOfSheets) { _ref = ref1; _width = ref1.NumberOfSheets; }
protected Eval XlateRefEval(RefEval reval) { Eval retval = reval.InnerValueEval; if (retval is RefEval) { retval = XlateRefEval((RefEval)retval); } return retval; }
/** * uses the relevant flags to decode the supplied RefVal * @param eval */ private ValueEval XlateRefEval(RefEval reval) { ValueEval eval = reval.InnerValueEval; // most common case - least worries :) if (eval is NumberEval) { return eval; } if (eval is BoolEval) { return ((flags & REF_BOOL_IS_PARSED) > 0) ? (ValueEval)eval : BlankEval.instance; } if (eval is StringEval) { return XlateRefStringEval((StringEval)eval); } if (eval is ErrorEval) { return eval; } if (eval is BlankEval) { return XlateBlankEval(REF_BLANK_IS_PARSED); } throw new Exception("Invalid ValueEval type passed for conversion: (" + eval.GetType().Name + ")"); }
private static ValueEval ChooseSingleElementFromRef(RefEval ref1) { return ref1.GetInnerValueEval(ref1.FirstSheetIndex); }
private static ValueEval ChooseSingleElementFromRef(RefEval ref1) { return(ref1.GetInnerValueEval(ref1.FirstSheetIndex)); }