-
-
Notifications
You must be signed in to change notification settings - Fork 294
Expand file tree
/
Copy pathArrayResizer.dna
More file actions
272 lines (229 loc) · 9.45 KB
/
ArrayResizer.dna
File metadata and controls
272 lines (229 loc) · 9.45 KB
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
<DnaLibrary RuntimeVersion="v4.0" Language="CS">
<![CDATA[
using System;
using System.Collections.Generic;
using ExcelDna.Integration;
namespace AsyncFunctions
{
// This class defines a few test functions that can be used to explore the automatic array resizing.
public static class ResizeTestFunctions
{
// Just returns an array of the given size
public static object[,] MakeArray(int rows, int columns)
{
object[,] result = new object [rows, columns];
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
result[i,j] = i + j;
}
}
return result;
}
public static double[,] MakeArrayDoubles(int rows, int columns)
{
double[,] result = new double[rows, columns];
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
result[i,j] = i + (j/1000.0);
}
}
return result;
}
public static object MakeMixedArrayAndResize(int rows, int columns)
{
object[,] result = new object [rows, columns];
for (int j = 0; j < columns; j++)
{
result[0,j] = "Col " + j;
}
for (int i = 1; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
result[i,j] = i + (j * 0.1);
}
}
return ArrayResizer.Resize(result);
}
// Makes an array, but automatically resizes the result
public static object MakeArrayAndResize(int rows, int columns, string unused, string unusedtoo)
{
object[,] result = MakeArray(rows, columns);
return ArrayResizer.Resize(result);
// Can also call Resize via Excel - so if the Resize add-in is not part of this code, it should still work
// (though calling direct is better for large arrays - it prevents extra marshaling).
// return XlCall.Excel(XlCall.xlUDF, "Resize", result);
}
public static double[,] MakeArrayAndResizeDoubles(int rows, int columns)
{
double[,] result = MakeArrayDoubles(rows, columns);
return ArrayResizer.ResizeDoubles(result);
}
}
public class ArrayResizer : XlCall
{
// This function will run in the UDF context.
// Needs extra protection to allow multithreaded use.
public static object Resize(object[,] array)
{
var caller = Excel(xlfCaller) as ExcelReference;
if (caller == null)
return array;
int rows = array.GetLength(0);
int columns = array.GetLength(1);
if (rows == 0 || columns == 0)
return array;
if ((caller.RowLast - caller.RowFirst + 1 == rows) &&
(caller.ColumnLast - caller.ColumnFirst + 1 == columns))
{
// Size is already OK - just return result
return array;
}
var rowLast = caller.RowFirst + rows - 1;
var columnLast = caller.ColumnFirst + columns - 1;
// Check for the sheet limits
if (rowLast > ExcelDnaUtil.ExcelLimits.MaxRows - 1 ||
columnLast > ExcelDnaUtil.ExcelLimits.MaxColumns - 1)
{
// Can't resize - goes beyond the end of the sheet - just return #VALUE
// (Can't give message here, or change cells)
return ExcelError.ExcelErrorValue;
}
// TODO: Add some kind of guard for ever-changing result?
ExcelAsyncUtil.QueueAsMacro(() =>
{
// Create a reference of the right size
var target = new ExcelReference(caller.RowFirst, rowLast, caller.ColumnFirst, columnLast, caller.SheetId);
DoResize(target); // Will trigger a recalc by writing formula
});
// Return what we have - to prevent flashing #N/A
return array;
}
public static double[,] ResizeDoubles(double[,] array)
{
var caller = Excel(xlfCaller) as ExcelReference;
if (caller == null)
return array;
int rows = array.GetLength(0);
int columns = array.GetLength(1);
if (rows == 0 || columns == 0)
return array;
if ((caller.RowLast - caller.RowFirst + 1 == rows) &&
(caller.ColumnLast - caller.ColumnFirst + 1 == columns))
{
// Size is already OK - just return result
return array;
}
var rowLast = caller.RowFirst + rows - 1;
var columnLast = caller.ColumnFirst + columns - 1;
if (rowLast > ExcelDnaUtil.ExcelLimits.MaxRows - 1 ||
columnLast > ExcelDnaUtil.ExcelLimits.MaxColumns - 1)
{
// Can't resize - goes beyond the end of the sheet - just return null (for #NUM!)
// (Can't give message here, or change cells)
return null;
}
// TODO: Add guard for ever-changing result?
ExcelAsyncUtil.QueueAsMacro(() =>
{
// Create a reference of the right size
var target = new ExcelReference(caller.RowFirst, rowLast, caller.ColumnFirst, columnLast, caller.SheetId);
DoResize(target); // Will trigger a recalc by writing formula
});
// Return what we have - to prevent flashing #N/A
return array;
}
static void DoResize(ExcelReference target)
{
// Get the current state for reset later
using (new ExcelEchoOffHelper())
using (new ExcelCalculationManualHelper())
using (new ExcelSelectionHelper(target))
{
var app = (Microsoft.Office.Interop.Excel.Application)ExcelDnaUtil.Application;
var firstCell = app.ActiveCell;
var formula = firstCell.Formula;
// Clear existing contents
if (firstCell.HasArray)
{
firstCell.CurrentArray.ClearContents();
}
else
{
firstCell.ClearContents();
}
// Set formula array in modified range
app.Selection.FormulaArray = formula;
app.Selection.Calculate();
}
}
}
// RIIA-style helpers to deal with Excel selections
// Don't use if you agree with Eric Lippert here: http://stackoverflow.com/a/1757344/44264
public class ExcelEchoOffHelper : XlCall, IDisposable
{
object oldEcho;
public ExcelEchoOffHelper()
{
oldEcho = Excel(xlfGetWorkspace, 40);
Excel(xlcEcho, false);
}
public void Dispose()
{
Excel(xlcEcho, oldEcho);
}
}
public class ExcelCalculationManualHelper : XlCall, IDisposable
{
object oldCalculationMode;
public ExcelCalculationManualHelper()
{
oldCalculationMode = Excel(xlfGetDocument, 14);
Excel(xlcOptionsCalculation, 3);
}
public void Dispose()
{
Excel(xlcOptionsCalculation, oldCalculationMode);
}
}
// Select an ExcelReference (perhaps on another sheet) allowing changes to be made there.
// On clean-up, resets all the selections and the active sheet.
// Should not be used if the work you are going to do will switch sheets, amke new sheets etc.
public class ExcelSelectionHelper : XlCall, IDisposable
{
object oldSelectionOnActiveSheet;
object oldActiveCellOnActiveSheet;
object oldSelectionOnRefSheet;
object oldActiveCellOnRefSheet;
public ExcelSelectionHelper(ExcelReference refToSelect)
{
// Remember old selection state on the active sheet
oldSelectionOnActiveSheet = Excel(xlfSelection);
oldActiveCellOnActiveSheet = Excel(xlfActiveCell);
// Switch to the sheet we want to select
string refSheet = (string)Excel(xlSheetNm, refToSelect);
Excel(xlcWorkbookSelect, new object[] { refSheet });
// record selection and active cell on the sheet we want to select
oldSelectionOnRefSheet = Excel(xlfSelection);
oldActiveCellOnRefSheet = Excel(xlfActiveCell);
// make the selection
Excel(xlcFormulaGoto, refToSelect);
}
public void Dispose()
{
// Reset the selection on the target sheet
Excel(xlcSelect, oldSelectionOnRefSheet, oldActiveCellOnRefSheet);
// Reset the sheet originally selected
string oldActiveSheet = (string)Excel(xlSheetNm, oldSelectionOnActiveSheet);
Excel(xlcWorkbookSelect, new object[] { oldActiveSheet });
// Reset the selection in the active sheet (some bugs make this change sometimes too)
Excel(xlcSelect, oldSelectionOnActiveSheet, oldActiveCellOnActiveSheet);
}
}
}
]]>
</DnaLibrary>