Csv Node.js Reference Documentation

Csv

Current Version: 11.5.0

Chilkat.Csv

Read, edit, sort, and write CSV or delimiter-separated data in memory.

Chilkat.Csv is a practical in-memory table class for working with CSV and other delimiter-separated text formats. It can load CSV data from files or strings, access cells by row/column index or column name, manage header rows, insert and delete rows or columns, sort records, search for matching rows, and write the result back using configurable delimiters, quoting rules, character encodings, and line endings.

In-memory CSV table

Load CSV data, modify cells, add rows, delete rows or columns, and write the updated table back to a file or string.

Header and named columns

Use a header row to access fields by column name instead of only by numeric column index.

Cell and row operations

Read and update individual cells, insert columns, remove data, find matching rows, and sort the table as needed.

Delimiter control

Work with comma-separated data or other delimiter-separated formats by configuring the delimiter character.

Quoting and output formatting

Control quoted fields, forced quoting, output line endings, explicit character encodings, and UTF-8 BOM emission.

Excel worksheet loading

Load worksheet data from an opened .xlsx ZIP container when CSV-style access to spreadsheet content is needed.

Common pattern: Load CSV data, specify whether the first row is a header, access or modify cells by index or column name, perform any row, column, search, or sort operations, then save the table using the delimiter, charset, quoting, BOM, and line-ending settings required by the target system.

Object Creation

var obj = new chilkat.Csv();

Properties

AutoTrim
AutoTrim
· boolean

Controls whether leading and trailing whitespace is removed from cell values returned by GetCell and GetCellByName.

  • When true, returned cell values are automatically trimmed.
  • When false, returned cell values are left unchanged.

top
Crlf
Crlf
· boolean

Controls the line endings used when the CSV is written by SaveFile, SaveFile2, or SaveToString.

  • When true, records are separated by CRLF line endings.
  • When false, records are separated by LF line endings.

top
DebugLogFilePath
DebugLogFilePath
· string

If set to a file path, this property logs the LastErrorText of each Chilkat method or property call to the specified file. This logging helps identify the context and history of Chilkat calls leading up to any crash or hang, aiding in debugging.

Enabling the VerboseLogging property provides more detailed information. This property is mainly used for debugging rare instances where a Chilkat method call causes a hang or crash, which should generally not happen.

Possible causes of hangs include:

  • A timeout property set to 0, indicating an infinite timeout.
  • A hang occurring within an event callback in the application code.
  • An internal bug in the Chilkat code causing the hang.

More Information and Examples
top
Delimiter
Delimiter
· string

Specifies the character used to separate fields within each CSV record. The default delimiter is a comma (,).

When loading CSV data, Chilkat automatically detects the delimiter if this property was not explicitly set. A semicolon (;) is commonly used in locales where a comma is used as the decimal separator.

Creating a new CSV: To use a delimiter other than a comma, set this property before adding rows or columns.

Vertical-bar delimiter: If this property is explicitly set to |, and EnableQuotes has not been explicitly set, EnableQuotes defaults to false. This allows quotation marks to be treated as ordinary characters in vertical-bar-delimited data.

top
EnableQuotes
EnableQuotes
· boolean
Introduced in version 9.5.0.71

Controls whether double quotation marks are interpreted as CSV field delimiters when reading or writing data.

When true, content enclosed in double quotation marks is treated as a single field, even when it contains delimiter characters, carriage returns, or line feeds. The default value is true.

More Information and Examples
top
EscapeBackslash
EscapeBackslash
· boolean
Introduced in version 9.5.0.44

Controls whether the backslash character (\) is treated as an escape character when reading or writing CSV data.

The default value is false.

top
HasColumnNames
HasColumnNames
· boolean

Indicates whether the first record contains column names rather than data.

Set this property to true before loading CSV data when the first record is a header row. Chilkat will then load the field names as column names and exclude the header row from the data-row count.

More Information and Examples
top
LastErrorHtml
LastErrorHtml
· string, read-only

Provides HTML-formatted information about the last called method or property. If a method call fails or behaves unexpectedly, check this property for details. Note that information is available regardless of the method call's success.

top
LastErrorText
LastErrorText
· string, read-only

Provides plain text information about the last called method or property. If a method call fails or behaves unexpectedly, check this property for details. Note that information is available regardless of the method call's success.

top
LastErrorXml
LastErrorXml
· string, read-only

Provides XML-formatted information about the last called method or property. If a method call fails or behaves unexpectedly, check this property for details. Note that information is available regardless of the method call's success.

top
LastMethodSuccess
LastMethodSuccess
· boolean

Indicates the success or failure of the most recent method call: true means success, false means failure. This property remains unchanged by property setters or getters. This method is present to address challenges in checking for null or Nothing returns in certain programming languages. Note: This property does not apply to methods that return integer values or to boolean-returning methods where the boolean does not indicate success or failure.

