Class: CSVFile
The CSVFile is a helper class to load and process tabular data which is stored in a text file.
The type of delimiter is auto-detected from the first lines of the file. Possible delimeters are ;, ,, a tabulator ( or space(s).
Lines beginning with # are and empty lines are ignored. Quotes around (") values are removed. Both Linux () and Windows () line ending characters are supported.
All indices are 0-based (e.g., valid rows are 0..rowCount-1).
Example
var csv = new CSVFile(""); // do not load a file
csv.loadFromString('a;b;c\n1;2;3\n1;4;12.1'); // note the newline characters
console.log( csv.value(1,2) ); // -> 12.1
// change a value
csv.setValue(csv.rowCount-1,csv.columnIndex('a'), 100);
csv.saveFile(Globals.path('temp/test.csv'));
var csv = new CSVFile( Globals.path('temp/test.csv') );
console.log( csv.value(1,"c") ); // -> 12.1
console.log( csv.row(0) ); // get the full row (including delimiters)
// loop over all rows
for (var i=0;i<csv.rowCount;++i)
console.log( csv.value(i,0) );Properties Overview
| Name | Type | Description |
|---|---|---|
captions |
bool |
if true, then the first line of the input file are considered to be headers. Default: true. |
colCount |
integer |
The number of columns in the table (-1 if not loaded). |
flat |
bool |
if true, it is assumed that the file contains only one column (default: false). |
rowCount |
integer |
The number of rows in the table (-1 if not loaded). |
Methods Overview
| Method | Return Type | Description |
|---|---|---|
columnIndex(colName) |
integer |
Retrieve the index of the column with the name colName. The index can be used in later call to value(). The functions returns -1 if the column is not present. |
columnName(col) |
string |
Retrieve the column name with the index col. |
jsValue(row) |
value |
Retrieve the value of the column colNameand the row with the index row. |
jsValue(row) |
value |
Retrieve the value of the column with the index coland the row with the index row. The return value is a string value. The javascript built-in function Number can be used to force a conversion to a numerical value. |
loadFile(fileName) |
void |
Load a table from the file given in fileName. The delimiter is autodetected (see above). |
loadFromString(content) |
bool |
Load a table from a string. The delimiter is autodetected (see above). Use newline characters (\n) to separate lines. Captions |
saveFile(fileName) |
void |
Save the current state of the table to a file with the name fileName. |
setValue(row) |
void |
Change the value of the cell with in the column with the index coland the row with the index row. The new value can be either a numeric or a string. |
value(row) |
string |
Retrieve the value of the column colNameand the row with the index row. |
value(row) |
string |
Retrieve the value of the column with the index coland the row with the index row. The return value is a string value. The javascript built-in function Number can be used to force a conversion to a numerical value. |
Properties Details
captions
bool
if true, then the first line of the input file are considered to be headers. Default: true.
See also: [columnIndex()](csvfile.qmd#columnindex)colCount
integer
The number of columns in the table (-1 if not loaded).
flat
bool
if true, it is assumed that the file contains only one column (default: false).
rowCount
integer
The number of rows in the table (-1 if not loaded).
Methods Details
columnIndex(colName)
Returns: integer
Retrieve the index of the column with the name colName. The index can be used in later call to value(). The functions returns -1 if the column is not present.
Parameters:
colName(string): a column name @return {integer} the index of the given column, or -1 if the column is not available.
Return Value Description: the index of the given column, or -1 if the column is not available.
columnName(col)
Returns: string
Retrieve the column name with the index col.
Parameters:
col(integer): a valid column index (0<=index<colCount). @return {string} the column name
Return Value Description: the column name
jsValue(row)
Returns: value
Retrieve the value of the column colNameand the row with the index row.
Parameters:
row(integer): row index of the value to retrieve (0<=row<rowCount). @param {string} colName a column name @return {value} the value (either int, double, or string converted from the input)
Return Value Description: the value (either int, double, or string converted from the input)
jsValue(row)
Returns: value
Retrieve the value of the column with the index coland the row with the index row. The return value is a string value. The javascript built-in function Number can be used to force a conversion to a numerical value.
See also: [columnIndex()](csvfile.qmd#columnindex),[value()](csvfile.qmd#value)Parameters:
row(integer): row index of the value to retrieve (0<=row<rowCount). @param {integer} col column index of the requested value (0<=col<colCount). @return {value} the value as a Javascript value (int, double, or string)
Return Value Description: the value as a Javascript value (int, double, or string)
loadFile(fileName)
Returns: void
Load a table from the file given in fileName. The delimiter is autodetected (see above).
Parameters:
fileName(string): file to load (Use path() for using relative paths). @return { bool }trueon sucess
loadFromString(content)
Returns: bool
Load a table from a string. The delimiter is autodetected (see above). Use newline characters (\n) to separate lines. Captions
Parameters:
content(string): string that should be interpreted as a table. @return {bool}trueon sucess
Return Value Description: true on sucess
saveFile(fileName)
Returns: void
Save the current state of the table to a file with the name fileName.
See also: [loadFile()](csvfile.qmd#loadfile), [loadFromString()](csvfile.qmd#loadfromstring)Parameters:
fileName(string): file to be created or overwritten (Use path() for using relative paths). @return { bool }trueon sucess
setValue(row)
Returns: void
Change the value of the cell with in the column with the index coland the row with the index row. The new value can be either a numeric or a string.
See also: [value()](csvfile.qmd#value)Parameters:
row(integer): index of the value to retrieve (0<=row<rowCount). @param {integer} col column index of the requested value (0<=col<colCount). @param {variant} newValue new value (either a string or numeric).
value(row)
Returns: string
Retrieve the value of the column colNameand the row with the index row.
Parameters:
row(integer): row index of the value to retrieve (0<=row<rowCount). @param {string} colName a column name @return {string} the value (use Javascript’sNumber()to convert to a number)
Return Value Description: the value (use Javascript’s Number() to convert to a number)
value(row)
Returns: string
Retrieve the value of the column with the index coland the row with the index row. The return value is a string value. The javascript built-in function Number can be used to force a conversion to a numerical value.
See also: [columnIndex()](csvfile.qmd#columnindex),[jsValue()](csvfile.qmd#jsvalue)Parameters:
row(integer): row index of the value to retrieve (0<=row<rowCount). @param {integer} col column index of the requested value (0<=col<colCount). @return {string} the value (use Javascript’sNumber()to convert to a number)
Return Value Description: the value (use Javascript’s Number() to convert to a number)