@@ -7,11 +7,7 @@ namespace BlazorDatasheet.Core.Commands.Data;
7
7
8
8
public class SetCellValuesCommand : BaseCommand , IUndoableCommand
9
9
{
10
- private readonly object [ ] [ ] ? _values ;
11
- private readonly CellValue [ ] [ ] ? _cellValues ;
12
- private readonly IRegion ? _region ;
13
- private readonly object ? _singleValue ;
14
- private readonly CellValue ? _singleCellValue ;
10
+ private readonly IEnumerable < IEnumerable < CellValue > > _cellValues ;
15
11
private readonly int _row ;
16
12
private readonly int _col ;
17
13
private readonly CellStoreRestoreData _restoreData = new ( ) ;
@@ -22,46 +18,37 @@ public class SetCellValuesCommand : BaseCommand, IUndoableCommand
22
18
/// <param name="values">The values to set. values[row] is the row offset from <paramref name="row"/>. Each values[row][col] is the col offset from <paramref name="col"/></param>
23
19
/// <param name="row"></param>
24
20
/// <param name="col"></param>
25
- public SetCellValuesCommand ( int row , int col , object [ ] [ ] values )
21
+ public SetCellValuesCommand ( int row , int col , CellValue [ ] [ ] values )
26
22
{
27
- _values = values ;
23
+ _cellValues = values ;
28
24
_row = row ;
29
25
_col = col ;
30
26
}
31
27
28
+
32
29
/// <summary>
33
30
/// Creates a command to set multiple cell values starting at position <paramref name="row"/>/<paramref name="col"/>
34
31
/// </summary>
35
- /// <param name="values">The values to set. values[row] is the row offset from <paramref name="row"/>. Each values[row][col] is the col offset from <paramref name="col"/></param>
36
32
/// <param name="row"></param>
37
33
/// <param name="col"></param>
38
- public SetCellValuesCommand ( int row , int col , CellValue [ ] [ ] values )
34
+ /// <param name="values"></param>
35
+ public SetCellValuesCommand ( int row , int col , IEnumerable < IEnumerable < CellValue > > values )
39
36
{
40
37
_cellValues = values ;
41
38
_row = row ;
42
39
_col = col ;
43
40
}
44
41
45
- /// <summary>
46
- /// Sets all the cells in the region <paramref name="region"/> to <paramref name="value"/>
47
- /// </summary>
48
- /// <param name="region"></param>
49
- /// <param name="value"></param>
50
- public SetCellValuesCommand ( IRegion region , object ? value )
51
- {
52
- _region = region ;
53
- _singleValue = value ;
54
- }
55
-
56
42
/// <summary>
57
43
/// Sets all the cells in the region <paramref name="region"/> to <paramref name="value"/>
58
44
/// </summary>
59
45
/// <param name="region"></param>
60
46
/// <param name="value"></param>
61
47
public SetCellValuesCommand ( IRegion region , CellValue value )
62
48
{
63
- _region = region ;
64
- _singleCellValue = value ;
49
+ _row = region . Top ;
50
+ _col = region . Left ;
51
+ _cellValues = Enumerable . Repeat ( Enumerable . Repeat ( value , region . Width ) , region . Height ) ;
65
52
}
66
53
67
54
public override bool CanExecute ( Sheet sheet ) => true ;
@@ -70,85 +57,39 @@ public override bool Execute(Sheet sheet)
70
57
{
71
58
sheet . ScreenUpdating = false ;
72
59
sheet . BatchUpdates ( ) ;
73
- if ( _values != null )
74
- ExecuteSetObjectArrayData ( sheet ) ;
75
- else if ( _cellValues != null )
76
- ExecuteSetCellValueData ( sheet ) ;
77
- else if ( _region != null && _singleCellValue != null )
78
- ExecuteSetRegionDataAsCellValue ( sheet ) ;
79
- else if ( _region != null )
80
- ExecuteSetRegionData ( sheet ) ;
81
- else
82
- return false ;
60
+ ExecuteSetCellValueData ( sheet ) ;
83
61
sheet . EndBatchUpdates ( ) ;
84
62
sheet . ScreenUpdating = true ;
85
63
86
64
return true ;
87
65
}
88
66
89
- private void ExecuteSetObjectArrayData ( Sheet sheet )
90
- {
91
- var rowEnd = _row + _values ! . Length ;
92
- var colEnd = _col ;
93
-
94
- for ( int i = 0 ; i < _values . Length ; i ++ )
95
- {
96
- for ( int j = 0 ; j < _values [ i ] . Length ; j ++ )
97
- {
98
- _restoreData . Merge ( sheet . Cells . SetValueImpl ( _row + i , _col + j , _values [ i ] [ j ] ) ) ;
99
- colEnd = Math . Max ( colEnd , j + _col ) ;
100
- }
101
- }
102
-
103
- sheet . MarkDirty ( new Region ( _row , rowEnd , _col , colEnd ) ) ;
104
- }
105
-
106
67
private void ExecuteSetCellValueData ( Sheet sheet )
107
68
{
108
- var rowEnd = _row + _cellValues ! . Length ;
69
+ var rowEnd = _row ;
109
70
var colEnd = _col ;
110
71
111
- for ( int i = 0 ; i < _cellValues . Length ; i ++ )
112
- {
113
- for ( int j = 0 ; j < _cellValues [ i ] . Length ; j ++ )
114
- {
115
- _restoreData . Merge ( sheet . Cells . SetValueImpl ( _row + i , _col + j , _cellValues [ i ] [ j ] ) ) ;
116
- colEnd = Math . Max ( colEnd , j + _col ) ;
117
- }
118
- }
72
+ int rowOffset = 0 ;
119
73
120
- sheet . MarkDirty ( new Region ( _row , rowEnd , _col , colEnd ) ) ;
121
- }
122
-
123
-
124
- private void ExecuteSetRegionData ( Sheet sheet )
125
- {
126
- for ( int row = _region ! . Top ; row <= _region ! . Bottom ; row ++ )
74
+ foreach ( var row in _cellValues )
127
75
{
128
- for ( int col = _region . Left ; col <= _region . Right ; col ++ )
76
+ int colOffset = 0 ;
77
+
78
+ foreach ( var value in row )
129
79
{
130
- _restoreData . Merge ( sheet . Cells . SetValueImpl ( row , col , _singleValue ) ) ;
80
+ _restoreData . Merge ( sheet . Cells . SetValueImpl ( _row + rowOffset , _col + colOffset , value ) ) ;
81
+ colEnd = Math . Max ( colEnd , _col + colOffset ) ;
82
+ colOffset ++ ;
131
83
}
132
- }
133
-
134
- sheet . MarkDirty ( _region ) ;
135
- }
136
-
137
- private void ExecuteSetRegionDataAsCellValue ( Sheet sheet )
138
- {
139
- sheet . ScreenUpdating = false ;
140
84
141
- for ( int row = _region ! . Top ; row <= _region ! . Bottom ; row ++ )
142
- {
143
- for ( int col = _region . Left ; col <= _region . Right ; col ++ )
144
- {
145
- _restoreData . Merge ( sheet . Cells . SetValueImpl ( row , col , _singleCellValue ! ) ) ;
146
- }
85
+ rowEnd = Math . Max ( rowEnd , _row + rowOffset ) ;
86
+ rowOffset ++ ;
147
87
}
148
88
149
- sheet . MarkDirty ( _region ) ;
89
+ sheet . MarkDirty ( new Region ( _row , rowEnd , _col , colEnd ) ) ;
150
90
}
151
91
92
+
152
93
public bool Undo ( Sheet sheet )
153
94
{
154
95
sheet . ScreenUpdating = false ;
0 commit comments