@@ -16,6 +16,17 @@ const inCollection = <T = any>(collection: { Item(index: any): T }, index: strin
16
16
return item ;
17
17
} ;
18
18
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
+
19
30
const app = new ActiveXObject ( 'Excel.Application' ) ;
20
31
21
32
// create a workbook -- https://msdn.microsoft.com/en-us/vba/excel-vba/articles/create-a-workbook
@@ -27,7 +38,7 @@ newBook.SaveAs('allsales.xls');
27
38
// create or replace a worksheet -- https://msdn.microsoft.com/en-us/vba/excel-vba/articles/create-or-replace-a-worksheet
28
39
const newOrExistingWorksheet = ( ) => {
29
40
const mySheetName = 'Sheet4' ;
30
- let mySheet = inCollection ( newBook . Worksheets , mySheetName ) as Excel . Worksheet | undefined ;
41
+ let mySheet = inCollection < Excel . Worksheet > ( newBook . Worksheets , mySheetName ) ;
31
42
if ( ! mySheet ) {
32
43
WScript . Echo ( `The sheet named "${ mySheetName } doesn't exist, but will be created.` ) ;
33
44
mySheet = app . Worksheets . Add ( ) as Excel . Worksheet ;
@@ -37,7 +48,7 @@ const newOrExistingWorksheet = () => {
37
48
const replaceWorksheet = ( ) => {
38
49
const mySheetName = 'Sheet4' ;
39
50
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 ) ;
41
52
if ( mySheet ) { mySheet . Delete ( ) ; }
42
53
app . DisplayAlerts = true ;
43
54
mySheet = app . Worksheets . Add ( ) as Excel . Worksheet ;
@@ -46,19 +57,17 @@ const replaceWorksheet = () => {
46
57
} ;
47
58
48
59
// 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 ) ;
52
61
53
62
// sort worksheets alphanumerically by name -- https://msdn.microsoft.com/en-us/vba/excel-vba/articles/sort-worksheets-alphanumerically-by-name
54
63
const sortSheetsTabName = ( ) => {
55
64
app . ScreenUpdating = false ;
56
65
const sheets = app . ActiveWorkbook . Sheets ;
57
66
const sheetCount = sheets . Count ;
58
67
for ( let i = 0 ; i < sheetCount ; i += 1 ) {
59
- const sheetI = sheets . Item ( i ) ;
68
+ const sheetI = sheets ( i ) ;
60
69
for ( let j = i ; j < sheetCount ; j += 1 ) {
61
- const sheetJ = sheets . Item ( j ) ;
70
+ const sheetJ = sheets ( j ) ;
62
71
if ( sheetJ . Name < sheetI . Name ) { sheetJ . Move ( sheetI ) ; }
63
72
}
64
73
}
@@ -68,7 +77,7 @@ const sortSheetsTabName = () => {
68
77
// 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
69
78
const fillCellsFromAbove = ( ) => {
70
79
app . ScreenUpdating = false ;
71
- const columnA = app . Columns . Item ( 1 ) ;
80
+ const columnA = app . Columns ( 1 ) ;
72
81
try {
73
82
columnA . SpecialCells ( Excel . XlCellType . xlCellTypeBlanks ) . Formula = '=R[-1]C' ;
74
83
columnA . Value = columnA . Value ;
@@ -78,7 +87,7 @@ const fillCellsFromAbove = () => {
78
87
79
88
// hide and unhide columns -- https://msdn.microsoft.com/en-us/vba/excel-vba/articles/hide-and-unhide-columns
80
89
const setColumnVisibility = ( visible : boolean ) => {
81
- const book = app . Workbooks . Item ( 1 ) ;
90
+ const book = app . Workbooks ( 1 ) ;
82
91
const sheet = inCollection < Excel . Worksheet | Excel . Chart | Excel . DialogSheet > ( book . Worksheets , 'Sheet1' ) ;
83
92
if ( ! sheet ) { return ; }
84
93
@@ -97,7 +106,7 @@ const setColumnVisibility = (visible: boolean) => {
97
106
} ;
98
107
99
108
// 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
+ {
101
110
const wks = app . ActiveSheet as Excel . Worksheet ;
102
111
103
112
// highlight active cell
@@ -120,10 +129,10 @@ const setColumnVisibility = (visible: boolean) => {
120
129
prm . Target . EntireColumn . Interior . ColorIndex = 8 ;
121
130
app . ScreenUpdating = true ;
122
131
} ) ;
123
- } ) ( ) ;
132
+ }
124
133
125
134
// referencing cells -- https://msdn.microsoft.com/en-us/vba/excel-vba/articles/reference-cells-and-ranges
126
- ( ( ) => {
135
+ {
127
136
const wks = app . ActiveSheet as Excel . Worksheet ;
128
137
129
138
// all the cells on a worksheet
@@ -141,20 +150,19 @@ const setColumnVisibility = (visible: boolean) => {
141
150
wks . Range ( 'A:A,C:C,F:F' ) . Font . Bold = true ;
142
151
143
152
// 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 ;
147
155
148
156
// iterating through cells using index numbers
149
157
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 ;
151
159
}
152
160
153
161
// 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 ;
155
163
156
164
// using a Range object
157
- const rng = wks . Cells . Item ( 'A1:D5' ) ;
165
+ const rng = wks . Cells ( 'A1:D5' ) ;
158
166
rng . Formula = '=RAND()' ;
159
167
rng . Font . Bold = true ;
160
168
@@ -166,23 +174,22 @@ const setColumnVisibility = (visible: boolean) => {
166
174
167
175
// refer to multiple ranges using Areas
168
176
WScript . Echo ( union . Areas . Count ) ;
169
- } ) ( ) ;
177
+ }
170
178
171
179
// looping through a range of cells -- https://msdn.microsoft.com/en-us/vba/excel-vba/articles/looping-through-a-range-of-cells
172
- ( ( ) => {
180
+ {
173
181
const wks = app . ActiveSheet as Excel . Worksheet ;
174
182
175
183
// using for
176
184
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 ;
181
188
}
182
189
}
183
190
184
191
// using Enumerator
185
- let enumerator = new Enumerator ( wks . Cells . Item ( 'A1:D10' ) ) ;
192
+ let enumerator = new Enumerator ( wks . Cells ( 'A1:D10' ) ) ;
186
193
enumerator . moveFirst ( ) ;
187
194
while ( ! enumerator . atEnd ( ) ) {
188
195
const currentCell = enumerator . item ( ) ;
@@ -202,11 +209,11 @@ const setColumnVisibility = (visible: boolean) => {
202
209
}
203
210
enumerator . moveNext ( ) ;
204
211
}
205
- } ) ( ) ;
212
+ }
206
213
207
214
// 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 ) ;
210
217
211
218
// make a worksheet the active worksheet; otherwise code which uses the selection will fail
212
219
wks . Select ( ) ;
@@ -219,24 +226,24 @@ const setColumnVisibility = (visible: boolean) => {
219
226
wks . Range ( "B1" ) . Activate ( ) ;
220
227
221
228
// 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 ( ) ;
223
230
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 ;
225
232
226
233
// alternatively, use FillAcrossSheets to fill formatting and data across sheets
227
234
const book = app . ActiveWorkbook ;
228
- const wks2 = book . Sheets . Item ( "Sheet2" ) as Excel . Worksheet ;
235
+ const wks2 = book . Worksheets ( "Sheet2" ) ;
229
236
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 ;
231
238
book . Sheets . FillAcrossSheets ( rng ) ;
232
- } ) ( ) ;
239
+ }
233
240
234
241
// 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 ) ;
237
244
238
245
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" ) ;
240
247
241
248
// 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
242
249
if (
@@ -253,10 +260,11 @@ const setColumnVisibility = (visible: boolean) => {
253
260
return ;
254
261
}
255
262
263
+ // const enumerator = new Enumerator<Excel.Worksheet | Excel.Chart | Excel.DialogSheet>(book.Worksheets);
256
264
const enumerator = new Enumerator ( book . Worksheets ) ;
257
265
enumerator . moveFirst ( ) ;
258
266
while ( ! enumerator . atEnd ( ) ) {
259
- const wks = enumerator . item ( ) as Excel . Worksheet ;
267
+ const wks = enumerator . item ( ) ;
260
268
if ( wks . Name === prm . Target . Name ) { continue ; }
261
269
262
270
// 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) => {
267
275
app . EnableEvents = true ;
268
276
}
269
277
} ) ;
270
- } ) ( ) ;
278
+ }
271
279
272
280
// 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
+ {
275
283
// using the AdvancedFilter property
276
284
const book = app . ThisWorkbook ;
277
- const sheet = book . Worksheets . Item ( "Sheet1" ) as Excel . Worksheet ;
285
+ const sheet = book . Worksheets ( "Sheet1" ) ;
278
286
const dataRange = sheet . Range ( 'A1' , sheet . Range ( "A100" ) . End ( Excel . XlDirection . xlUp ) ) ;
279
287
dataRange . AdvancedFilter ( Excel . XlFilterAction . xlFilterCopy , undefined , sheet . Range ( 'L1' ) , true ) ;
280
288
const data = sheet . Range ( "L2" , sheet . Range ( 'L100' ) . End ( Excel . XlDirection . xlUp ) ) . Value ( ) as SafeArray ;
@@ -284,15 +292,17 @@ const setColumnVisibility = (visible: boolean) => {
284
292
combobox . Clear ( ) ;
285
293
ActiveXObject . set ( combobox , 'List' , [ ] , data ) ;
286
294
combobox . ListIndex = - 1 ;
287
- } ) ( ) ;
295
+ }
288
296
289
- ( ( ) => {
297
+ {
290
298
// using a Dictionary
291
- const sheet = app . ThisWorkbook . Sheets . Item ( 'Sheet2' ) as Excel . Worksheet ;
299
+ const sheet = app . ThisWorkbook . Sheets ( 'Sheet2' ) as Excel . Worksheet ;
292
300
const data = sheet . Range ( 'A2' , sheet . Range ( 'A100' ) . End ( Excel . XlDirection . xlUp ) ) . Value2 as SafeArray ;
293
301
const arr = new VBArray ( data ) . toArray ( ) ;
294
302
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
+ }
296
306
297
307
const combobox = sheet . OLEObjects ( 'ComboBox1' ) . Object as MSForms . ComboBox ;
298
308
combobox . Clear ( ) ;
@@ -307,21 +317,28 @@ const setColumnVisibility = (visible: boolean) => {
307
317
308
318
// alternatively, make a JS array out of the keys, and iterate using forEach
309
319
// new VBArray(dict.Keys()).toArray().forEach(x => combobox.AddItem(x));
310
- } ) ( ) ;
311
- } ) ( ) ;
320
+ }
312
321
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 ) ;
317
326
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' ) ;
320
329
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
+ }
326
343
}
327
- } ) ( ) ;
344
+ }
0 commit comments