Beispiel #1
0
        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);
        }
Beispiel #2
0
        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);
        }
Beispiel #3
0
 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);
 }
        private ValueEval UnwrapEval(ValueEval eval)
        {
            ValueEval comp = eval;

            while (comp is RefEval)
            {
                RefEval reference = (RefEval)comp;
                comp = reference.GetInnerValueEval(reference.FirstSheetIndex);
            }
            return(comp);
        }
Beispiel #5
0
            public ValueEval GetItem(int index)
            {
                if (index >= _size)
                {
                    throw new IndexOutOfRangeException("Specified index (" + index
                                                       + ") is outside the allowed range (0.." + (_size - 1) + ")");
                }
                int sheetIndex = _re.FirstSheetIndex + index;

                return(_re.GetInnerValueEval(sheetIndex));
            }
Beispiel #6
0
 /**
  * Collects values from a single argument
  */
 private void CollectValues(ValueEval operand, DoubleList temp)
 {
     if (operand is ThreeDEval)
     {
         ThreeDEval ae = (ThreeDEval)operand;
         for (int sIx = ae.FirstSheetIndex; sIx <= ae.LastSheetIndex; sIx++)
         {
             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(sIx, rrIx, rcIx);
                     if (!IsSubtotalCounted && ae.IsSubTotal(rrIx, rcIx))
                     {
                         continue;
                     }
                     CollectValue(ve, true, temp);
                 }
             }
         }
         return;
     }
     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;
         for (int sIx = re.FirstSheetIndex; sIx <= re.LastSheetIndex; sIx++)
         {
             CollectValue(re.GetInnerValueEval(sIx), true, temp);
         }
         return;
     }
     CollectValue((ValueEval)operand, false, temp);
 }
Beispiel #7
0
        /**
         * @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);
        }
Beispiel #8
0
        /**
         * @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;
        }
Beispiel #9
0
 /**
  * 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);
     }
 }
Beispiel #10
0
        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 + ")");
        }
Beispiel #11
0
        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));
        }
Beispiel #12
0
        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);
            }
        }
Beispiel #13
0
 private static ValueEval ChooseSingleElementFromRef(RefEval ref1)
 {
     return ref1.GetInnerValueEval(ref1.FirstSheetIndex);
 }
Beispiel #14
0
            protected override ValueEval GetItemInternal(int index)
            {
                int sIx = (index % _width) + _ref.FirstSheetIndex;

                return(_ref.GetInnerValueEval(sIx));
            }
Beispiel #15
0
        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)));
        }
Beispiel #16
0
 private static ValueEval ChooseSingleElementFromRef(RefEval ref1)
 {
     return(ref1.GetInnerValueEval(ref1.FirstSheetIndex));
 }
Beispiel #17
0
        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);
        }