Skip to content

Commit eed9321

Browse files
committed
Update AsyncResizer samples
1 parent d22c26e commit eed9321

File tree

4 files changed

+70
-52
lines changed

4 files changed

+70
-52
lines changed

ArrayResizer/CSharp/ArrayResizer.cs

+57-41
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System;
22
using System.Collections.Generic;
33
using ExcelDna.Integration;
4-
using static ExcelDna.Integration.XlCall;
54

65
namespace AsyncFunctions
76
{
@@ -79,20 +78,27 @@ public class ArrayResizer
7978
// Needs extra protection to allow multithreaded use.
8079
public static object dnaResize(object[,] array)
8180
{
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;
8782
if (caller == null)
83+
{
8884
return array;
85+
}
8986

9087
int rows = array.GetLength(0);
9188
int columns = array.GetLength(1);
9289

9390
if (rows == 0 || columns == 0)
9491
return array;
9592

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+
96102
if ((caller.RowLast - caller.RowFirst + 1 == rows) &&
97103
(caller.ColumnLast - caller.ColumnFirst + 1 == columns))
98104
{
@@ -125,19 +131,28 @@ public static object dnaResize(object[,] array)
125131

126132
public static double[,] dnaResizeDoubles(double[,] array)
127133
{
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;
133135
if (caller == null)
136+
{
134137
return array;
138+
}
135139

136140
int rows = array.GetLength(0);
137141
int columns = array.GetLength(1);
138142

139143
if (rows == 0 || columns == 0)
144+
{
140145
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+
}
141156

142157
if ((caller.RowLast - caller.RowFirst + 1 == rows) &&
143158
(caller.ColumnLast - caller.ColumnFirst + 1 == columns))
@@ -164,6 +179,7 @@ public static object dnaResize(object[,] array)
164179
var target = new ExcelReference(caller.RowFirst, rowLast, caller.ColumnFirst, columnLast, caller.SheetId);
165180
DoResize(target); // Will trigger a recalc by writing formula
166181
});
182+
167183
// Return what we have - to prevent flashing #N/A
168184
return array;
169185
}
@@ -177,31 +193,31 @@ static void DoResize(ExcelReference target)
177193
ExcelReference firstCell = new ExcelReference(target.RowFirst, target.RowFirst, target.ColumnFirst, target.ColumnFirst, target.SheetId);
178194

179195
// 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);
182198
if (isFormulaArray)
183199
{
184200
// Select the sheet and firstCell - needed because we want to use SelectSpecial.
185201
using (new ExcelSelectionHelper(firstCell))
186202
{
187203
// 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);
190206

191207
oldArray.SetValue(ExcelEmpty.Value);
192208
}
193209
}
194210
// Get the formula and convert to R1C1 mode
195-
bool isR1C1Mode = (bool)Excel(xlfGetWorkspace, 4);
211+
bool isR1C1Mode = (bool)XlCall.Excel(XlCall.xlfGetWorkspace, 4);
196212
string formulaR1C1 = formula;
197213
if (!isR1C1Mode)
198214
{
199215
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)
202218
{
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.");
205221
firstCell.SetValue("'" + formula);
206222
return;
207223
}
@@ -210,15 +226,15 @@ static void DoResize(ExcelReference target)
210226
// Must be R1C1-style references
211227
object ignoredResult;
212228
//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);
214230
//Debug.Print("Resizing FINISH");
215231

216232
// TODO: Find some dummy macro to clear the undo stack
217233

218-
if (formulaArrayReturn != XlReturn.XlReturnSuccess)
234+
if (formulaArrayReturn != XlCall.XlReturn.XlReturnSuccess)
219235
{
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.");
222238
// Might have failed due to array in the way.
223239
firstCell.SetValue("'" + formula);
224240
}
@@ -234,13 +250,13 @@ public class ExcelEchoOffHelper : XlCall, IDisposable
234250

235251
public ExcelEchoOffHelper()
236252
{
237-
oldEcho = Excel(xlfGetWorkspace, 40);
238-
Excel(xlcEcho, false);
253+
oldEcho = XlCall.Excel(XlCall.xlfGetWorkspace, 40);
254+
XlCall.Excel(XlCall.xlcEcho, false);
239255
}
240256

241257
public void Dispose()
242258
{
243-
Excel(xlcEcho, oldEcho);
259+
XlCall.Excel(XlCall.xlcEcho, oldEcho);
244260
}
245261
}
246262