top
NumColumns
NumColumns
· integer, read-only

The number of columns in the first row of the CSV.

When HasColumnNames is true, the first row is the column-name row, and this property reports the number of column names.

top
NumRows
NumRows
· integer, read-only

The number of data rows in the CSV.

  • When HasColumnNames is true, the column-name row is not included in the count.
  • Whitespace-only lines following the last non-empty row are not included.

top
UncommonOptions
UncommonOptions
· string
Introduced in version 9.5.0.88

Provides optional keywords for uncommon CSV-processing requirements. This property should normally remain empty. To enable more than one option, use a comma-separated list of keywords.

The following keywords are defined:

  • QuotedCells (v9.5.0.96) — Encloses every cell value in double quotation marks when writing the CSV to a file or string.
  • QuotedColumnNames (v9.5.0.96) — Encloses every column name in double quotation marks when writing the CSV to a file or string.
  • EMIT_BOM (v9.5.0.93) — Writes a UTF-8 byte order mark (BOM) when saving the CSV to a UTF-8 encoded file.

top
VerboseLogging
VerboseLogging
· boolean

If set to true, then the contents of LastErrorText (or LastErrorXml, or LastErrorHtml) may contain more verbose information. The default value is false. Verbose logging should only be used for debugging. The potentially large quantity of logged information may adversely affect peformance.

top
Version
Version
· string, read-only

Version of the component/library, such as "10.1.0"

More Information and Examples
top
XlsxDateFormat
XlsxDateFormat
· string
Introduced in version 11.3.0

Specifies the date format used when converting date cells from an Excel .xlsx worksheet to CSV text.

The default format is mm-dd-yy. Other valid formats include d/m/yyyy, m/d/yyyy, and similar date-format patterns.

top

Methods

DeleteColumn
var status = DeleteColumn(index);
· Returns Boolean (true for success, false for failure).
· index Number

Deletes the column at zero-based index index. The first column is at index 0.

Returns true for success, false for failure.

More Information and Examples
top
DeleteColumnByName
var status = DeleteColumnByName(columnName);
· Returns Boolean (true for success, false for failure).
· columnName String

Deletes the column whose name is specified by columnName.

Returns true for success, false for failure.

More Information and Examples
top
DeleteRow
var status = DeleteRow(index);
· Returns Boolean (true for success, false for failure).
· index Number

Deletes the entire row at zero-based index index. The first data row is at index 0.

Returns true for success, false for failure.

More Information and Examples
top
GetCell
var retStr = GetCell(row, col);
· Returns a String.
· row Number
· col Number

Returns the contents of the cell at row row and column col.

Row and column indexes are zero-based. The upper-left data cell is at row 0, column 0.

Returns null on failure

More Information and Examples
top
GetCellByName
var retStr = GetCellByName(rowIndex, columnName);
· Returns a String.
· rowIndex Number
· columnName String

Returns the contents of the cell in row rowIndex and the column named by columnName.

Row indexing begins at 0.

Returns null on failure

top
GetColumnName
var retStr = GetColumnName(index);
· Returns a String.
· index Number

Returns the name of the column at zero-based index index. The first column is at index 0.

Returns null on failure

More Information and Examples
top
GetIndex
var retInt = GetIndex(columnName);
· Returns a Number.
· columnName String

Returns the zero-based index of the column named by columnName.

Returns -1 if no column has the specified name.

top
GetNumCols
var retInt = GetNumCols(row);
· Returns a Number.
· row Number

Returns the number of columns in the row at zero-based index row.

Returns 0 if row is beyond the available rows.

top
InsertColumn
var status = InsertColumn(index);
· Returns Boolean (true for success, false for failure).
· index Number
Introduced in version 9.5.0.89

Inserts a new empty column immediately before the column at zero-based index index. The first column is at index 0.

Returns true for success, false for failure.

More Information and Examples
top
LoadFile
var status = LoadFile(path);
· Returns Boolean (true for success, false for failure).
· path String

Loads CSV data from the file specified by path.

The file is interpreted using the ANSI character encoding.

Returns true for success, false for failure.

top
LoadFile2
var status = LoadFile2(filename, charset);
· Returns Boolean (true for success, false for failure).
· filename String
· charset String

Loads CSV data from the file specified by filename. charset specifies the character encoding used by the file.

See Supported Charsets for the character-encoding names recognized by Chilkat.

Returns true for success, false for failure.

More Information and Examples
top
LoadFromString
var status = LoadFromString(csvData);
· Returns Boolean (true for success, false for failure).
· csvData String

Loads CSV data from the in-memory string contained in csvData.

Returns true for success, false for failure.

More Information and Examples
top
RowMatches
var retBool = RowMatches(rowIndex, matchPattern, caseSensitive);
· Returns a Boolean.
· rowIndex Number
· matchPattern String
· caseSensitive Boolean

