Page 1 of 1

[Lib] ObjCSV Library - Load/save CSV files to Objects and LV

Posted: 30 Sep 2013, 15:28
by JnLlnd
ObjCSV Library
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
Other usages:
  • 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
   
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

Re: [Lib] ObjCSV Library - Load/save CSV files to Objects an

Posted: 29 Dec 2013, 16:32
by JnLlnd
v0.4.0 2013-12-29 Improved file system error handling (upgrade recommended). Compatibility breaker: review ErrorLevel codes only.

I found occasional file system errors occurring when saving files. These error were related to file locking not detected by ObjCSV. This new version 0.4 of the library improving error handling is now available on GitHub (raw source of ObjCSV.ahk).

This upgrade is recommended for data reliability. Compatibility issue: you will have to review your code only if you check the ErrorLevel codes returned by ObjCSV functions.

See the library online documentation for more info.

Re: [Lib] ObjCSV Library - Load/save CSV files to Objects and LV

Posted: 23 May 2016, 13:53
by JnLlnd
The library has been updated to v0.5 to support file encoding other that ANSI.

v0.5.0 2016-05-23
- Addition of file encoding optional parameter strFileEncoding to ObjCSV_CSV2Collection, ObjCSV_Collection2CSV, ObjCSV_Collection2Fixed, ObjCSV_Collection2HTML and ObjCSV_Collection2XML.
- In ObjCSV_CSV2Collection if the strFileEncoding ByRef parameter is empty, it is returned with the detected file encoding. If strFileEncoding is a literal value or a filled variable, the parameter is used to set reading encoding.
- File encoding supported: ANSI (default), UTF-8, UTF-16, UTF-8-RAW (No BOM), UTF-16-RAW (No BOM) or CPnnnn (nnnn being a code page numeric identifier
- See https://autohotkey.com/docs/commands/FileEncoding.htm

This version of the library is used in CSVBuddy v1.3+.

Library Quick Reference: ObjCSV Library

Posted: 23 May 2016, 14:08
by JnLlnd
Here is an updated version of Library Quick Reference: ObjCSV Library.

Library Quick Reference: ObjCSV Library
Author: By Jean Lalonde
Version: v0.5.0

ObjCSV_CSV2Collection(strFilePath, ByRef strFieldNames [, blnHeader = 1, blnMultiline = 1, intProgressType = 0, strFieldDelimiter = ",", strEncapsulator = """", strEolReplacement = "", strProgressText := "", ByRef strFileEncoding := ""])
ObjCSV_Collection2CSV(objCollection, strFilePath [, blnHeader = 0, strFieldOrder = "", intProgressType = 0, blnOverwrite = 0, strFieldDelimiter = ",", strEncapsulator = """", strEolReplacement = "", strProgressText = "", strFileEncoding := ""])
ObjCSV_Collection2Fixed(objCollection, strFilePath, strWidth [, blnHeader = 0, strFieldOrder = "", intProgressType = 0, blnOverwrite = 0, strFieldDelimiter = ",", strEncapsulator = """", strEolReplacement = "", strProgressText = "", strFileEncoding := ""])
ObjCSV_Collection2HTML(objCollection, strFilePath, strTemplateFile [, strTemplateEncapsulator = ~, intProgressType = 0, blnOverwrite = 0, strProgressText = "", strFileEncoding := ""])
ObjCSV_Collection2XML(objCollection, strFilePath [, intProgressType = 0, blnOverwrite = 0, strProgressText = "", strFileEncoding := ""])
ObjCSV_Collection2ListView(objCollection [, strGuiID = "", strListViewID = "", strFieldOrder = "", strFieldDelimiter = ",", strEncapsulator = """", strSortFields = "", strSortOptions = "", intProgressType = 0, strProgressText = ""])
ObjCSV_ListView2Collection([strGuiID = "", strListViewID = "", strFieldOrder = "", strFieldDelimiter = ",", strEncapsulator = """", intProgressType = 0, strProgressText = ""])
ObjCSV_SortCollection(objCollection, strSortFields [, strSortOptions = "", intProgressType = 0, strProgressText = ""])
ObjCSV_Format4CSV(strF4C [, strFieldDelimiter = ",", strEncapsulator = """"])
ObjCSV_ReturnDSVObjectArray(strCurrentDSVLine, strDelimiter = ",", strEncapsulator = """")

Re: [Lib] ObjCSV Library - Load/save CSV files to Objects and LV

Posted: 24 Jun 2017, 13:36
by JnLlnd
Hi! Just an update for this page about ObjCSV. The library has been updated a few times since my last post here:

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.

Re: [Lib] ObjCSV Library - Load/save CSV files to Objects and LV

Posted: 20 Jul 2017, 13:29
by JnLlnd
Hi! Another update for this reference page about ObjCSV. The library has been updated two times since my last post here:

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.

v0.5.9 solves an issue that I consider serious enough to recommend users to update their library if they process files with HTML or XML multi-line content.

Re: [Lib] ObjCSV Library - Load/save CSV files to Objects and LV

Posted: 25 Jan 2021, 02:40
by BoBo
That baby is now 7 years old and still amazing! Thx for sharing it :thumbup:

Re: [Lib] ObjCSV Library - Load/save CSV files to Objects and LV

Posted: 25 Jan 2021, 10:44
by JnLlnd
BoBo wrote:
25 Jan 2021, 02:40
That baby is now 7 years old and still amazing! Thx for sharing it :thumbup:
Thanks for the feed back, BoBo :-)

Re: [Lib] ObjCSV Library v1.0 announcement

Posted: 18 Jul 2022, 16:15
by JnLlnd
After close to ten years of usage in a production application (CSV Buddy) without issue, I think I can now stop tagging this library as "beta" and promote it to v1.0 production status. This new release has been developed to support the new "merge" features in CSV Buddy.

ObjCSV is a collection of AutoHotkey v1.1 (AHK) functions to load from CSV files, sort, display and save collections of records using the Object data type.
  • Read and save files in any delimited format (CSV, semi-colon, tab delimited, single-line or multi-line, etc.).
  • Merge existing fields in a new field
  • Display, edit and read Collections in GUI ListView objects.
  • Export Collection to fixed-width, HTML or XML files.
Summary of changes in v1.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.

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://code.jeanlalonde.ca/ahk/ObjCSV/ObjCSV-doc/

Example of an application using ObjCSV: CSV Buddy
https://github.com/JnLlnd/CSVBuddy