Library to load and save CSV files to/from objects and ListView
Written using AutoHotkey v1.1+
By JnLlnd on AHK forum
A set of AutoHotkey (AHK) functions to load from CSV files, sort, display and save collections of records using the Object data type. Files can be read and saved in any delimited format (CSV, semi-colon, tab delimited, single-line or multi-line, etc.). Collections can also be displayed, edited and read in Gui ListView objects. For more info on CSV files, see http://en.wikipedia.org/wiki/Comma-separated_values.
Even if you don't know much about AHK objects, simply using these functions will help to:
- Transform a tab or semi-colon delimited CSV file to a straight coma-delimited file (any single character delimited is supported).
- Load a CSV file with multi-line fields (for example, Notes fields in a Google Contact or Outlook tasks export) and save it in a single line CSV file (with the end-of-line replacement character of your choice) that can be imported easily in Excel (see demo here).
- (with v1.00+) Merge existing fields in a new field
- Export the data from a CSV file to various formats: fixed-width, HTML or XML.
- (with v0.5+) Read and save file encodings: ANSI (default), UTF-8, UTF-16, UTF-8-RAW, UTF-16-RAW or CPnnnn (nnnn being a code page numeric identifier). See https://autohotkey.com/docs/commands/FileEncoding.htm
- Load a file to object to run any scripted manipulation on the content of the file with the ease and safety of AHK objets.
- Add/change CSV header names, change the order of fields or remove fields in a CSV file programmatically.
- Display the file content in a ListView for further viewing or editing; multiple Gui and ListView controls are supported (see how I've done that with CSVBuddy).
- Sort the data on any field combination before loading to the ListView or saving to a CSV file.
- Save all or selected rows of a ListView to a CSV file.
- Save to a file with or without header, with the fields delimiter and encapsulator of your choice.
ONLINE MATERIAL
- Home of this library is on GitHub: https://github.com/JnLlnd/ObjCSV
- The most up-to-date version of this AHK file on GitHub: https://raw.github.com/JnLlnd/ObjCSV/master/Lib/ObjCSV.ahk
- Online ObjCSV Library Help: http://jeanlalonde.ca/ahk/ObjCSV/ObjCSV-doc/
- Example of an application using ObjCSV: CSV Buddy: https://github.com/JnLlnd/CSVBuddy
- Demo and tutorial on how to use this library can be found here:
INSTRUCTIONS
Copy this script in a file named ObjCSV.ahk and save this file in one of these \Lib folders:
- %A_ScriptDir%\Lib\
- %A_MyDocuments%\AutoHotkey\Lib\
- [path to the currently running AutoHotkey_L.exe]\Lib\ (AutoHotKey_L can be downloaded here: http://l.autohotkey.net/
You can use the functions in this library by calling ObjCSV_FunctionName. No #Include required!
VERSIONS HISTORY
1.0.00 2022-07-18 New function ObjCSV_BuildMergeField allowing to copy or combine existing fields in a new field; new function ObjCSV_MergeSpecsError to validate merge specs syntax; merge fields support in ObjCSV_Collection2CSV, ObjCSV_CSV2Collection and ObjCSV_ReturnDSVObjectArray; support merged specs in file header and in strFieldNames. Note: changes in v1.0.00 are backward compatible.
0.5.9 2017-07-20 In ObjCSV_CSV2Collection, reverse change in v0.4.1 to import non-standard CSV files created by XL causing issue (stripping "=") in encapsulated fields with containing "...=""..."
0.5.8 2016-12-22 In ObjCSV_CSV2Collection, fix bug when creating "C" names header if blnHeader is false (0) and strFieldNames is empty.
0.5.7 2016-12-20 In ObjCSV_CSV2Collection, if blnHeader is false (0) and strFieldNames is empty, strFieldNames returns the "C" field names created by the function.
0.5.6 2016-10-20 Stop trimming data value read from CSV file. Addition of blnTrim parameter to ObjCSV_ReturnDSVObjectArray (true by default for backward compatibility).
0.5.5 2016-08-28 Optional parameter strEol to ObjCSV_Collection2CSV and ObjCSV_Collection2Fixed now empty by default. If not provided, end-of-lines character(s) are detected in value to replace. The first end-of-lines character(s) found is used for remaining fields and records.
0.5.4 2016-08-23 Add optional parameter strEol to ObjCSV_Collection2CSV and ObjCSV_Collection2Fixed to set end-of-line character(s) in fields when line-breaks are replaced.
0.5.3 2016-08-21 Fix bug with blnAlwaysEncapsulate in ObjCSV_Collection2CSV.
0.5.2 2016-07-24 Add an option to ObjCSV_Collection2CSV and blnAlwaysEncapsulate functions to force encapsulation of all values.
0.5.1 2016-06-06 In ObjCSV_CSV2Collection if the ByRef parameter is empty, the file encoding is returned only for UTF-8 or UTF-16 encoded files (no BOM) because other types (ANSI or UTF-n-RAW) files cannot be differentiated by the AHK engine.
0.5.0 2016-05-23 Read and save file encodings: ANSI (default), UTF-8, UTF-16, UTF-8-RAW, UTF-16-RAW or CPnnnn (nnnn being a code page numeric identifier). See https://autohotkey.com/docs/commands/FileEncoding.htm. No compatibibity break.
0.4.1 2014-03-05 Import files with equal sign before opening field encasulator to indicate text data or formula not to be interpreted as numeric when imported by XL (eg. ...;="12345";...). This is an XL-only CSV feature, not a standard CSV feature.
0.4.0 2013-12-29 Improved file system error handling (upgrade recommended). Compatibility breaker: review ErrorLevel codes only.
0.3.2 2013-11-27 Check presence of ROWS delimiters in HTML export template
0.3.1 2013-10-10 Fix ProgressStop missing bug, fix numeric column names bug
0.3.0 2013-10-07 Removed strRecordDelimiter, strOmitChars and strEndOfLine parameters. Replaced by ``r``n (CR-LF).
Compatibility breaker. Review functions calls for ObjCSV_CSV2Collection, ObjCSV_Collection2CSV, ObjCSV_Collection2Fixed,
ObjCSV_Collection2HTML, ObjCSV_Collection2XML, ObjCSV_Format4CSV and ObjCSV_ReturnDSVObjectArray
0.2.8 2013-10-06 Fix bug in progress start and stop
0.2.7 2013-10-06 Memory management optimization and introduction of ErrorLevel results
0.2.6 2013-09-29 Display progress using Progress bar or Status bar, customize progress messages, doc converted to GenDocs 3.0
0.2.5 2013-09-26 Optimize large variables management in save functions (2CSV, 2Fixed, 2HTML and 2XML), optimize progress bars refresh rates
0.2.2 2013-09-15 Export to fixed-width (ObjCSV_Collection2Fixed), HTML (ObjCSV_Collection2HTML) and XML (ObjCSV_Collection2XML)
0.1.3 2013-09-08 Multi-line replacement character at load time in ObjCSV_CSV2Collection
0.1.2 2013-09-05 Standardize boolean parameters to 0/1 (not True/False) and without double-quotes
0.1.1 2013-08-26 First release
The most up-to-date version of this AHK file can be found here:
https://raw.github.com/JnLlnd/ObjCSV/master/Lib/ObjCSV.ahk