zhao
2021-06-04 c7ec496f9e41c2227103b3ef776e4a3f91bce6b2
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
 
/* ====================================================================
   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.HSSF.Record.Aggregates
{
 
    using System;
    using System.Text;
    using System.Collections;
    using HH.WMS.Utils.NPOI.DDF;
    using HH.WMS.Utils.NPOI.Util;
    using HH.WMS.Utils.NPOI.HSSF.Record;
    using HH.WMS.Utils.NPOI.SS.Formula;
    using HH.WMS.Utils.NPOI.HSSF.Model;
    using HH.WMS.Utils.NPOI.SS.Formula.PTG;
 
    /**
     *
     * Aggregate value records toGether.  Things are easier to handle that way.
     *
     * @author  andy
     * @author  Glen Stampoultzis (glens at apache.org)
     * @author Jason Height (jheight at chariot dot net dot au)
     */
 
    public class ValueRecordsAggregate
    {
        const int MAX_ROW_INDEX = 0XFFFF;
        const int INDEX_NOT_SET = -1;
        public const short sid = -1001; // 1000 clashes with RowRecordsAggregate
        int firstcell = INDEX_NOT_SET;
        int lastcell = INDEX_NOT_SET;
        CellValueRecordInterface[][] records;
 
        /** Creates a new instance of ValueRecordsAggregate */
 
        public ValueRecordsAggregate():
            this(INDEX_NOT_SET, INDEX_NOT_SET, new CellValueRecordInterface[30][]) // We start with 30 Rows.
        {
            
        }
        private ValueRecordsAggregate(int firstCellIx, int lastCellIx, CellValueRecordInterface[][] pRecords)
        {
            firstcell = firstCellIx;
            lastcell = lastCellIx;
            records = pRecords;
        }
 
        public void InsertCell(CellValueRecordInterface cell)
        {
            int column = cell.Column;
            int row = cell.Row;
            if (row >= records.Length)
            {
                CellValueRecordInterface[][] oldRecords = records;
                int newSize = oldRecords.Length * 2;
                if (newSize < row + 1) newSize = row + 1;
                records = new CellValueRecordInterface[newSize][];
                Array.Copy(oldRecords, 0, records, 0, oldRecords.Length);
            }
            object objRowCells = records[row];
            if (objRowCells == null)
            {
                int newSize = column + 1;
                if (newSize < 10) newSize = 10;
                objRowCells = new CellValueRecordInterface[newSize];
                records[row] = (CellValueRecordInterface[])objRowCells;
            }
            CellValueRecordInterface[] rowCells = (CellValueRecordInterface[])objRowCells;
            if (column >= rowCells.Length)
            {
                CellValueRecordInterface[] oldRowCells = rowCells;
                int newSize = oldRowCells.Length * 2;
                if (newSize < column + 1) newSize = column + 1;
                // if(newSize>257) newSize=257; // activate?
                rowCells = new CellValueRecordInterface[newSize];
                Array.Copy(oldRowCells, 0, rowCells, 0, oldRowCells.Length);
                records[row] = rowCells;
            }
            rowCells[column] = cell;
 
            if ((column < firstcell) || (firstcell == -1))
            {
                firstcell = column;
            }
            if ((column > lastcell) || (lastcell == -1))
            {
                lastcell = column;
            }
        }
 
        public void RemoveCell(CellValueRecordInterface cell)
        {
            if (cell == null)
            {
                throw new ArgumentException("cell must not be null");
            }
 
            int row = cell.Row;
            if (row >= records.Length)
            {
                throw new Exception("cell row is out of range"); 
            }
            CellValueRecordInterface[] rowCells = records[row];
            if (rowCells == null)
            {
                throw new Exception("cell row is already empty");
            }
            int column = cell.Column;
            if (column >= rowCells.Length)
            {
                throw new Exception("cell column is out of range");
            }
            rowCells[column] = null;
            
        }
        public void RemoveAllCellsValuesForRow(int rowIndex)
        {
            if (rowIndex < 0 || rowIndex > MAX_ROW_INDEX)
            {
                throw new ArgumentException("Specified rowIndex " + rowIndex
                        + " is outside the allowable range (0.." + MAX_ROW_INDEX + ")");
            }
            if (rowIndex >= records.Length)
            {
                // this can happen when the client code has created a row,
                // and then removes/replaces it before adding any cells. (see bug 46312)
                return;
            }
 
            records[rowIndex] = null;
        }
        public CellValueRecordInterface[] GetValueRecords()
        {
            ArrayList temp = new ArrayList();
 
            for (int i = 0; i < records.Length; i++)
            {
                CellValueRecordInterface[] rowCells = records[i];
                if (rowCells == null)
                {
                    continue;
                }
                for (int j = 0; j < rowCells.Length; j++)
                {
                    CellValueRecordInterface cell = rowCells[j];
                    if (cell != null)
                    {
                        temp.Add(cell);
                    }
                }
            }
             return (CellValueRecordInterface[])temp.ToArray(typeof(CellValueRecordInterface));
        }
        public int PhysicalNumberOfCells
        {
            get
            {
                int count = 0;
                for (int r = 0; r < records.Length; r++)
                {
                    CellValueRecordInterface[] rowCells = records[r];
                    if (rowCells != null)
                        for (short c = 0; c < rowCells.Length; c++)
                        {
                            if (rowCells[c] != null) count++;
                        }
                }
                return count;
            }
        }
 
        public int FirstCellNum
        {
            get
            {
                return firstcell;
            }
        }
 
        public int LastCellNum
        {
            get
            {
                return lastcell;
            }
        }
        public void AddMultipleBlanks(MulBlankRecord mbr)
        {
            for (int j = 0; j < mbr.NumColumns; j++)
            {
                BlankRecord br = new BlankRecord();
 
                br.Column = j + mbr.FirstColumn;
                br.Row = mbr.Row;
                br.XFIndex = (mbr.GetXFAt(j));
                InsertCell(br);
            }
        }
        private MulBlankRecord CreateMBR(CellValueRecordInterface[] cellValues, int startIx, int nBlank)
        {
 
            short[] xfs = new short[nBlank];
            for (int i = 0; i < xfs.Length; i++)
            {
                xfs[i] = ((BlankRecord)cellValues[startIx + i]).XFIndex;
            }
            int rowIx = cellValues[startIx].Row;
            return new MulBlankRecord(rowIx, startIx, xfs);
        }
 
        public void Construct(CellValueRecordInterface rec, RecordStream rs, SharedValueManager sfh)
        {
            if (rec is FormulaRecord)
            {
                FormulaRecord formulaRec = (FormulaRecord)rec;
                // read optional cached text value
                StringRecord cachedText=null;
                Type nextClass = rs.PeekNextClass();
                if (nextClass == typeof(StringRecord))
                {
                    cachedText = (StringRecord)rs.GetNext();
                }
                else
                {
                    cachedText = null;
                }
                InsertCell(new FormulaRecordAggregate(formulaRec, cachedText, sfh));
            }
            else
            {
                InsertCell(rec);
            }
        }
 
        /**
         * Sometimes the shared formula flag "seems" to be erroneously Set, in which case there is no 
         * call to <c>SharedFormulaRecord.ConvertSharedFormulaRecord</c> and hence the 
         * <c>ParsedExpression</c> field of this <c>FormulaRecord</c> will not Get updated.<br/>
         * As it turns out, this is not a problem, because in these circumstances, the existing value
         * for <c>ParsedExpression</c> is perfectly OK.<p/>
         * 
         * This method may also be used for Setting breakpoints to help diagnose Issues regarding the
         * abnormally-Set 'shared formula' flags. 
         * (see TestValueRecordsAggregate.testSpuriousSharedFormulaFlag()).<p/>
         * 
         * The method currently does nothing but do not delete it without Finding a nice home for this 
         * comment.
         */
        static void HandleMissingSharedFormulaRecord(FormulaRecord formula)
        {
            // could log an info message here since this is a fairly Unusual occurrence.
        }
 
 
        /** Tallies a count of the size of the cell records
         *  that are attached to the rows in the range specified.
         */
        public int GetRowCellBlockSize(int startRow, int endRow)
        {
            MyEnumerator itr = new MyEnumerator(ref records,startRow, endRow);
            int size = 0;
            while (itr.MoveNext())
            {
                CellValueRecordInterface cell = (CellValueRecordInterface)itr.Current;
                int row = cell.Row;
                if (row > endRow)
                    break;
                if ((row >= startRow) && (row <= endRow))
                    size += ((RecordBase)cell).RecordSize;
            }
            return size;
        }
 
        /** Returns true if the row has cells attached to it */
        public bool RowHasCells(int row)
        {
            if (row > records.Length - 1) //previously this said row > records.Length which means if 
                return false;  // if records.Length == 60 and I pass "60" here I Get array out of bounds
            CellValueRecordInterface[] rowCells = records[row]; //because a 60 Length array has the last index = 59
            if (rowCells == null) return false;
            for (int col = 0; col < rowCells.Length; col++)
            {
                if (rowCells[col] != null) return true;
            }
            return false;
        }
        public void UpdateFormulasAfterRowShift(FormulaShifter shifter, int currentExternSheetIndex)
        {
            for (int i = 0; i < records.Length; i++)
            {
                CellValueRecordInterface[] rowCells = records[i];
                if (rowCells == null)
                {
                    continue;
                }
                for (int j = 0; j < rowCells.Length; j++)
                {
                    CellValueRecordInterface cell = rowCells[j];
                    if (cell is FormulaRecordAggregate)
                    {
                        FormulaRecord fr = ((FormulaRecordAggregate)cell).FormulaRecord;
                        Ptg[] ptgs = fr.ParsedExpression; // needs clone() inside this getter?
                        if (shifter.AdjustFormula(ptgs, currentExternSheetIndex))
                        {
                            fr.ParsedExpression = (ptgs);
                        }
                    }
                }
            }
        }
 
        public void VisitCellsForRow(int rowIndex, RecordVisitor rv)
        {
 
            CellValueRecordInterface[] rowCells = records[rowIndex];
            if (rowCells == null)
            {
                throw new ArgumentException("Row [" + rowIndex + "] is empty");
            }
            for (int i = 0; i < rowCells.Length; i++)
            {
                RecordBase cvr = (RecordBase)rowCells[i];
                if (cvr == null)
                {
                    continue;
                }
                int nBlank = CountBlanks(rowCells, i);
                if (nBlank > 1)
                {
                    rv.VisitRecord(CreateMBR(rowCells, i, nBlank));
                    i += nBlank - 1;
                }
                else if (cvr is RecordAggregate)
                {
                    RecordAggregate agg = (RecordAggregate)cvr;
                    agg.VisitContainedRecords(rv);
                }
                else
                {
                    rv.VisitRecord((Record)cvr);
                }
            }
        }
        static int CountBlanks(CellValueRecordInterface[] rowCellValues, int startIx)
        {
            int i = startIx;
            while (i < rowCellValues.Length)
            {
                CellValueRecordInterface cvr = rowCellValues[i];
                if (!(cvr is BlankRecord))
                {
                    break;
                }
                i++;
            }
            return i - startIx;
        }
        /** Serializes the cells that are allocated to a certain row range*/
        public int SerializeCellRow(int row, int offset, byte[] data)
        {
            MyEnumerator itr = new MyEnumerator(ref records,row, row);
            int pos = offset;
 
            while (itr.MoveNext())
            {
                CellValueRecordInterface cell = (CellValueRecordInterface)itr.Current;
                if (cell.Row != row)
                    break;
                pos += ((RecordBase)cell).Serialize(pos, data);
            }
            return pos - offset;
        }
 
        public IEnumerator GetEnumerator()
        {
            return new MyEnumerator(ref records);
        }
 
        private class MyEnumerator : IEnumerator
        {
            short nextColumn = -1;
            int nextRow, lastRow;
 
            CellValueRecordInterface[][] records;
 
            public MyEnumerator(ref CellValueRecordInterface[][] _records)
            {
                this.records = _records;
                this.nextRow = 0;
                this.lastRow = _records.Length - 1;
                //FindNext();
            }
 
            public MyEnumerator(ref CellValueRecordInterface[][] _records,int firstRow, int lastRow)
            {
                this.records = _records;
                this.nextRow = firstRow;
                this.lastRow = lastRow;
                //FindNext();
            }
            public bool MoveNext()
            {
                
                FindNext();
                return nextRow <= lastRow; ;
            }
            public Object Current
            {
                get
                {
                    Object o = this.records[nextRow][nextColumn];
 
                    return o;
                }
            }
            public void Remove()
            {
                throw new InvalidOperationException("gibt's noch nicht");
            }
 
            private void FindNext()
            {
                nextColumn++;
                for (; nextRow <= lastRow; nextRow++)
                {
                    //previously this threw array out of bounds...
                    CellValueRecordInterface[] rowCells = (nextRow < records.Length) ? records[nextRow] : null;
                    if (rowCells == null)
                    { // This row is empty
                        nextColumn = 0;
                        continue;
                    }
                    for (; nextColumn < rowCells.Length; nextColumn++)
                    {
                        if (rowCells[nextColumn] != null) return;
                    }
                    nextColumn = 0;
                }
            }
            public void Reset()
            {
                nextColumn = -1;
                nextRow = 0;
            }
        }
    }
}