示例#1
0
        public override ValueEval Evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0,
                                           ValueEval arg1)
        {
            String arg;
            int    index;

            try
            {
                arg   = TextFunction.EvaluateStringArg(arg0, srcRowIndex, srcColumnIndex);
                index = TextFunction.EvaluateIntArg(arg1, srcRowIndex, srcColumnIndex);
            }
            catch (EvaluationException e)
            {
                return(e.GetErrorEval());
            }

            if (index < 0)
            {
                return(ErrorEval.VALUE_INVALID);
            }

            String result;

            if (_isLeft)
            {
                result = arg.Substring(0, Math.Min(arg.Length, index));
            }
            else
            {
                result = arg.Substring(Math.Max(0, arg.Length - index));
            }
            return(new StringEval(result));
        }
示例#2
0
 public override ValueEval Evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0, ValueEval arg1)
 {
     try
     {
         String needle   = TextFunction.EvaluateStringArg(arg0, srcRowIndex, srcColumnIndex);
         String haystack = TextFunction.EvaluateStringArg(arg1, srcRowIndex, srcColumnIndex);
         return(Eval(haystack, needle, 0));
     }
     catch (EvaluationException e)
     {
         return(e.GetErrorEval());
     }
 }
示例#3
0
文件: Clean.cs 项目: zhgl7688/-
        public override ValueEval Evaluate(String arg)
        {
            StringBuilder result = new StringBuilder();

            for (int i = 0; i < arg.Length; i++)
            {
                char c = arg[i];
                if (TextFunction.IsPrintable(c))
                {
                    result.Append(c);
                }
            }
            return(new StringEval(result.ToString()));
        }
示例#4
0
文件: CHAR.cs 项目: zhgl7688/-
        public override ValueEval Evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0)
        {
            int arg;

            try
            {
                arg = TextFunction.EvaluateIntArg(arg0, srcRowIndex, srcColumnIndex);
                if (arg < 0 || arg >= 256)
                {
                    throw new EvaluationException(ErrorEval.VALUE_INVALID);
                }
            }
            catch (EvaluationException e)
            {
                return(e.GetErrorEval());
            }
            return(new StringEval(Convert.ToString((char)arg, CultureInfo.CurrentCulture)));
        }
示例#5
0
 public override ValueEval Evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0, ValueEval arg1,
                                    ValueEval arg2)
 {
     try
     {
         String needle   = TextFunction.EvaluateStringArg(arg0, srcRowIndex, srcColumnIndex);
         String haystack = TextFunction.EvaluateStringArg(arg1, srcRowIndex, srcColumnIndex);
         // evaluate third arg and convert from 1-based to 0-based index
         int startpos = TextFunction.EvaluateIntArg(arg2, srcRowIndex, srcColumnIndex) - 1;
         if (startpos < 0)
         {
             return(ErrorEval.VALUE_INVALID);
         }
         return(Eval(haystack, needle, startpos));
     }
     catch (EvaluationException e)
     {
         return(e.GetErrorEval());
     }
 }
示例#6
0
        public override ValueEval Evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0, ValueEval arg1)
        {
            double s0;
            String s1;

            try
            {
                s0 = TextFunction.EvaluateDoubleArg(arg0, srcRowIndex, srcColumnIndex);
                s1 = TextFunction.EvaluateStringArg(arg1, srcRowIndex, srcColumnIndex);
            }
            catch (EvaluationException e)
            {
                return(e.GetErrorEval());
            }
            if (Regex.Match(s1, "[y|m|M|d|s|h]+").Success)
            {
                //may be datetime string
                ValueEval result = TryParseDateTime(s0, s1);
                if (result != ErrorEval.VALUE_INVALID)
                {
                    return(result);
                }
            }
            //The regular expression needs ^ and $.
            if (Regex.Match(s1, @"^[\d,\#,\.,\$,\,]+$").Success)
            {
                //TODO: simulate DecimalFormat class in java.
                FormatBase formatter = new DecimalFormat(s1);
                return(new StringEval(formatter.Format(s0)));
            }
            else if (s1.IndexOf("/", StringComparison.Ordinal) == s1.LastIndexOf("/", StringComparison.Ordinal) && s1.IndexOf("/", StringComparison.Ordinal) >= 0 && !s1.Contains("-"))
            {
                double wholePart = Math.Floor(s0);
                double decPart   = s0 - wholePart;
                if (wholePart * decPart == 0)
                {
                    return(new StringEval("0"));
                }
                String[] parts = s1.Split(' ');
                String[] fractParts;
                if (parts.Length == 2)
                {
                    fractParts = parts[1].Split('/');
                }
                else
                {
                    fractParts = s1.Split('/');
                }

                if (fractParts.Length == 2)
                {
                    double minVal    = 1.0;
                    double currDenom = Math.Pow(10, fractParts[1].Length) - 1d;
                    double currNeum  = 0;
                    for (int i = (int)(Math.Pow(10, fractParts[1].Length) - 1d); i > 0; i--)
                    {
                        for (int i2 = (int)(Math.Pow(10, fractParts[1].Length) - 1d); i2 > 0; i2--)
                        {
                            if (minVal >= Math.Abs((double)i2 / (double)i - decPart))
                            {
                                currDenom = i;
                                currNeum  = i2;
                                minVal    = Math.Abs((double)i2 / (double)i - decPart);
                            }
                        }
                    }
                    FormatBase neumFormatter  = new DecimalFormat(fractParts[0]);
                    FormatBase denomFormatter = new DecimalFormat(fractParts[1]);
                    if (parts.Length == 2)
                    {
                        FormatBase wholeFormatter = new DecimalFormat(parts[0]);
                        String     result         = wholeFormatter.Format(wholePart) + " " + neumFormatter.Format(currNeum) + "/" + denomFormatter.Format(currDenom);
                        return(new StringEval(result));
                    }
                    else
                    {
                        String result = neumFormatter.Format(currNeum + (currDenom * wholePart)) + "/" + denomFormatter.Format(currDenom);
                        return(new StringEval(result));
                    }
                }
                else
                {
                    return(ErrorEval.VALUE_INVALID);
                }
            }
            else
            {
                return(TryParseDateTime(s0, s1));
            }
        }