/
RangeHelper.cs
1047 lines (866 loc) · 26.1 KB
/
RangeHelper.cs
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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System ;
using System.Collections ;
using System.Collections.Generic ;
using System.Globalization ;
using System.IO ;
using System.Reflection ;
using System.Text ;
// using Microsoft.Office.Interop.Excel ;
using Spire.Xls ;
using Range=Spire.Xls.CellRange ;
using System.Data ;
namespace AFC.WorkStation.ExcelReport
{
public class RangeHelper
{
public static int getColValueCount = 0 ;
public static object GetColValue(DataTable table, int row, string colName)
{
getColValueCount ++ ;
// return null ;
// int colIndex ;
int colIndex = table.Columns.IndexOf (colName) ;
if (string.IsNullOrEmpty(colName) || colIndex < 0)
return null;
return table.Rows[row][colIndex];
}
public static object GetColValue(DataTable table, int row, int colIndex)
{
getColValueByIndexCount++;
// return null ;
// int colIndex ;
// int colIndex = table.Columns.IndexOf(colName);
if (/*string.IsNullOrEmpty(colName) || */colIndex < 0)
return null;
return table.Rows[row][colIndex];
}
public static object MakeCellName (int column, int row)
{
column -= 1 ;
string value = GetExcelColIndex (column) ;
return value + row ;
}
private static string GetExcelColIndex (int column)
{
string value = "" ;
int deg = 0 ;
do
{
value = "" + (char)((column % 26 - (deg > 0 ? 1 : 0)) + 'A') + value ;
deg ++ ;
column = column/26 ;
} while (column > 0) ;
return value ;
}
public static int getRangeCallTimes = 0 ;
public static Range GetRange (Worksheet sheet, int startColumn, int startRow, int columns, int rows)
{
getRangeCallTimes ++ ;
return sheet [startRow, startColumn, startRow + rows - 1, startColumn + columns - 1] ;
/*return sheet.get_Range (MakeCellName (startColumn, startRow),
MakeCellName (startColumn + columns - 1, startRow + rows - 1)) ;*/
}
public static Range GetLine (Worksheet sheet, int startColumn, int startRow)
{
return sheet [startRow, startColumn].EntireRow ;
// return sheet.get_Range (MakeCellName (startColumn, startRow), Missing.Value).EntireRow ;
}
public static Range GetCell (Worksheet sheet, int startColumn, int startRow)
{
return GetRange (sheet, startColumn, startRow, 1, 1) ;
// return sheet.get_Range (MakeCellName (startColumn, startRow), Missing.Value) ;
}
public static Range InsertCopyRange (Worksheet sheet, Range orign, int columns, int rows, int targetColumn,
int targetRow, XlInsertShiftDirection direction)
{
return InsertCopyRange (sheet, orign, columns, rows, targetColumn,
targetRow, direction, 1) ;
}
public static int insertCopyRangeCallTimes = 0 ;
public static Range InsertCopyRange (Worksheet sheet, Range orign, int columns, int rows, int targetColumn,
int targetRow, XlInsertShiftDirection direction,
int lastColCount)
{
insertCopyRangeCallTimes++;
// return GetRange(sheet, targetColumn, targetRow, columns, rows);
int orgColumn = orign.Column ;
int orgRow = orign.Row ;
Range target = GetRange (sheet, targetColumn, targetRow, columns, rows) ;
if (direction == XlInsertShiftDirection.xlShiftToRight)
{
// insert blank
// target.Insert (direction, Missing.Value) ;
// Move target from origin to Right first
Range movedRange = GetRange(sheet, targetColumn, targetRow, lastColCount, rows);
Range movedNextRange = GetRange(sheet, targetColumn + columns, targetRow, lastColCount, rows);
movedRange.Move(movedNextRange, true, false);
target = GetRange (sheet, targetColumn, targetRow, columns, rows) ;
target.UnMerge ();
}
orign.Copy (target, false, true) ;
switch(direction)
{
case XlInsertShiftDirection.xlShiftDown:
// copy row height ;
for (int i = 0 ; i < rows ; i ++)
{
Range sRow = GetEntireRow(sheet, i + orgRow);
Range tRow = GetEntireRow(sheet, i + targetRow);
try
{
tRow.RowHeight = sRow.RowHeight ;
}
catch (Exception e)
{
Console.WriteLine ("Set RowHeight Error: " + e);
}
}
break ;
case XlInsertShiftDirection.xlShiftToRight:
// copy col width ;
for (int i = 0; i < columns; i++)
{
Range sCol = GetEntireCol(sheet, i + orgColumn);
Range tCol = GetEntireCol(sheet, i + targetColumn + 1);
try
{
tCol.ColumnWidth = sCol.ColumnWidth;
}catch (Exception e)
{
Console.WriteLine("Set ColumnWidth Error: " + e);
}
}
break ;
}
return target ;
}
public static Range GetEntireRow (Worksheet sheet, int rowIndex)
{
return sheet [rowIndex, 1].EntireRow ;
// return sheet.get_Range ("A"+rowIndex.ToString (), Missing.Value).EntireRow ;
}
public static Range GetEntireCol (Worksheet sheet, int colIndex)
{
return sheet[1, colIndex].EntireColumn;
// return sheet.get_Range(GetExcelColIndex (colIndex - 1)+"1", Missing.Value).EntireColumn;
}
public static IEnumerator GetRangeCells(Range range)
{
Type t = range.GetType();
PropertyInfo pCells = t.BaseType.GetProperty("Cells");
Array cells = (Array)pCells.GetValue(range, new object[0]);
return cells.GetEnumerator();
}
public static int MergeRanges (Range currentCellRange, MergeOption mOption)
{
Worksheet sheet = currentCellRange.Worksheet;
Range range = null;
// clear value first.
currentCellRange.Value2 = null;
currentCellRange.Text = "";
switch (mOption)
{
case MergeOption.Up:
// Select previous range.
range = GetRange(sheet, currentCellRange.Column, currentCellRange.Row - 1, 1, 1) ;
if (range.HasMerged)
{
Range marea = range.MergeArea ;
int mWidth = currentCellRange.Column - marea.Column + 1 ;
int mHeight = currentCellRange.Row - marea.Row + 1 ;
/*if (mWidth <= marea.ColumnCount && mHeight <= marea.RowCount)
{
Console.WriteLine ("Has be merged. ignore");
return 1 ;
}*/
if (mWidth < marea.ColumnCount)
mWidth = marea.ColumnCount ;
if (mHeight < marea.RowCount)
mHeight = marea.RowCount ;
range = GetRange(sheet,
marea.Column, marea.Row,
mWidth ,
mHeight) ;
/*
Console.WriteLine("Up cell has been merged: [" +
marea.Column + ", " + marea.Row + "], " +
(currentCellRange.Column - marea.Column + 1) + ", " +
(currentCellRange.Row - marea.Row + 1) + ", [" +
marea.ColumnCount + "," + marea.RowCount + "]");
*/
range.Merge () ;
}
else
{
range = GetRange(sheet,
currentCellRange.Column,
currentCellRange.Row - 1,
1, 2);
range.Merge();
}
return 1;
case MergeOption.Left:
// merge with Left cell.
/*
Console.WriteLine("Merge Left: get left cell first: [" +
(currentCellRange.Column ) +
", " + currentCellRange.Row + "].") ;
*/
range = GetRange (sheet, currentCellRange.Column - 1, currentCellRange.Row, 1, 1) ;
if (range.HasMerged)
{
Range marea = range.MergeArea;
int mWidth = currentCellRange.Column - marea.Column + 1;
int mHeight = currentCellRange.Row - marea.Row + 1;
if (mWidth < marea.ColumnCount)
mWidth = marea.ColumnCount;
if (mHeight < marea.RowCount)
mHeight = marea.RowCount;
range = GetRange(sheet,
marea.Column, marea.Row,
mWidth,
mHeight);
/*
Console.WriteLine ("Left cell has been merged: [" +
marea.Column + ", " + marea.Row + "], " +
(currentCellRange.Column - marea.Column + 1) + ", " +
(currentCellRange.Row - marea.Row + 1) + ", [" +
marea.ColumnCount + "," + marea.RowCount + "]");
*/
range.Merge () ;
}
else
{
range = GetRange(sheet,
currentCellRange.Column - 1,
currentCellRange.Row,
2, 1);
range.Merge () ;
}
// range.Merge ();
return 1;
case MergeOption.never:
default:
return 0;
}
}
public const string FORMAT_DATE = "date";
public const string FORMAT_MONTH = "month";
public const string FORMAT_DATE_TIME = "datetime";
public const string FORMAT_TIME = "time";
public const string FORMAT_FEN = "fen";
public const string FORMAT_NUMBER = "num";
public const string FORMAT_TIME_SPAN = "span";
static int UpdateCellValueCallTimes = 0 ;
public static int getColValueByIndexCount = 0 ;
public static void UpdateCellValue (ReportSheetTemplate tpl, CellRange cell, object value, string format)
{
UpdateCellValueCallTimes ++ ;
// return ;
value = FormatValue (tpl, value, format) ;
try
{
switch(format)
{
case FORMAT_DATE:
case FORMAT_MONTH:
case FORMAT_DATE_TIME:
case FORMAT_TIME:
case FORMAT_TIME_SPAN :
// cell.DateTimeValue = (DateTime) value ;
// return ;
break ;
case FORMAT_FEN:
case FORMAT_NUMBER:
try
{
cell.NumberValue = Convert.ToDouble(value) ;
}
catch (Exception e)
{
// ignore exception.
cell.NumberValue = 0;
}
return ;
default :
break ;
}
}
catch (Exception e)
{
// ignore exception.
}
cell.Text = Convert.ToString(value);
}
public static object FormatValue(ReportSheetTemplate tpl, object value, string format)
{
if (string.IsNullOrEmpty(format) || value == null)
return value;
switch (format)
{
case FORMAT_DATE:
return ValueToDate(value);
case FORMAT_MONTH:
return ValueToMonth(value);
case FORMAT_TIME:
return ValueToTime(value);
case FORMAT_DATE_TIME:
return ValueToDateTime(value);
case FORMAT_TIME_SPAN :
return ValueToTimeSpan (value, tpl) ;
case FORMAT_FEN:
return ValueToFen(value);
default:
return value;
}
}
private static object ValueToTimeSpan (object value, ReportSheetTemplate tpl)
{
int spanNumber ;
object span ;
DateTime time ;
if (value == null ||
! tpl.paramMap.TryGetValue ("time_span", out span) ||
! int.TryParse(span.ToString (), out spanNumber) ||
! DateTime.TryParseExact (value.ToString (), "yyyyMMddHHmmss",
null, DateTimeStyles.None, out time))
return ValueToTime (value) ;
DateTime time2 = time.Add (new TimeSpan (0, 0, spanNumber, 0)) ;
return time.ToString ("HH:mm") + "-" + time2.ToString ("HH:mm") ;
}
private static object ValueToMonth (object value)
{
string str = value.ToString();
if (str.Length < 6)
return value;
if (str.Length > 6)
str = str.Substring(0, 6);
try
{
return DateTime.ParseExact(str, "yyyyMM", null).ToString("yyyyÄêMMÔÂ"); // .ToString();
}
catch (Exception e)
{
Console.WriteLine("parse Month Error [" + str + "]: " + e);
return value;
}
}
private static object ValueToFen(object value)
{
try
{
double d = Convert.ToDouble(value);
return d / 100;
}
catch (Exception e)
{
Console.WriteLine("parse double Error [" + value + "]: " + e);
return value;
}
}
private static object ValueToDateTime(object value)
{
string str = value.ToString();
if (str.Length < 14)
return value;
if (str.Length > 14)
str = str.Substring(0, 14);
try
{
return DateTime.ParseExact(str, "yyyyMMddHHmmss", null).ToString("yyyyÄêMMÔÂddÈÕ HH:mm:ss"); // .ToString();
}
catch (Exception e)
{
Console.WriteLine("parse Datetime Error [" + str + "]: " + e);
return value;
}
}
private static object ValueToTime(object value)
{
string str = value.ToString();
if (str.Length < 6)
return value;
if (str.Length > 6)
str = str.Substring(str.Length - 6);
try
{
return DateTime.ParseExact(str, "HHmmss", null).ToString ("HH:mm:ss") ; // .ToShortTimeString();
}
catch (Exception e)
{
Console.WriteLine("parse Time Error [" + str + "]: " + e);
return value;
}
}
private static object ValueToDate(object value)
{
string str = value.ToString();
if (str.Length < 8)
return value;
if (str.Length > 8)
str = str.Substring(0, 8);
try
{
return DateTime.ParseExact (str, "yyyyMMdd", null).ToString ("yyyyÄêMMÔÂddÈÕ") ;
}
catch (Exception e)
{
Console.WriteLine("parse Date Error [" + str + "]: " + e);
return value;
}
}
public static object GetColValueByIndex (DataTable table, int rowIndex, int colIndex)
{
getColValueByIndexCount++;
// return null ;
// int colIndex ;
return table.Rows[rowIndex][colIndex];
}
}
/*
* Line template A,1
* repeat from A, 10
* cell (A,1), (B,1), (C,1), (D1)
*
* repeat Cell : (A , 10 + repeatCount)
* Merge count : = 2 ;
* Merge Area : (A, 10 + repeatCount
*/
public class GroupDataHolder
{
public Dictionary<GroupValueSearchKey, object> valueList = new Dictionary<GroupValueSearchKey, object> (640*1024);
public object GetValue (GroupValueSearchKey gkey, object defaultValue)
{
object value ;
if (valueList.TryGetValue (gkey, out value))
return value ;
else
return defaultValue ;
}
public void AddValue (Dictionary<GroupValueSearchKey, bool> map, GroupValueSearchKey gkey,
System.Data.DataTable table, int valueIndex)
{
if (map.ContainsKey(gkey))
// ignored.
return ;
// judge Data is Match
if (gkey.key != null)
{
SearchKey tmpKey = gkey.key ;
while(tmpKey != null)
{
if (tmpKey.rKey == null)
{
tmpKey.rKey = ReusedKey.FindReusedKey (tmpKey) ;
}
if (tmpKey.isFixedValue)
{
// Compare Fixed value Key only
object keyValue = null ;
keyValue = tmpKey.rKey.GetReusedValue (table, valueIndex) ;
// RangeHelper.GetColValue (table, valueIndex, tmpKey.colName) ;
// Not match, return .
if (! Equals (keyValue, tmpKey.keyValue))
return ;
}
tmpKey = tmpKey.nextKey ;
}
/*SearchKey copyKey = gkey.key.Copy0 (false) ;
copyKey.FillKey (table, valueIndex);
// not match.
if (! copyKey.Equals (gkey.key))
return ;*/
}
object lastValue ;
if (!valueList.TryGetValue(gkey, out lastValue))
{
lastValue = 0 ;
valueList.Add(gkey, lastValue);
}
if (gkey.rKey == null)
{
SearchKey key = new SearchKey ();
key.colName = gkey.valueColName ;
gkey.rKey = ReusedKey.FindReusedKey(key);
}
valueList [gkey] = CalculateForumla (gkey.formula, lastValue,
gkey.rKey.GetReusedValue (table, valueIndex)) ;
map[gkey/*.Copy()*/] = true;
}
public object CalculateForumla(string formulaName, object lastValue, object value)
{
formulaName = formulaName.ToLower();
lastValue = CellForumla.Calculate(formulaName, lastValue, value);
return lastValue;
}
private static SearchKey CreateTopKey (SearchKey key)
{
SearchKey topKey = key.Copy () ;
topKey.keyValue = "" ;
topKey.colName = "" ;
topKey.nextKey = key ;
topKey.isFixedValue = true ;
topKey.keyValue = null ;
return topKey ;
}
/*public void AddValue0(bool searchInChildren, SearchKey key, string valueColName, string formulaName, object value)
{
if (! searchInChildren)
{
GroupValue gValue ;
if (! valueList.TryGetValue (valueColName, out gValue))
{
// not found, Add New Value.
gValue = new GroupValue ();
gValue.colName = valueColName ;
valueList.Add (valueColName, gValue);
}
gValue.CalculateForumla(formulaName, value);
key = key.nextKey ;
}
if (key != null)
{
// search next key
GroupDataHolder nextDataHolder ;
if (! children.TryGetValue (key, out nextDataHolder))
{
// create new holder
nextDataHolder = new GroupDataHolder ();
nextDataHolder.colIndex = key.colIndex ;
nextDataHolder.colName = key.colName ;
nextDataHolder.keyValue = key.keyValue ;
children.Add (key, nextDataHolder);
}
nextDataHolder.AddValue0(false, key, valueColName, formulaName, value);
}
}*/
}
public class SearchKey : KeyValuePair
{
public bool isFixedValue = false ;
public SearchKey nextKey = null ;
public ReusedKey rKey = null ;
public void FillKey (System.Data.DataTable table, int rowIndex)
{
if (! isFixedValue)
{
// use context key value ;
if (string.IsNullOrEmpty(colName))
keyValue = null ;
if (rKey == null)
rKey = ReusedKey.FindReusedKey (this) ;
keyValue = rKey.GetReusedValue (table, rowIndex) ;
// RangeHelper.GetColValue (table, rowIndex, colName) ;
}
if (nextKey != null)
nextKey.FillKey (table, rowIndex);
}
public SearchKey Copy ()
{
return Copy0 (true) ;
}
public SearchKey Copy0(bool copyValue)
{
SearchKey key = new SearchKey();
key.colIndex = colIndex;
key.colName = colName;
key.rKey = rKey ;
if (copyValue)
{
key.keyValue = keyValue;
key.isFixedValue = isFixedValue;
}
if (nextKey != null)
key.nextKey = nextKey.Copy0(copyValue);
return key;
}
public override int GetHashCode ()
{
return base.GetHashCode () + 29*(nextKey != null ? nextKey.GetHashCode () : 0) ;
}
public override bool Equals (object obj)
{
if (this == obj) return true ;
SearchKey searchKey = obj as SearchKey ;
if (searchKey == null) return false ;
if (!base.Equals (obj)) return false ;
if (!Equals (nextKey, searchKey.nextKey)) return false ;
return true ;
}
public override string ToString ()
{
return "SearchKey: [" + colName + ", " + keyValue + ", " + isFixedValue + "]" +
"next->" + nextKey ;
}
}
/*public class GroupValue : KeyValuePair
{
private Dictionary<string, object> fValueList = new Dictionary<string, object>();
public object GetFormulaValue (string formula)
{
object value ;
if (fValueList.TryGetValue (formula, out value))
return value ;
throw new ArgumentOutOfRangeException ("formula", formula, "Formula Value not found in valueList.") ;
}
public object CalculateForumla (string formulaName, object value)
{
object lastValue ;
formulaName = formulaName.ToLower () ;
if (! fValueList.TryGetValue (formulaName, out lastValue))
{
lastValue = 0 ;
fValueList.Add (formulaName, lastValue);
}
lastValue = CellForumla.Calculate(formulaName, lastValue, value);
fValueList [formulaName] = lastValue ;
return lastValue ;
}
}
*/
public class KeyValuePair
{
public const string COL_NAME_NONE = "";
public const int COL_INDEX_NONE = -1;
public int colIndex;
public string colName;
public object keyValue;
public override int GetHashCode ()
{
return (colName != null ? colName.GetHashCode () : 0) + 29*(keyValue != null ? keyValue.GetHashCode () : 0) ;
}
public override bool Equals (object obj)
{
if (this == obj) return true ;
KeyValuePair keyValuePair = obj as KeyValuePair ;
if (keyValuePair == null) return false ;
if (!Equals (colName, keyValuePair.colName)) return false ;
if (!Equals (keyValue, keyValuePair.keyValue)) return false ;
return true ;
}
public static bool CompareValue (object v1, object v2)
{
if (Equals (v1, v2))
return true ;
return string.Equals ("" + v1, "" + v2) ;
}
}
public class ReusedKey : KeyValuePair
{
public DataTable lastUsedTable = null ;
public int lastUsedColIndex = -1 ;
public int lastUsedRowIndex = -1 ;
public object lastValue ;
public static int getReusedValueCallCount = 0 ;
public static int columnNameFindCount = 0 ;
public static int valueReusedCount = 0 ;
public static List<ReusedKey> keyPool = new List<ReusedKey> ();
public object GetReusedValue (DataTable table, int rowIndex)
{
getReusedValueCallCount ++ ;
if (lastUsedTable != table)
{
lastUsedTable = table ;
columnNameFindCount ++ ;
lastUsedColIndex = table.Columns.IndexOf (colName) ;
lastUsedRowIndex = -1 ;
lastValue = null ;
}
else
if (lastUsedRowIndex == rowIndex)
{
valueReusedCount ++ ;
return lastValue ;
}
lastUsedRowIndex = rowIndex ;
/*
if (lastUsedColIndex < 0)
lastValue = null ;
else
lastValue = table.Rows[rowIndex][lastUsedColIndex];
*/
lastValue = RangeHelper.GetColValue (table, rowIndex, lastUsedColIndex) ;
return lastValue ;
}
public static ReusedKey FindReusedKey (SearchKey key)
{
for (int i = 0; i < keyPool.Count; i++)
{
ReusedKey reusedKey = keyPool [i] ;
if (Equals (reusedKey.colName, key.colName))
return reusedKey ;
}
ReusedKey rKey = new ReusedKey () ;
rKey.colName = key.colName ;
keyPool.Add (rKey);
return rKey ;
}
}
public class GroupValueSearchKey
{
public SearchKey key ;
public string valueColName ;
public string formula ;
public ReusedKey rKey ;
public GroupValueSearchKey Copy ()
{
GroupValueSearchKey gkey = new GroupValueSearchKey ();
if (key != null)
gkey.key = key.Copy () ;
gkey.formula = formula ;
gkey.valueColName = valueColName;
gkey.rKey = rKey ;
return gkey;
}
public override int GetHashCode ()
{
int result = key != null ? key.GetHashCode () : 0 ;
result = 29*result + (valueColName != null ? valueColName.GetHashCode () : 0) ;
result = 29*result + (formula != null ? formula.GetHashCode () : 0) ;
return result ;
}
public override bool Equals (object obj)
{
if (this == obj) return true ;
GroupValueSearchKey groupValueSearchKey = obj as GroupValueSearchKey ;
if (groupValueSearchKey == null) return false ;
if (!Equals (key, groupValueSearchKey.key)) return false ;
if (!Equals (valueColName, groupValueSearchKey.valueColName)) return false ;
if (!Equals (formula, groupValueSearchKey.formula)) return false ;
return true ;
}
public override string ToString ()
{
return "GroupValueSearchKey [" + formula + ", " + valueColName + "]" +
key ;
}
}
public class CellForumla
{
public string formulaName = string.Empty;
public List<GroupValueSearchKey> keyList = new List<GroupValueSearchKey> ();
public object GetValue (GroupDataHolder holder, System.Data.DataTable table, int rowIndex)
{
object lastValue = 0 ;
for (int i = 0; i < keyList.Count; i++)
{
GroupValueSearchKey gkey = keyList [i] ;
if (gkey.key != null)
gkey.key.FillKey (table, rowIndex);
object value = holder.GetValue (gkey, null) ;
/*
Console.WriteLine ("row ["+ rowIndex + "] Get value for gkey: " + gkey );
Console.WriteLine ("row [" + rowIndex + "] = [" + value + "]") ;
*/
lastValue = Calculate (formulaName, lastValue, value) ;
}
return lastValue ;
}
public static object Calculate (string formulaName, object lastValue, object value)
{
switch (formulaName)
{
case "" :
return value ;
case "sum":
lastValue = ToNumber(lastValue) + ToNumber(value);
break;
case "count":
lastValue = ToNumber(lastValue) + 1;
break;
default:
throw new ArgumentOutOfRangeException("formula", formulaName, "Formula [" + formulaName + "] is not supported.");
}
return lastValue ;
}
private static double ToNumber(object value)
{
if (value == null)
return 0D;
string svalue = value.ToString();
double d = 0;
double.TryParse(svalue, out d);
return d;
}
public CellForumla Copy ()
{
CellForumla f = new CellForumla ();
f.formulaName = formulaName ;
for (int i = 0; i < keyList.Count; i++)
{
GroupValueSearchKey gkey = keyList [i] ;
f.keyList.Add (gkey.Copy ());
}
return f ;
}
public override string ToString ()
{
StringBuilder buf = new StringBuilder ();
buf.Append ("CellForumla [" + formulaName + "] keyCount = " + keyList.Count + "\n") ;
for (int i = 0; i < keyList.Count; i++)
{
GroupValueSearchKey key = keyList [i] ;
buf.Append ("\t") ;
buf.Append (key.ToString ()) ;
buf.Append ("\n") ;
}
return buf.ToString () ;
}
}
//provide empty table
public class EmptyTable
{
//provide a empty table with 1 empty row by default
public static DataTable GetEmptyTalbe(DataTable sourceTable)
{
return GetEmptyTalbe(sourceTable, 1);
}
//provide a empty table with custum number of enmpty rows
public static DataTable GetEmptyTalbe(DataTable sourceTable,int rowsCount)
{
if (rowsCount <= 0)
return sourceTable;
//DataRow row = sourceTable.NewRow();
//row[0] = DBNull.Value;
for (int i = 0; i < rowsCount;i++ )
{