Show:
Defined in: iLand\csvfie_doc.js:1
Module: iLand

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 (\t) or space(s).

Lines beginning with # are and empty lines are ignored. Quotes around (") values are removed. Both Linux (\n) and Windows (\r\n) 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) );

Methods

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.

Parameters:

  • colName String

    a column name

Returns:

Integer:

the index of the given column, or -1 if the column is not available.

columnName

(
  • col
)
String

Retrieve the column name with the index col.

Parameters:

  • col Integer

    a valid column index (0<=index<colCount).

Returns:

String:

the column name

jsValue

(
  • row
  • colName
)
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).

  • colName String

    a column name

Returns:

Value:

the value (either int, double, or string converted from the input)

jsValue

(
  • row
  • col
)
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,value

Parameters:

  • row Integer

    row index of the value to retrieve (0<=row<rowCount).

  • col Integer

    column index of the requested value (0<=col<colCount).

Returns:

Value:

the value as a Javascript value (int, double, or string)

loadFile

(
  • fileName
)
Bool

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).

Returns:

Bool:

true on sucess

loadFromString

(
  • content
)
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.

Returns:

Bool:

true on sucess

saveFile

(
  • fileName
)
Bool

Save the current state of the table to a file with the name fileName.

See also: loadFile, loadFromString

Parameters:

  • fileName String

    file to be created or overwritten (Use path for using relative paths).

Returns:

Bool:

true on sucess

setValue

(
  • row
  • col
  • newValue
)

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

Parameters:

  • row Integer

    index of the value to retrieve (0<=row<rowCount).

  • col Integer

    column index of the requested value (0<=col<colCount).

  • newValue Variant

    new value (either a string or numeric).

value

(
  • row
  • colName
)
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).

  • colName String

    a column name

Returns:

String:

the value (use Javascript's Number() to convert to a number)

value

(
  • row
  • col
)
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,jsValue

Parameters:

  • row Integer

    row index of the value to retrieve (0<=row<rowCount).

  • col Integer

    column index of the requested value (0<=col<colCount).

Returns:

String:

the value (use Javascript's Number() to convert to a number)

Properties

captions

Bool

if true, then the first line of the input file are considered to be headers. Default: true.

See also: 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).