Compares the contents of the row at zero-based index rowIndex with the wildcard pattern in matchPattern.

  • The asterisk character (*) may appear any number of times and matches zero or more characters.
  • When caseSensitive is true, matching is case-sensitive. When caseSensitive is false, matching is case-insensitive.

Returns true when the row matches the pattern; otherwise returns false.

top
SaveFile
var status = SaveFile(path);
· Returns Boolean (true for success, false for failure).
· path String

Writes the CSV to the file specified by path.

The output file is written using the ANSI character encoding.

Returns true for success, false for failure.

top
SaveFile2
var status = SaveFile2(filename, charset);
· Returns Boolean (true for success, false for failure).
· filename String
· charset String

Writes the CSV to the file specified by filename. charset specifies the character encoding to use for the output file. Chilkat converts the CSV text to that encoding while saving.

See Supported Charsets for the character-encoding names recognized by Chilkat.

Returns true for success, false for failure.

More Information and Examples
top
SaveToSb
var status = SaveToSb(sb);
· Returns Boolean (true for success, false for failure).
· sb StringBuilder
Introduced in version 9.5.0.93

Clears sb and writes the entire CSV document into it.

The record separator is controlled by the Crlf property.

Returns true for success, false for failure.

top
SaveToString
var retStr = SaveToString();
· Returns a String.

Returns the entire CSV document as a string.

The record separator is controlled by the Crlf property.

Returns null on failure

top
SetCell
var status = SetCell(row, col, content);
· Returns Boolean (true for success, false for failure).
· row Number
· col Number
· content String

Sets the cell at row row and column col to the string in content. Row and column indexes are zero-based.

content may contain any characters, including CRLF line endings, double quotation marks, and delimiter characters. When the CSV is saved, Chilkat automatically quotes fields that require quoting. When CSV data is loaded, quoted and escaped fields are parsed automatically.

Returns true for success, false for failure.

top
SetCellByName
var status = SetCellByName(rowIndex, columnName, contentStr);
· Returns Boolean (true for success, false for failure).
· rowIndex Number
· columnName String
· contentStr String

Sets the cell in row rowIndex and the column named by columnName to the string in contentStr. Row indexing begins at 0.

contentStr may contain delimiter characters, quotation marks, and line endings. Chilkat applies the required CSV quoting when the data is saved.

Returns true for success, false for failure.

More Information and Examples
top
SetColumnName
var status = SetColumnName(index, columnName);
· Returns Boolean (true for success, false for failure).
· index Number
· columnName String

Sets the name of the column at zero-based index index to columnName. The first column is at index 0.

Returns true for success, false for failure.

More Information and Examples
top
SortByColumn
var status = SortByColumn(columnName, ascending, caseSensitive);
· Returns Boolean (true for success, false for failure).
· columnName String
· ascending Boolean
· caseSensitive Boolean

Sorts the CSV rows using the values in the column named by columnName.

  • When ascending is true, rows are sorted in ascending order.
  • When ascending is false, rows are sorted in descending order.
  • When caseSensitive is true, comparisons are case-sensitive.
  • When caseSensitive is false, comparisons are case-insensitive.

Returns true for success, false for failure.

top
SortByColumnIndex
var status = SortByColumnIndex(index, ascending, caseSensitive);
· Returns Boolean (true for success, false for failure).
· index Number
· ascending Boolean
· caseSensitive Boolean
Introduced in version 9.5.0.83

Sorts the CSV rows using the values in the column at zero-based index index.

  • When ascending is true, rows are sorted in ascending order.
  • When ascending is false, rows are sorted in descending order.
  • When caseSensitive is true, comparisons are case-sensitive.
  • When caseSensitive is false, comparisons are case-insensitive.

Returns true for success, false for failure.

top
XlsxGetSheets
var status = XlsxGetSheets(xlsx, sheetNames);
· Returns Boolean (true for success, false for failure).
· xlsx Zip
· sheetNames StringTable
Introduced in version 11.3.0

Gets the names of the worksheets contained in an Excel .xlsx spreadsheet.

  • xlsx is a Zip object containing the already-opened .xlsx file.
  • sheetNames receives the worksheet names.

Returns true for success, false for failure.

More Information and Examples
top
XlsxLoadSheet
var status = XlsxLoadSheet(zip, sheetName);
· Returns Boolean (true for success, false for failure).
· zip Zip
· sheetName String
Introduced in version 11.3.0

Loads a worksheet from an Excel .xlsx spreadsheet into this CSV object.

  • zip is a Zip object containing the already-opened .xlsx file.
  • sheetName is the name of the worksheet to load.
  • To load the first, or default, worksheet, pass an empty string in sheetName.

Returns true for success, false for failure.

top