1
1
using System ;
2
2
using System . Collections . Generic ;
3
3
using ExcelDna . Integration ;
4
- using static ExcelDna . Integration . XlCall ;
5
4
6
5
namespace AsyncFunctions
7
6
{
@@ -79,20 +78,27 @@ public class ArrayResizer
79
78
// Needs extra protection to allow multithreaded use.
80
79
public static object dnaResize ( object [ , ] array )
81
80
{
82
- // For dynamic-array aware Excel we don't do anything
83
- if ( UtilityFunctions . dnaSupportsDynamicArrays ( ) )
84
- return array ;
85
-
86
- var caller = Excel ( xlfCaller ) as ExcelReference ;
81
+ var caller = XlCall . Excel ( XlCall . xlfCaller ) as ExcelReference ;
87
82
if ( caller == null )
83
+ {
88
84
return array ;
85
+ }
89
86
90
87
int rows = array . GetLength ( 0 ) ;
91
88
int columns = array . GetLength ( 1 ) ;
92
89
93
90
if ( rows == 0 || columns == 0 )
94
91
return array ;
95
92
93
+ // For dynamic-array aware Excel we don't do anything if the caller is a single cell
94
+ // Excel will expand in this case
95
+ if ( UtilityFunctions . dnaSupportsDynamicArrays ( ) &&
96
+ caller . RowFirst == caller . RowLast &&
97
+ caller . ColumnFirst == caller . ColumnLast )
98
+ {
99
+ return array ;
100
+ }
101
+
96
102
if ( ( caller . RowLast - caller . RowFirst + 1 == rows ) &&
97
103
( caller . ColumnLast - caller . ColumnFirst + 1 == columns ) )
98
104
{
@@ -125,19 +131,28 @@ public static object dnaResize(object[,] array)
125
131
126
132
public static double [ , ] dnaResizeDoubles ( double [ , ] array )
127
133
{
128
- // For dynamic-array aware Excel we don't do anything
129
- if ( UtilityFunctions . dnaSupportsDynamicArrays ( ) )
130
- return array ;
131
-
132
- var caller = Excel ( xlfCaller ) as ExcelReference ;
134
+ var caller = XlCall . Excel ( XlCall . xlfCaller ) as ExcelReference ;
133
135
if ( caller == null )
136
+ {
134
137
return array ;
138
+ }
135
139
136
140
int rows = array . GetLength ( 0 ) ;
137
141
int columns = array . GetLength ( 1 ) ;
138
142
139
143
if ( rows == 0 || columns == 0 )
144
+ {
140
145
return array ;
146
+ }
147
+
148
+ // For dynamic-array aware Excel we don't do anything if the caller is a single cell
149
+ // Excel will expand in this case
150
+ if ( UtilityFunctions . dnaSupportsDynamicArrays ( ) &&
151
+ caller . RowFirst == caller . RowLast &&
152
+ caller . ColumnFirst == caller . ColumnLast )
153
+ {
154
+ return array ;
155
+ }
141
156
142
157
if ( ( caller . RowLast - caller . RowFirst + 1 == rows ) &&
143
158
( caller . ColumnLast - caller . ColumnFirst + 1 == columns ) )
@@ -164,6 +179,7 @@ public static object dnaResize(object[,] array)
164
179
var target = new ExcelReference ( caller . RowFirst , rowLast , caller . ColumnFirst , columnLast , caller . SheetId ) ;
165
180
DoResize ( target ) ; // Will trigger a recalc by writing formula
166
181
} ) ;
182
+
167
183
// Return what we have - to prevent flashing #N/A
168
184
return array ;
169
185
}
@@ -177,31 +193,31 @@ static void DoResize(ExcelReference target)
177
193
ExcelReference firstCell = new ExcelReference ( target . RowFirst , target . RowFirst , target . ColumnFirst , target . ColumnFirst , target . SheetId ) ;
178
194
179
195
// Get the formula in the first cell of the target
180
- string formula = ( string ) Excel ( xlfGetCell , 41 , firstCell ) ;
181
- bool isFormulaArray = ( bool ) Excel ( xlfGetCell , 49 , firstCell ) ;
196
+ string formula = ( string ) XlCall . Excel ( XlCall . xlfGetCell , 41 , firstCell ) ;
197
+ bool isFormulaArray = ( bool ) XlCall . Excel ( XlCall . xlfGetCell , 49 , firstCell ) ;
182
198
if ( isFormulaArray )
183
199
{
184
200
// Select the sheet and firstCell - needed because we want to use SelectSpecial.
185
201
using ( new ExcelSelectionHelper ( firstCell ) )
186
202
{
187
203
// Extend the selection to the whole array and clear
188
- Excel ( xlcSelectSpecial , 6 ) ;
189
- ExcelReference oldArray = ( ExcelReference ) Excel ( xlfSelection ) ;
204
+ XlCall . Excel ( XlCall . xlcSelectSpecial , 6 ) ;
205
+ ExcelReference oldArray = ( ExcelReference ) XlCall . Excel ( XlCall . xlfSelection ) ;
190
206
191
207
oldArray . SetValue ( ExcelEmpty . Value ) ;
192
208
}
193
209
}
194
210
// Get the formula and convert to R1C1 mode
195
- bool isR1C1Mode = ( bool ) Excel ( xlfGetWorkspace , 4 ) ;
211
+ bool isR1C1Mode = ( bool ) XlCall . Excel ( XlCall . xlfGetWorkspace , 4 ) ;
196
212
string formulaR1C1 = formula ;
197
213
if ( ! isR1C1Mode )
198
214
{
199
215
object formulaR1C1Obj ;
200
- XlReturn formulaR1C1Return = TryExcel ( xlfFormulaConvert , out formulaR1C1Obj , formula , true , false , ExcelMissing . Value , firstCell ) ;
201
- if ( formulaR1C1Return != XlReturn . XlReturnSuccess || formulaR1C1Obj is ExcelError )
216
+ XlCall . XlReturn formulaR1C1Return = XlCall . TryExcel ( XlCall . xlfFormulaConvert , out formulaR1C1Obj , formula , true , false , ExcelMissing . Value , firstCell ) ;
217
+ if ( formulaR1C1Return != XlCall . XlReturn . XlReturnSuccess || formulaR1C1Obj is ExcelError )
202
218
{
203
- string firstCellAddress = ( string ) Excel ( xlfReftext , firstCell , true ) ;
204
- Excel ( xlcAlert , "Cannot resize array formula at " + firstCellAddress + " - formula might be too long when converted to R1C1 format." ) ;
219
+ string firstCellAddress = ( string ) XlCall . Excel ( XlCall . xlfReftext , firstCell , true ) ;
220
+ XlCall . Excel ( XlCall . xlcAlert , "Cannot resize array formula at " + firstCellAddress + " - formula might be too long when converted to R1C1 format." ) ;
205
221
firstCell . SetValue ( "'" + formula ) ;
206
222
return ;
207
223
}
@@ -210,15 +226,15 @@ static void DoResize(ExcelReference target)
210
226
// Must be R1C1-style references
211
227
object ignoredResult ;
212
228
//Debug.Print("Resizing START: " + target.RowLast);
213
- XlReturn formulaArrayReturn = TryExcel ( xlcFormulaArray , out ignoredResult , formulaR1C1 , target ) ;
229
+ XlCall . XlReturn formulaArrayReturn = XlCall . TryExcel ( XlCall . xlcFormulaArray , out ignoredResult , formulaR1C1 , target ) ;
214
230
//Debug.Print("Resizing FINISH");
215
231
216
232
// TODO: Find some dummy macro to clear the undo stack
217
233
218
- if ( formulaArrayReturn != XlReturn . XlReturnSuccess )
234
+ if ( formulaArrayReturn != XlCall . XlReturn . XlReturnSuccess )
219
235
{
220
- string firstCellAddress = ( string ) Excel ( xlfReftext , firstCell , true ) ;
221
- Excel ( xlcAlert , "Cannot resize array formula at " + firstCellAddress + " - result might overlap another array." ) ;
236
+ string firstCellAddress = ( string ) XlCall . Excel ( XlCall . xlfReftext , firstCell , true ) ;
237
+ XlCall . Excel ( XlCall . xlcAlert , "Cannot resize array formula at " + firstCellAddress + " - result might overlap another array." ) ;
222
238
// Might have failed due to array in the way.
223
239
firstCell . SetValue ( "'" + formula ) ;
224
240
}
@@ -234,13 +250,13 @@ public class ExcelEchoOffHelper : XlCall, IDisposable
234
250
235
251
public ExcelEchoOffHelper ( )
236
252
{
237
- oldEcho = Excel ( xlfGetWorkspace , 40 ) ;
238
- Excel ( xlcEcho , false ) ;
253
+ oldEcho = XlCall . Excel ( XlCall . xlfGetWorkspace , 40 ) ;
254
+ XlCall . Excel ( XlCall . xlcEcho , false ) ;
239
255
}
240
256
241
257
public void Dispose ( )
242
258
{
243
- Excel ( xlcEcho , oldEcho ) ;
259
+ XlCall . Excel ( XlCall . xlcEcho , oldEcho ) ;
244
260
}
245
261
}
246
262
@@ -250,13 +266,13 @@ public class ExcelCalculationManualHelper : XlCall, IDisposable
250
266
251
267
public ExcelCalculationManualHelper ( )
252
268
{
253
- oldCalculationMode = Excel ( xlfGetDocument , 14 ) ;
254
- Excel ( xlcOptionsCalculation , 3 ) ;
269
+ oldCalculationMode = XlCall . Excel ( XlCall . xlfGetDocument , 14 ) ;
270
+ XlCall . Excel ( XlCall . xlcOptionsCalculation , 3 ) ;
255
271
}
256
272
257
273
public void Dispose ( )
258
274
{
259
- Excel ( xlcOptionsCalculation , oldCalculationMode ) ;
275
+ XlCall . Excel ( XlCall . xlcOptionsCalculation , oldCalculationMode ) ;
260
276
}
261
277
}
262
278
@@ -274,32 +290,32 @@ public class ExcelSelectionHelper : XlCall, IDisposable
274
290
public ExcelSelectionHelper ( ExcelReference refToSelect )
275
291
{
276
292
// Remember old selection state on the active sheet
277
- oldSelectionOnActiveSheet = Excel ( xlfSelection ) ;
278
- oldActiveCellOnActiveSheet = Excel ( xlfActiveCell ) ;
293
+ oldSelectionOnActiveSheet = XlCall . Excel ( XlCall . xlfSelection ) ;
294
+ oldActiveCellOnActiveSheet = XlCall . Excel ( XlCall . xlfActiveCell ) ;
279
295
280
296
// Switch to the sheet we want to select
281
- string refSheet = ( string ) Excel ( xlSheetNm , refToSelect ) ;
282
- Excel ( xlcWorkbookSelect , new object [ ] { refSheet } ) ;
297
+ string refSheet = ( string ) XlCall . Excel ( XlCall . xlSheetNm , refToSelect ) ;
298
+ XlCall . Excel ( XlCall . xlcWorkbookSelect , new object [ ] { refSheet } ) ;
283
299
284
300
// record selection and active cell on the sheet we want to select
285
- oldSelectionOnRefSheet = Excel ( xlfSelection ) ;
286
- oldActiveCellOnRefSheet = Excel ( xlfActiveCell ) ;
301
+ oldSelectionOnRefSheet = XlCall . Excel ( XlCall . xlfSelection ) ;
302
+ oldActiveCellOnRefSheet = XlCall . Excel ( XlCall . xlfActiveCell ) ;
287
303
288
304
// make the selection
289
- Excel ( xlcFormulaGoto , refToSelect ) ;
305
+ XlCall . Excel ( XlCall . xlcFormulaGoto , refToSelect ) ;
290
306
}
291
307
292
308
public void Dispose ( )
293
309
{
294
310
// Reset the selection on the target sheet
295
- Excel ( xlcSelect , oldSelectionOnRefSheet , oldActiveCellOnRefSheet ) ;
311
+ XlCall . Excel ( XlCall . xlcSelect , oldSelectionOnRefSheet , oldActiveCellOnRefSheet ) ;
296
312
297
313
// Reset the sheet originally selected
298
- string oldActiveSheet = ( string ) Excel ( xlSheetNm , oldSelectionOnActiveSheet ) ;
299
- Excel ( xlcWorkbookSelect , new object [ ] { oldActiveSheet } ) ;
314
+ string oldActiveSheet = ( string ) XlCall . Excel ( XlCall . xlSheetNm , oldSelectionOnActiveSheet ) ;
315
+ XlCall . Excel ( XlCall . xlcWorkbookSelect , new object [ ] { oldActiveSheet } ) ;
300
316
301
317
// Reset the selection in the active sheet (some bugs make this change sometimes too)
302
- Excel ( xlcSelect , oldSelectionOnActiveSheet , oldActiveCellOnActiveSheet ) ;
318
+ XlCall . Excel ( XlCall . xlcSelect , oldSelectionOnActiveSheet , oldActiveCellOnActiveSheet ) ;
303
319
}
304
320
}
305
321
0 commit comments