Skip to content

Commit d00d623

Browse files
zspitzweswigham
authored andcommitted
activex-excel: Default properties; remove EnumeratorConstructor overloads; reduce any (DefinitelyTyped#27427)
* Default properties; reduce any; remove EnumeratorConstructor overloads * Test fixes
1 parent c6a52c6 commit d00d623

File tree

6 files changed

+2253
-1846
lines changed

6 files changed

+2253
-1846
lines changed

types/activex-excel/activex-excel-tests.ts

Lines changed: 76 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,17 @@ const inCollection = <T = any>(collection: { Item(index: any): T }, index: strin
1616
return item;
1717
};
1818

19+
{
20+
let app1: Excel.Application | null = new ActiveXObject('Excel.Application');
21+
app1.Visible = true;
22+
const book1 = app1.Workbooks.Add();
23+
24+
app1.Quit();
25+
app1 = null;
26+
27+
WScript.Quit();
28+
}
29+
1930
const app = new ActiveXObject('Excel.Application');
2031

2132
// create a workbook -- https://msdn.microsoft.com/en-us/vba/excel-vba/articles/create-a-workbook
@@ -27,7 +38,7 @@ newBook.SaveAs('allsales.xls');
2738
// create or replace a worksheet -- https://msdn.microsoft.com/en-us/vba/excel-vba/articles/create-or-replace-a-worksheet
2839
const newOrExistingWorksheet = () => {
2940
const mySheetName = 'Sheet4';
30-
let mySheet = inCollection(newBook.Worksheets, mySheetName) as Excel.Worksheet | undefined;
41+
let mySheet = inCollection<Excel.Worksheet>(newBook.Worksheets, mySheetName);
3142
if (!mySheet) {
3243
WScript.Echo(`The sheet named "${mySheetName} doesn't exist, but will be created.`);
3344
mySheet = app.Worksheets.Add() as Excel.Worksheet;
@@ -37,7 +48,7 @@ const newOrExistingWorksheet = () => {
3748
const replaceWorksheet = () => {
3849
const mySheetName = 'Sheet4';
3950
app.DisplayAlerts = false;
40-
let mySheet = inCollection<Excel.Worksheet | Excel.Chart | Excel.DialogSheet>(app.Worksheets, mySheetName);
51+
let mySheet = inCollection<Excel.Worksheet>(app.Worksheets, mySheetName);
4152
if (mySheet) { mySheet.Delete(); }
4253
app.DisplayAlerts = true;
4354
mySheet = app.Worksheets.Add() as Excel.Worksheet;
@@ -46,19 +57,17 @@ const replaceWorksheet = () => {
4657
};
4758

4859
// referencing multiple sheets -- https://msdn.microsoft.com/VBA/Excel-VBA/articles/sheets-object-excel
49-
const moveMultipleSheets = () => {
50-
app.Worksheets.Item(toSafeArray<string | number>(1, 'Sheet2')).Move(4);
51-
};
60+
const moveMultipleSheets = () => app.Worksheets(toSafeArray<string | number>(1, 'Sheet2')).Move(4);
5261

5362
// sort worksheets alphanumerically by name -- https://msdn.microsoft.com/en-us/vba/excel-vba/articles/sort-worksheets-alphanumerically-by-name
5463
const sortSheetsTabName = () => {
5564
app.ScreenUpdating = false;
5665
const sheets = app.ActiveWorkbook.Sheets;
5766
const sheetCount = sheets.Count;
5867
for (let i = 0; i < sheetCount; i += 1) {
59-
const sheetI = sheets.Item(i);
68+
const sheetI = sheets(i);
6069
for (let j = i; j < sheetCount; j += 1) {
61-
const sheetJ = sheets.Item(j);
70+
const sheetJ = sheets(j);
6271
if (sheetJ.Name < sheetI.Name) { sheetJ.Move(sheetI); }
6372
}
6473
}
@@ -68,7 +77,7 @@ const sortSheetsTabName = () => {
6877
// fill a value down into blank cells in a column -- https://msdn.microsoft.com/en-us/vba/excel-vba/articles/fill-a-value-down-into-blank-cells-in-a-column
6978
const fillCellsFromAbove = () => {
7079
app.ScreenUpdating = false;
71-
const columnA = app.Columns.Item(1);
80+
const columnA = app.Columns(1);
7281
try {
7382
columnA.SpecialCells(Excel.XlCellType.xlCellTypeBlanks).Formula = '=R[-1]C';
7483
columnA.Value = columnA.Value;
@@ -78,7 +87,7 @@ const fillCellsFromAbove = () => {
7887

7988
// hide and unhide columns -- https://msdn.microsoft.com/en-us/vba/excel-vba/articles/hide-and-unhide-columns
8089
const setColumnVisibility = (visible: boolean) => {
81-
const book = app.Workbooks.Item(1);
90+
const book = app.Workbooks(1);
8291
const sheet = inCollection<Excel.Worksheet | Excel.Chart | Excel.DialogSheet>(book.Worksheets, 'Sheet1');
8392
if (!sheet) { return; }
8493

@@ -97,7 +106,7 @@ const setColumnVisibility = (visible: boolean) => {
97106
};
98107

99108
// highlighting the active cell, row, or column -- https://msdn.microsoft.com/en-us/vba/excel-vba/articles/highlight-the-active-cell-row-or-column
100-
(() => {
109+
{
101110
const wks = app.ActiveSheet as Excel.Worksheet;
102111

103112
// highlight active cell
@@ -120,10 +129,10 @@ const setColumnVisibility = (visible: boolean) => {
120129
prm.Target.EntireColumn.Interior.ColorIndex = 8;
121130
app.ScreenUpdating = true;
122131
});
123-
})();
132+
}
124133

125134
// referencing cells -- https://msdn.microsoft.com/en-us/vba/excel-vba/articles/reference-cells-and-ranges
126-
(() => {
135+
{
127136
const wks = app.ActiveSheet as Excel.Worksheet;
128137

129138
// all the cells on a worksheet
@@ -141,20 +150,19 @@ const setColumnVisibility = (visible: boolean) => {
141150
wks.Range('A:A,C:C,F:F').Font.Bold = true;
142151

143152
// using index numbers
144-
wks.Cells.Item(6, 1).Value2 = 10;
145-
// Value is also a property with parameters
146-
ActiveXObject.set(wks.Cells.Item(6, 1), 'Value', 10);
153+
wks.Cells(6, 1).Value2 = 10;
154+
wks.Cells(6, 1).Value = 10;
147155

148156
// iterating through cells using index numbers
149157
for (let counter = 1; counter < 20; counter += 1) {
150-
ActiveXObject.set(wks.Cells.Item(counter, 1), 'Value', 10);
158+
wks.Cells(counter, 1).Value = 10;
151159
}
152160

153161
// relative to other cells
154-
wks.Cells.Item(1, 1).Font.Underline = Excel.XlUnderlineStyle.xlUnderlineStyleDouble;
162+
wks.Cells(1, 1).Font.Underline = Excel.XlUnderlineStyle.xlUnderlineStyleDouble;
155163

156164
// using a Range object
157-
const rng = wks.Cells.Item('A1:D5');
165+
const rng = wks.Cells('A1:D5');
158166
rng.Formula = '=RAND()';
159167
rng.Font.Bold = true;
160168

@@ -166,23 +174,22 @@ const setColumnVisibility = (visible: boolean) => {
166174

167175
// refer to multiple ranges using Areas
168176
WScript.Echo(union.Areas.Count);
169-
})();
177+
}
170178

171179
// looping through a range of cells -- https://msdn.microsoft.com/en-us/vba/excel-vba/articles/looping-through-a-range-of-cells
172-
(() => {
180+
{
173181
const wks = app.ActiveSheet as Excel.Worksheet;
174182

175183
// using for
176184
for (let x = 1; x < 20; x++) {
177-
const currentCell = wks.Cells.Item(x, 1);
178-
if (Math.abs(currentCell.Value()) < 0.01) {
179-
// because Value is typed as a method on the Excel.Range class, we have to treat it as a setter with parameters
180-
ActiveXObject.set(currentCell, 'Value', 0);
185+
const currentCell = wks.Cells(x, 1);
186+
if (Math.abs(currentCell.Value) < 0.01) {
187+
currentCell.Value = 0;
181188
}
182189
}
183190

184191
// using Enumerator
185-
let enumerator = new Enumerator(wks.Cells.Item('A1:D10'));
192+
let enumerator = new Enumerator(wks.Cells('A1:D10'));
186193
enumerator.moveFirst();
187194
while (!enumerator.atEnd()) {
188195
const currentCell = enumerator.item();
@@ -202,11 +209,11 @@ const setColumnVisibility = (visible: boolean) => {
202209
}
203210
enumerator.moveNext();
204211
}
205-
})();
212+
}
206213

207214
// using selection -- https://msdn.microsoft.com/en-us/vba/excel-vba/articles/selecting-and-activating-cells
208-
(() => {
209-
const wks = app.ActiveWorkbook.Worksheets.Item(1) as Excel.Worksheet;
215+
{
216+
const wks = app.ActiveWorkbook.Worksheets(1);
210217

211218
// make a worksheet the active worksheet; otherwise code which uses the selection will fail
212219
wks.Select();
@@ -219,24 +226,24 @@ const setColumnVisibility = (visible: boolean) => {
219226
wks.Range("B1").Activate();
220227

221228
// working with 3-D ranges -- https://msdn.microsoft.com/en-us/vba/excel-vba/articles/working-with-3-d-ranges
222-
app.Sheets.Item(toSafeArray("Sheet2", "Sheet3", "Sheet4")).Select();
229+
app.Sheets(toSafeArray("Sheet2", "Sheet3", "Sheet4")).Select();
223230
app.Range("A1:H1").Select();
224-
(app.Selection as Excel.Range).Borders.Item(Excel.XlBordersIndex.xlEdgeBottom).LineStyle = Excel.XlLineStyle.xlDouble;
231+
(app.Selection as Excel.Range).Borders(Excel.XlBordersIndex.xlEdgeBottom).LineStyle = Excel.XlLineStyle.xlDouble;
225232

226233
// alternatively, use FillAcrossSheets to fill formatting and data across sheets
227234
const book = app.ActiveWorkbook;
228-
const wks2 = book.Sheets.Item("Sheet2") as Excel.Worksheet;
235+
const wks2 = book.Worksheets("Sheet2");
229236
const rng = wks2.Range("A1:H1");
230-
rng.Borders.Item(Excel.XlBordersIndex.xlEdgeBottom).LineStyle = Excel.XlLineStyle.xlDouble;
237+
rng.Borders(Excel.XlBordersIndex.xlEdgeBottom).LineStyle = Excel.XlLineStyle.xlDouble;
231238
book.Sheets.FillAcrossSheets(rng);
232-
})();
239+
}
233240

234241
// prevent duplicate entry -- https://msdn.microsoft.com/en-us/vba/excel-vba/articles/prevent-duplicate-entries-in-a-range
235-
(() => {
236-
const book = app.Workbooks.Item(1);
242+
{
243+
const book = app.Workbooks(1);
237244

238245
ActiveXObject.on(book, 'SheetChange', ['Sh', 'Target'], function(this, prm) {
239-
const EvalRange = this.ActiveSheet.Range("A1:B20");
246+
const EvalRange = (this.ActiveSheet as Excel.Worksheet).Range("A1:B20");
240247

241248
// If the cell where the value was entered is not in the defined range, if the value pasted is larger than a single cell, or if no value was entered in the cell, then exit the macro
242249
if (
@@ -253,10 +260,11 @@ const setColumnVisibility = (visible: boolean) => {
253260
return;
254261
}
255262

263+
// const enumerator = new Enumerator<Excel.Worksheet | Excel.Chart | Excel.DialogSheet>(book.Worksheets);
256264
const enumerator = new Enumerator(book.Worksheets);
257265
enumerator.moveFirst();
258266
while (!enumerator.atEnd()) {
259-
const wks = enumerator.item() as Excel.Worksheet;
267+
const wks = enumerator.item();
260268
if (wks.Name === prm.Target.Name) { continue; }
261269

262270
// If the value entered already exists in the defined range on the current worksheet, undo the entry.
@@ -267,14 +275,14 @@ const setColumnVisibility = (visible: boolean) => {
267275
app.EnableEvents = true;
268276
}
269277
});
270-
})();
278+
}
271279

272280
// add a unique list of values to a combobox -- https://msdn.microsoft.com/en-us/vba/excel-vba/articles/add-a-unique-list-of-values-to-a-combo-box
273-
(() => {
274-
(() => {
281+
{
282+
{
275283
// using the AdvancedFilter property
276284
const book = app.ThisWorkbook;
277-
const sheet = book.Worksheets.Item("Sheet1") as Excel.Worksheet;
285+
const sheet = book.Worksheets("Sheet1");
278286
const dataRange = sheet.Range('A1', sheet.Range("A100").End(Excel.XlDirection.xlUp));
279287
dataRange.AdvancedFilter(Excel.XlFilterAction.xlFilterCopy, undefined, sheet.Range('L1'), true);
280288
const data = sheet.Range("L2", sheet.Range('L100').End(Excel.XlDirection.xlUp)).Value() as SafeArray;
@@ -284,15 +292,17 @@ const setColumnVisibility = (visible: boolean) => {
284292
combobox.Clear();
285293
ActiveXObject.set(combobox, 'List', [], data);
286294
combobox.ListIndex = -1;
287-
})();
295+
}
288296

289-
(() => {
297+
{
290298
// using a Dictionary
291-
const sheet = app.ThisWorkbook.Sheets.Item('Sheet2') as Excel.Worksheet;
299+
const sheet = app.ThisWorkbook.Sheets('Sheet2') as Excel.Worksheet;
292300
const data = sheet.Range('A2', sheet.Range('A100').End(Excel.XlDirection.xlUp)).Value2 as SafeArray;
293301
const arr = new VBArray(data).toArray();
294302
const dict = new ActiveXObject('Scripting.Dictionary');
295-
arr.forEach(x => ActiveXObject.set(dict, 'Item', [x], true));
303+
for (const x of arr) {
304+
ActiveXObject.set(dict, 'Item', [x], true);
305+
}
296306

297307
const combobox = sheet.OLEObjects('ComboBox1').Object as MSForms.ComboBox;
298308
combobox.Clear();
@@ -307,21 +317,28 @@ const setColumnVisibility = (visible: boolean) => {
307317

308318
// alternatively, make a JS array out of the keys, and iterate using forEach
309319
// new VBArray(dict.Keys()).toArray().forEach(x => combobox.AddItem(x));
310-
})();
311-
})();
320+
}
312321

313-
// animating a sparkline -- https://msdn.microsoft.com/en-us/vba/excel-vba/articles/animate-a-sparkline
314-
(() => {
315-
const wks = app.ActiveSheet as Excel.Worksheet;
316-
const oSparkGroup = wks.Cells.SparklineGroups.Item(1);
322+
// animating a sparkline -- https://msdn.microsoft.com/en-us/vba/excel-vba/articles/animate-a-sparkline
323+
{
324+
const wks = app.ActiveSheet as Excel.Worksheet;
325+
const oSparkGroup = wks.Cells.SparklineGroups(1);
317326

318-
// Set the data source to the first year of data
319-
oSparkGroup.ModifySourceData('B2:M4');
327+
// Set the data source to the first year of data
328+
oSparkGroup.ModifySourceData('B2:M4');
320329

321-
// Loop through the data points for the subsequent two years
322-
for (let i = 1; i <= 24; i++) {
323-
// Move the reference for the sparkline group over one cell
324-
oSparkGroup.ModifySourceData(wks.Range(oSparkGroup.SourceData).Offset(0, 1).Address());
325-
WScript.Sleep(1000);
330+
// Loop through the data points for the subsequent two years
331+
for (let i = 1; i <= 24; i++) {
332+
// Move the reference for the sparkline group over one cell
333+
oSparkGroup.ModifySourceData(wks.Range(oSparkGroup.SourceData).Offset(0, 1).Address());
334+
WScript.Sleep(1000);
335+
}
336+
}
337+
338+
{
339+
const formats = new VBArray(app.ClipboardFormats).toArray();
340+
for (const format of formats) {
341+
WScript.Echo(format);
342+
}
326343
}
327-
})();
344+
}

0 commit comments

Comments
 (0)