Posts

TypeScript module install

To install a module for use in TypeScript ('lodash' here as an example): Install 'lodash': npm i lodash If the node_modules folder is not created then you should create a packages.json file first: npm init -y Install types: npm i -D @types/lodash Then you can use the module: import * as _ from 'lodash'; Note, that it is best to set the TypeScript settings first (tsconfig.json). A great guide here.

TypeScript settings

Default TypeScript settings (file tsconfig.json) { "compilerOptions": { "target": "esnext", "watch": true, "lib":["DOM","es2017"], "moduleResolution": "node" } }

Update npm

To update npm: sudo npm -g update npm

Install node.js

Clear NPM's cache: sudo npm cache clean -f Install a little helper called 'n' sudo npm install -g n Install latest stable Node.js version sudo n stable Alternatively pick a specific version and install like this: sudo n 0.8.20

Quick expression parsing tip

Image
In Windows, you can avoid "manual" parsing of arithmetic expressions by using the Microsoft Script Control COM object. Although it is a bit dangerous to run unsupervised scripts, it is a very handy quick way to parse any numerical expression string. It is recommended if the string is created/used in your program internally. It is NOT recommended if the "script string" is provided outside the program and you cannot control its content. All you have to do is reference the COM library Microsoft Script Control 1.0 . Then write the following under a click event -for example- to test the parsing immediately! MSScriptControl.ScriptControl c = new MSScriptControl.ScriptControl(); c.Language = "VBScript"; double v = c.Eval("5+6*8"); MessageBox.Show(v.ToString()); You can use similar code in other languages that supports COM such as Visual Basic (including VBA).

Get a list of files in VBA

Image
There are two ways to get a list of files inside a folder with VBA. The first one is the Microsoft Scripting Library way, where the FileSystemObject is used. The second one is the "traditional" Dir command which returns a file each time the function is called. 'the scripting lib way Public Function GetListOfFiles(path As String, ParamArray Extensions()) As String() Dim fs As New FileSystemObject Dim fld As Folder: Set fld = fs.GetFolder(path) Dim fl As file Dim list() As String, count As Long: count = 0 For Each fl In fld.files Dim ext As String, extIncluded As Boolean, i As Long ext = fs.GetExtensionName(fl.name) extIncluded = False For i = 0 To UBound(Extensions) If LCase(ext) = LCase(Extensions(i)) Then extIncluded = True Exit For End If Next 'If ext = "xls" Or ext = "xlsb" Or ext = "xlsx" Or ext = "xlsm" Then

Automate Excel using C#

Image
Excel can be easily automated using C#. At first you must check that the Microsoft.Office.Interop.Excel is referenced. When the Framework 4.0 is targeted you should check that the Microsoft.CSharp.dll is referenced too. using System; using Excel = Microsoft.Office.Interop.Excel; //... Excel.Application xlApp = new Excel.Application(); //_Application xlApp = new Excel.Application(); xlApp.Visible = true; //create a workbook Workbook wb = xlApp.Workbooks.Add(XlWBATemplate.xlWBATWorksheet); //or open a workbook Workbook wb = xlApp.Workbooks.Open("c:\\book.xlsx"); //get the first worksheet Worksheet ws = (Worksheet)wb.Worksheets[1]; //Worksheets["Sheet1"] //create a worksheet Worksheet ws2 = (Worksheet)wb.Worksheets.Add(); //retrieve a range Range range = (Range)ws.Range["a1"]; Range range2 = (Range)ws.Cells[1, 1]; //the A1 cell range.Value="arkoudaki"; //alternative way to do the same thing //range2.set_Value(XlRangeValueDataType.xlRangeValueD