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

Post your working scripts, libraries and tools
User avatar
JnLlnd
Posts: 212
Joined: 29 Sep 2013, 21:29
Location: Montreal, Quebec, Canada

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

30 Sep 2013, 15:28

ObjCSV Library
Library to load and save CSV files to/from objects and ListView

Written using AutoHotkey_L v1.1.09.03+ (http://l.autohotkey.net/)
By JnLlnd on AHK forum

A set of AutoHotkey_L (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).
  • 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 programatically.
  • 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
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
Last edited by JnLlnd on 20 Jul 2017, 13:28, edited 4 times in total.
User avatar
JnLlnd
Posts: 212
Joined: 29 Sep 2013, 21:29
Location: Montreal, Quebec, Canada

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

29 Dec 2013, 16:32

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.
Author of freeware apps Quick Access Popup ( http://www.quickaccesspopup.com ),
FoldersPopup and CSV Buddy ( http://code.jeanlalonde.ca )
User avatar
JnLlnd
Posts: 212
Joined: 29 Sep 2013, 21:29
Location: Montreal, Quebec, Canada

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

23 May 2016, 13:53

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+.
Author of freeware apps Quick Access Popup ( http://www.quickaccesspopup.com ),
FoldersPopup and CSV Buddy ( http://code.jeanlalonde.ca )
User avatar
JnLlnd
Posts: 212
Joined: 29 Sep 2013, 21:29
Location: Montreal, Quebec, Canada

Library Quick Reference: ObjCSV Library

23 May 2016, 14:08

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 = """")
Author of freeware apps Quick Access Popup ( http://www.quickaccesspopup.com ),
FoldersPopup and CSV Buddy ( http://code.jeanlalonde.ca )
User avatar
JnLlnd
Posts: 212
Joined: 29 Sep 2013, 21:29
Location: Montreal, Quebec, Canada

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

24 Jun 2017, 13:36

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.
Author of freeware apps Quick Access Popup ( http://www.quickaccesspopup.com ),
FoldersPopup and CSV Buddy ( http://code.jeanlalonde.ca )
User avatar
JnLlnd
Posts: 212
Joined: 29 Sep 2013, 21:29
Location: Montreal, Quebec, Canada

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

20 Jul 2017, 13:29

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.
Author of freeware apps Quick Access Popup ( http://www.quickaccesspopup.com ),
FoldersPopup and CSV Buddy ( http://code.jeanlalonde.ca )

Return to “Scripts and Functions”

Who is online

Users browsing this forum: denyerec and 11 guests