activex-excel: Default properties; remove EnumeratorConstructor overloads; reduce any (#27427)

* Default properties; reduce any; remove EnumeratorConstructor overloads

* Test fixes
This commit is contained in:
Zev Spitz
2018-07-20 17:26:40 -07:00
committed by Wesley Wigham
parent c6a52c68c8
commit d00d623d95
6 changed files with 2234 additions and 1827 deletions
+79 -62
View File
@@ -16,6 +16,17 @@ const inCollection = <T = any>(collection: { Item(index: any): T }, index: strin
return item;
};
{
let app1: Excel.Application | null = new ActiveXObject('Excel.Application');
app1.Visible = true;
const book1 = app1.Workbooks.Add();
app1.Quit();
app1 = null;
WScript.Quit();
}
const app = new ActiveXObject('Excel.Application');
// create a workbook -- https://msdn.microsoft.com/en-us/vba/excel-vba/articles/create-a-workbook
@@ -27,7 +38,7 @@ newBook.SaveAs('allsales.xls');
// create or replace a worksheet -- https://msdn.microsoft.com/en-us/vba/excel-vba/articles/create-or-replace-a-worksheet
const newOrExistingWorksheet = () => {
const mySheetName = 'Sheet4';
let mySheet = inCollection(newBook.Worksheets, mySheetName) as Excel.Worksheet | undefined;
let mySheet = inCollection<Excel.Worksheet>(newBook.Worksheets, mySheetName);
if (!mySheet) {
WScript.Echo(`The sheet named "${mySheetName} doesn't exist, but will be created.`);
mySheet = app.Worksheets.Add() as Excel.Worksheet;
@@ -37,7 +48,7 @@ const newOrExistingWorksheet = () => {
const replaceWorksheet = () => {
const mySheetName = 'Sheet4';
app.DisplayAlerts = false;
let mySheet = inCollection<Excel.Worksheet | Excel.Chart | Excel.DialogSheet>(app.Worksheets, mySheetName);
let mySheet = inCollection<Excel.Worksheet>(app.Worksheets, mySheetName);
if (mySheet) { mySheet.Delete(); }
app.DisplayAlerts = true;
mySheet = app.Worksheets.Add() as Excel.Worksheet;
@@ -46,9 +57,7 @@ const replaceWorksheet = () => {
};
// referencing multiple sheets -- https://msdn.microsoft.com/VBA/Excel-VBA/articles/sheets-object-excel
const moveMultipleSheets = () => {
app.Worksheets.Item(toSafeArray<string | number>(1, 'Sheet2')).Move(4);
};
const moveMultipleSheets = () => app.Worksheets(toSafeArray<string | number>(1, 'Sheet2')).Move(4);
// sort worksheets alphanumerically by name -- https://msdn.microsoft.com/en-us/vba/excel-vba/articles/sort-worksheets-alphanumerically-by-name
const sortSheetsTabName = () => {
@@ -56,9 +65,9 @@ const sortSheetsTabName = () => {
const sheets = app.ActiveWorkbook.Sheets;
const sheetCount = sheets.Count;
for (let i = 0; i < sheetCount; i += 1) {
const sheetI = sheets.Item(i);
const sheetI = sheets(i);
for (let j = i; j < sheetCount; j += 1) {
const sheetJ = sheets.Item(j);
const sheetJ = sheets(j);
if (sheetJ.Name < sheetI.Name) { sheetJ.Move(sheetI); }
}
}
@@ -68,7 +77,7 @@ const sortSheetsTabName = () => {
// 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
const fillCellsFromAbove = () => {
app.ScreenUpdating = false;
const columnA = app.Columns.Item(1);
const columnA = app.Columns(1);
try {
columnA.SpecialCells(Excel.XlCellType.xlCellTypeBlanks).Formula = '=R[-1]C';
columnA.Value = columnA.Value;
@@ -78,7 +87,7 @@ const fillCellsFromAbove = () => {
// hide and unhide columns -- https://msdn.microsoft.com/en-us/vba/excel-vba/articles/hide-and-unhide-columns
const setColumnVisibility = (visible: boolean) => {
const book = app.Workbooks.Item(1);
const book = app.Workbooks(1);
const sheet = inCollection<Excel.Worksheet | Excel.Chart | Excel.DialogSheet>(book.Worksheets, 'Sheet1');
if (!sheet) { return; }
@@ -97,7 +106,7 @@ const setColumnVisibility = (visible: boolean) => {
};
// highlighting the active cell, row, or column -- https://msdn.microsoft.com/en-us/vba/excel-vba/articles/highlight-the-active-cell-row-or-column
(() => {
{
const wks = app.ActiveSheet as Excel.Worksheet;
// highlight active cell
@@ -120,10 +129,10 @@ const setColumnVisibility = (visible: boolean) => {
prm.Target.EntireColumn.Interior.ColorIndex = 8;
app.ScreenUpdating = true;
});
})();
}
// referencing cells -- https://msdn.microsoft.com/en-us/vba/excel-vba/articles/reference-cells-and-ranges
(() => {
{
const wks = app.ActiveSheet as Excel.Worksheet;
// all the cells on a worksheet
@@ -141,20 +150,19 @@ const setColumnVisibility = (visible: boolean) => {
wks.Range('A:A,C:C,F:F').Font.Bold = true;
// using index numbers
wks.Cells.Item(6, 1).Value2 = 10;
// Value is also a property with parameters
ActiveXObject.set(wks.Cells.Item(6, 1), 'Value', 10);
wks.Cells(6, 1).Value2 = 10;
wks.Cells(6, 1).Value = 10;
// iterating through cells using index numbers
for (let counter = 1; counter < 20; counter += 1) {
ActiveXObject.set(wks.Cells.Item(counter, 1), 'Value', 10);
wks.Cells(counter, 1).Value = 10;
}
// relative to other cells
wks.Cells.Item(1, 1).Font.Underline = Excel.XlUnderlineStyle.xlUnderlineStyleDouble;
wks.Cells(1, 1).Font.Underline = Excel.XlUnderlineStyle.xlUnderlineStyleDouble;
// using a Range object
const rng = wks.Cells.Item('A1:D5');
const rng = wks.Cells('A1:D5');
rng.Formula = '=RAND()';
rng.Font.Bold = true;
@@ -166,23 +174,22 @@ const setColumnVisibility = (visible: boolean) => {
// refer to multiple ranges using Areas
WScript.Echo(union.Areas.Count);
})();
}
// looping through a range of cells -- https://msdn.microsoft.com/en-us/vba/excel-vba/articles/looping-through-a-range-of-cells
(() => {
{
const wks = app.ActiveSheet as Excel.Worksheet;
// using for
for (let x = 1; x < 20; x++) {
const currentCell = wks.Cells.Item(x, 1);
if (Math.abs(currentCell.Value()) < 0.01) {
// because Value is typed as a method on the Excel.Range class, we have to treat it as a setter with parameters
ActiveXObject.set(currentCell, 'Value', 0);
const currentCell = wks.Cells(x, 1);
if (Math.abs(currentCell.Value) < 0.01) {
currentCell.Value = 0;
}
}
// using Enumerator
let enumerator = new Enumerator(wks.Cells.Item('A1:D10'));
let enumerator = new Enumerator(wks.Cells('A1:D10'));
enumerator.moveFirst();
while (!enumerator.atEnd()) {
const currentCell = enumerator.item();
@@ -202,11 +209,11 @@ const setColumnVisibility = (visible: boolean) => {
}
enumerator.moveNext();
}
})();
}
// using selection -- https://msdn.microsoft.com/en-us/vba/excel-vba/articles/selecting-and-activating-cells
(() => {
const wks = app.ActiveWorkbook.Worksheets.Item(1) as Excel.Worksheet;
{
const wks = app.ActiveWorkbook.Worksheets(1);
// make a worksheet the active worksheet; otherwise code which uses the selection will fail
wks.Select();
@@ -219,24 +226,24 @@ const setColumnVisibility = (visible: boolean) => {
wks.Range("B1").Activate();
// working with 3-D ranges -- https://msdn.microsoft.com/en-us/vba/excel-vba/articles/working-with-3-d-ranges
app.Sheets.Item(toSafeArray("Sheet2", "Sheet3", "Sheet4")).Select();
app.Sheets(toSafeArray("Sheet2", "Sheet3", "Sheet4")).Select();
app.Range("A1:H1").Select();
(app.Selection as Excel.Range).Borders.Item(Excel.XlBordersIndex.xlEdgeBottom).LineStyle = Excel.XlLineStyle.xlDouble;
(app.Selection as Excel.Range).Borders(Excel.XlBordersIndex.xlEdgeBottom).LineStyle = Excel.XlLineStyle.xlDouble;
// alternatively, use FillAcrossSheets to fill formatting and data across sheets
const book = app.ActiveWorkbook;
const wks2 = book.Sheets.Item("Sheet2") as Excel.Worksheet;
const wks2 = book.Worksheets("Sheet2");
const rng = wks2.Range("A1:H1");
rng.Borders.Item(Excel.XlBordersIndex.xlEdgeBottom).LineStyle = Excel.XlLineStyle.xlDouble;
rng.Borders(Excel.XlBordersIndex.xlEdgeBottom).LineStyle = Excel.XlLineStyle.xlDouble;
book.Sheets.FillAcrossSheets(rng);
})();
}
// prevent duplicate entry -- https://msdn.microsoft.com/en-us/vba/excel-vba/articles/prevent-duplicate-entries-in-a-range
(() => {
const book = app.Workbooks.Item(1);
{
const book = app.Workbooks(1);
ActiveXObject.on(book, 'SheetChange', ['Sh', 'Target'], function(this, prm) {
const EvalRange = this.ActiveSheet.Range("A1:B20");
const EvalRange = (this.ActiveSheet as Excel.Worksheet).Range("A1:B20");
// 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
if (
@@ -253,10 +260,11 @@ const setColumnVisibility = (visible: boolean) => {
return;
}
// const enumerator = new Enumerator<Excel.Worksheet | Excel.Chart | Excel.DialogSheet>(book.Worksheets);
const enumerator = new Enumerator(book.Worksheets);
enumerator.moveFirst();
while (!enumerator.atEnd()) {
const wks = enumerator.item() as Excel.Worksheet;
const wks = enumerator.item();
if (wks.Name === prm.Target.Name) { continue; }
// 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) => {
app.EnableEvents = true;
}
});
})();
}
// 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
(() => {
(() => {
{
{
// using the AdvancedFilter property
const book = app.ThisWorkbook;
const sheet = book.Worksheets.Item("Sheet1") as Excel.Worksheet;
const sheet = book.Worksheets("Sheet1");
const dataRange = sheet.Range('A1', sheet.Range("A100").End(Excel.XlDirection.xlUp));
dataRange.AdvancedFilter(Excel.XlFilterAction.xlFilterCopy, undefined, sheet.Range('L1'), true);
const data = sheet.Range("L2", sheet.Range('L100').End(Excel.XlDirection.xlUp)).Value() as SafeArray;
@@ -284,15 +292,17 @@ const setColumnVisibility = (visible: boolean) => {
combobox.Clear();
ActiveXObject.set(combobox, 'List', [], data);
combobox.ListIndex = -1;
})();
}
(() => {
{
// using a Dictionary
const sheet = app.ThisWorkbook.Sheets.Item('Sheet2') as Excel.Worksheet;
const sheet = app.ThisWorkbook.Sheets('Sheet2') as Excel.Worksheet;
const data = sheet.Range('A2', sheet.Range('A100').End(Excel.XlDirection.xlUp)).Value2 as SafeArray;
const arr = new VBArray(data).toArray();
const dict = new ActiveXObject('Scripting.Dictionary');
arr.forEach(x => ActiveXObject.set(dict, 'Item', [x], true));
for (const x of arr) {
ActiveXObject.set(dict, 'Item', [x], true);
}
const combobox = sheet.OLEObjects('ComboBox1').Object as MSForms.ComboBox;
combobox.Clear();
@@ -307,21 +317,28 @@ const setColumnVisibility = (visible: boolean) => {
// alternatively, make a JS array out of the keys, and iterate using forEach
// new VBArray(dict.Keys()).toArray().forEach(x => combobox.AddItem(x));
})();
})();
// animating a sparkline -- https://msdn.microsoft.com/en-us/vba/excel-vba/articles/animate-a-sparkline
(() => {
const wks = app.ActiveSheet as Excel.Worksheet;
const oSparkGroup = wks.Cells.SparklineGroups.Item(1);
// Set the data source to the first year of data
oSparkGroup.ModifySourceData('B2:M4');
// Loop through the data points for the subsequent two years
for (let i = 1; i <= 24; i++) {
// Move the reference for the sparkline group over one cell
oSparkGroup.ModifySourceData(wks.Range(oSparkGroup.SourceData).Offset(0, 1).Address());
WScript.Sleep(1000);
}
})();
// animating a sparkline -- https://msdn.microsoft.com/en-us/vba/excel-vba/articles/animate-a-sparkline
{
const wks = app.ActiveSheet as Excel.Worksheet;
const oSparkGroup = wks.Cells.SparklineGroups(1);
// Set the data source to the first year of data
oSparkGroup.ModifySourceData('B2:M4');
// Loop through the data points for the subsequent two years
for (let i = 1; i <= 24; i++) {
// Move the reference for the sparkline group over one cell
oSparkGroup.ModifySourceData(wks.Range(oSparkGroup.SourceData).Offset(0, 1).Address());
WScript.Sleep(1000);
}
}
{
const formats = new VBArray(app.ClipboardFormats).toArray();
for (const format of formats) {
WScript.Echo(format);
}
}
}
+2148 -1759
View File
File diff suppressed because it is too large Load Diff
+5 -1
View File
@@ -1,3 +1,7 @@
{
"extends": "dtslint/dt.json"
"extends": "dtslint/dt.json",
"rules": {
"no-const-enum": false,
"max-line-length": false
}
}
-1
View File
@@ -51,7 +51,6 @@ interface Enumerator<T = any> {
interface EnumeratorConstructor {
new <T = any>(collection: { Item(index: any): T }): Enumerator<T>;
// new (collection: any): Enumerator;
}
declare var Enumerator: EnumeratorConstructor;
+2
View File
@@ -10,6 +10,8 @@
declare namespace Office {
type MsoRGBType = number;
type ByteArray = SafeArray<number>;
const enum BackstageGroupStyle {
BackstageGroupStyleError = 2,
BackstageGroupStyleNormal = 0,
-4
View File
@@ -5886,7 +5886,3 @@ interface EnumeratorConstructor {
new(col: Outlook.Reminders): Enumerator<Outlook.Reminder>;
new(col: Outlook.Views): Enumerator<Outlook.View>;
}
interface SafeArray<T = any> {
_brand: SafeArray<T>;
}