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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*
* Licensed to the Apache Software Foundation (ASF) Under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for Additional information regarding copyright ownership.
* The ASF licenses this file to You Under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License.  You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed Under the License is distributed on an "AS Is" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations Under the License.
*/
 
namespace HH.WMS.Utils.NPOI.SS.Formula.Functions
{
    using System;
    using HH.WMS.Utils.NPOI.SS.Formula.Eval;
    
    using HH.WMS.Utils.NPOI.SS.Formula;
    using System.Text;
 
    /**
     * Implementation for Excel function INDIRECT<p/>
     * 
     * INDIRECT() returns the cell or area reference denoted by the text argument.<p/> 
     * 
     * <b>Syntax</b>:<br/>
     * <b>INDIRECT</b>(<b>ref_text</b>,isA1Style)<p/>
     * 
     * <b>ref_text</b> a string representation of the desired reference as it would normally be written
     * in a cell formula.<br/>
     * <b>isA1Style</b> (default TRUE) specifies whether the ref_text should be interpreted as A1-style
     * or R1C1-style.
     * 
     * 
     * @author Josh Micich
     */
    public class Indirect : FreeRefFunction
    {
 
        public static FreeRefFunction instance = new Indirect();
 
        private Indirect()
        {
            // enforce singleton
        }
 
        public ValueEval Evaluate(ValueEval[] args, OperationEvaluationContext ec)
        {
            if (args.Length < 1)
            {
                return ErrorEval.VALUE_INVALID;
            }
 
            bool isA1style;
            String text;
            try
            {
                ValueEval ve = OperandResolver.GetSingleValue(args[0], ec.RowIndex, ec
                        .ColumnIndex);
                text = OperandResolver.CoerceValueToString(ve);
                switch (args.Length)
                {
                    case 1:
                        isA1style = true;
                        break;
                    case 2:
                        isA1style = EvaluateBooleanArg(args[1], ec);
                        break;
                    default:
                        return ErrorEval.VALUE_INVALID;
                }
            }
            catch (EvaluationException e)
            {
                return e.GetErrorEval();
            }
 
            return EvaluateIndirect(ec, text, isA1style);
        }
 
        private static bool EvaluateBooleanArg(ValueEval arg, OperationEvaluationContext ec)
        {
            ValueEval ve = OperandResolver.GetSingleValue(arg, ec.RowIndex, ec.ColumnIndex);
 
            if (ve == BlankEval.instance || ve == MissingArgEval.instance)
            {
                return false;
            }
            // numeric quantities follow standard bool conversion rules
            // for strings, only "TRUE" and "FALSE" (case insensitive) are valid
            return (bool)OperandResolver.CoerceValueToBoolean(ve, false);
        }
 
        private static ValueEval EvaluateIndirect(OperationEvaluationContext ec, String text,
                bool isA1style)
        {
            // Search backwards for '!' because sheet names can contain '!'
            int plingPos = text.LastIndexOf('!');
 
            String workbookName;
            String sheetName;
            String refText; // whitespace around this Gets Trimmed OK
            if (plingPos < 0)
            {
                workbookName = null;
                sheetName = null;
                refText = text;
            }
            else
            {
                String[] parts = ParseWorkbookAndSheetName(text.Substring(0, plingPos));
                if (parts == null)
                {
                    return ErrorEval.REF_INVALID;
                }
                workbookName = parts[0];
                sheetName = parts[1];
                refText = text.Substring(plingPos + 1);
            }
 
            String refStrPart1;
            String refStrPart2;
 
            int colonPos = refText.IndexOf(':');
            if (colonPos < 0)
            {
                refStrPart1 = refText.Trim();
                refStrPart2 = null;
            }
            else
            {
                refStrPart1 = refText.Substring(0, colonPos).Trim();
                refStrPart2 = refText.Substring(colonPos + 1).Trim();
            }
            return ec.GetDynamicReference(workbookName, sheetName, refStrPart1, refStrPart2, isA1style);
        }
 
        /**
         * @return array of length 2: {workbookName, sheetName,}.  Second element will always be
         * present.  First element may be null if sheetName is unqualified.
         * Returns <code>null</code> if text cannot be parsed.
         */
        private static String[] ParseWorkbookAndSheetName(string text)
        {
            int lastIx = text.Length - 1;
            if (lastIx < 0)
            {
                return null;
            }
            if (CanTrim(text))
            {
                return null;
            }
            char firstChar = text[0];
            if (Char.IsWhiteSpace(firstChar))
            {
                return null;
            }
            if (firstChar == '\'')
            {
                // workbookName or sheetName needs quoting
                // quotes go around both
                if (text[lastIx] != '\'')
                {
                    return null;
                }
                firstChar = text[1];
                if (Char.IsWhiteSpace(firstChar))
                {
                    return null;
                }
                String wbName;
                int sheetStartPos;
                if (firstChar == '[')
                {
                    int rbPos = text.ToString().LastIndexOf(']');
                    if (rbPos < 0)
                    {
                        return null;
                    }
                    wbName = UnescapeString(text.Substring(2, rbPos - 2));
                    if (wbName == null || CanTrim(wbName))
                    {
                        return null;
                    }
                    sheetStartPos = rbPos + 1;
                }
                else
                {
                    wbName = null;
                    sheetStartPos = 1;
                }
 
                // else - just sheet name
                String sheetName = UnescapeString(text.Substring(sheetStartPos, lastIx - sheetStartPos));
                if (sheetName == null)
                { // note - when quoted, sheetName can
                    // start/end with whitespace
                    return null;
                }
                return new String[] { wbName, sheetName, };
            }
 
            if (firstChar == '[')
            {
                int rbPos = text.ToString().LastIndexOf(']');
                if (rbPos < 0)
                {
                    return null;
                }
                string wbName = text.Substring(1, rbPos - 1);
                if (CanTrim(wbName))
                {
                    return null;
                }
                string sheetName = text.Substring(rbPos + 1);
                if (CanTrim(sheetName))
                {
                    return null;
                }
                return new String[] { wbName.ToString(), sheetName.ToString(), };
            }
            // else - just sheet name
            return new String[] { null, text.ToString(), };
        }
 
        /**
         * @return <code>null</code> if there is a syntax error in any escape sequence
         * (the typical syntax error is a single quote character not followed by another).
         */
        private static String UnescapeString(string text)
        {
            int len = text.Length;
            StringBuilder sb = new StringBuilder(len);
            int i = 0;
            while (i < len)
            {
                char ch = text[i];
                if (ch == '\'')
                {
                    // every quote must be followed by another
                    i++;
                    if (i >= len)
                    {
                        return null;
                    }
                    ch = text[i];
                    if (ch != '\'')
                    {
                        return null;
                    }
                }
                sb.Append(ch);
                i++;
            }
            return sb.ToString();
        }
 
        private static bool CanTrim(string text)
        {
            int lastIx = text.Length - 1;
            if (lastIx < 0)
            {
                return false;
            }
            if (Char.IsWhiteSpace(text[0]))
            {
                return true;
            }
            if (Char.IsWhiteSpace(text[lastIx]))
            {
                return true;
            }
            return false;
        }
    }
}