@@ -250,13 +266,13 @@ public class ExcelCalculationManualHelper : XlCall, IDisposable
250266

251267
public ExcelCalculationManualHelper()
252268
{
253-
oldCalculationMode = Excel(xlfGetDocument, 14);
254-
Excel(xlcOptionsCalculation, 3);
269+
oldCalculationMode = XlCall.Excel(XlCall.xlfGetDocument, 14);
270+
XlCall.Excel(XlCall.xlcOptionsCalculation, 3);
255271
}
256272

257273
public void Dispose()
258274
{
259-
Excel(xlcOptionsCalculation, oldCalculationMode);
275+
XlCall.Excel(XlCall.xlcOptionsCalculation, oldCalculationMode);
260276
}
261277
}
262278

@@ -274,32 +290,32 @@ public class ExcelSelectionHelper : XlCall, IDisposable
274290
public ExcelSelectionHelper(ExcelReference refToSelect)
275291
{
276292
// 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);
279295

280296
// 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 });
283299

284300
// 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);
287303

288304
// make the selection
289-
Excel(xlcFormulaGoto, refToSelect);
305+
XlCall.Excel(XlCall.xlcFormulaGoto, refToSelect);
290306
}
291307

292308
public void Dispose()
293309
{
294310
// Reset the selection on the target sheet
295-
Excel(xlcSelect, oldSelectionOnRefSheet, oldActiveCellOnRefSheet);
311+
XlCall.Excel(XlCall.xlcSelect, oldSelectionOnRefSheet, oldActiveCellOnRefSheet);
296312

297313
// 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 });
300316

301317
// 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);
303319
}
304320
}
305321

ArrayResizer/CSharp/ArrayResizer.dna

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<DnaLibrary RuntimeVersion="v4.0">
22
<Project Language="C#" >
3-
<SourceItem Name="ArrayResizer.cs" />
3+
<SourceItem Path="ArrayResizer.cs" />
44
</Project>
55
</DnaLibrary>
+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<DnaLibrary RuntimeVersion="v4.0">
22
<Project Language="VB" >
3-
<SourceItem Name="ArrayResizer.vb" />
3+
<SourceItem Path="ArrayResizer.vb" />
44
</Project>
55
</DnaLibrary>

ArrayResizer/VisualBasic/ArrayResizer.vb

+11-9
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,6 @@ Namespace AsyncFunctions
5555
' Needs extra protection to allow multithreaded use.
5656
Public Function dnaResize(array As Object(,)) As Object
5757

58-
' Nothing to do if Excel already supports Dynamic Arrays
59-
If UtilityFunctions.dnaSupportsDynamicArrays() Then
60-
Return array
61-
End If
6258

6359
Dim caller As ExcelReference = TryCast(Excel(xlfCaller), ExcelReference)
6460
If caller Is Nothing Then
@@ -72,6 +68,11 @@ Namespace AsyncFunctions
7268
Return array
7369
End If
7470

71+
' Nothing to do if Excel already supports Dynamic Arrays, and we get a single cell caller
72+
If UtilityFunctions.dnaSupportsDynamicArrays() AndAlso caller.RowFirst = caller.RowLast AndAlso caller.ColumnFirst = caller.ColumnLast Then
73+
Return array
74+
End If
75+
7576
If (caller.RowLast - caller.RowFirst + 1 = rows) AndAlso (caller.ColumnLast - caller.ColumnFirst + 1 = columns) Then
7677
' Size is already OK - just return result
7778
Return array
@@ -219,15 +220,16 @@ Namespace AsyncFunctions
219220
Public Function dnaSupportsDynamicArrays() As Boolean
220221
Static supportsDynamicArrays? As Boolean
221222

222-
If Not _supportsDynamicArrays.HasValue Then
223+
If Not supportsDynamicArrays.HasValue Then
223224
Try
224225
Dim result = XlCall.Excel(614, New Object() {1}, New Object() {True})
225-
_supportsDynamicArrays = True
226+
supportsDynamicArrays = True
226227

227228
Catch
228-
_supportsDynamicArrays = False
229+
supportsDynamicArrays = False
229230
End Try
230231
End If
231-
Return _supportsDynamicArrays.Value
232-
End Module
232+
Return supportsDynamicArrays.Value
233+
End Function
234+
End Module
233235
End Namespace

0 commit comments

Comments
 (0)