zhao
2021-07-07 2fdf959ac739edd6de84aa8053b8b9683dce8e8b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
using System;
using System.Collections.Generic;
using System.Text;
using HH.WMS.Utils.NPOI.SS.Formula.Eval;
 
namespace HH.WMS.Utils.NPOI.SS.Formula.Functions
{
    public class SearchFind : Var2or3ArgFunction
    {
 
        private bool _isCaseSensitive;
 
        public SearchFind(bool isCaseSensitive)
        {
            _isCaseSensitive = isCaseSensitive;
        }
        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();
            }
        }
        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();
            }
        }
        private ValueEval Eval(String haystack, String needle, int startIndex)
        {
            int result;
            if (_isCaseSensitive)
            {
                result = haystack.IndexOf(needle, startIndex, StringComparison.CurrentCulture);
            }
            else
            {
                //result = haystack.ToUpper().IndexOf(needle.ToUpper(), startIndex);
                result = haystack.IndexOf(needle, startIndex, StringComparison.CurrentCultureIgnoreCase);
            }
            if (result == -1)
            {
                return ErrorEval.VALUE_INVALID;
            }
            return new NumberEval(result + 1);
        }
    }
}