CSVFile Class
iLand\csvfie_doc.js:1
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) );
Item Index
Methods
Methods
columnIndex
-
colName
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
Stringa column name
Returns:
the index of the given column, or -1 if the column is not available.
columnName
-
col
Retrieve the column name with the index col
.
Parameters:
-
col
Integera valid column index (0<=index<colCount).
Returns:
the column name
jsValue
-
row
-
colName
Retrieve the value of the column colName
and the row with the index row
.
Parameters:
-
row
Integerrow index of the value to retrieve (0<=row<rowCount).
-
colName
Stringa column name
Returns:
the value (either int, double, or string converted from the input)
jsValue
-
row
-
col
Retrieve the value of the column with the index col
and 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
Integerrow index of the value to retrieve (0<=row<rowCount).
-
col
Integercolumn index of the requested value (0<=col<colCount).
Returns:
the value as a Javascript value (int, double, or string)
loadFile
-
fileName
Load a table from the file given in fileName
. The delimiter is autodetected (see above).
Parameters:
-
fileName
Stringfile to load (Use path for using relative paths).
Returns:
true
on sucess
loadFromString
-
content
Load a table from a string. The delimiter is autodetected (see above). Use newline characters (\n
) to separate lines. Captions
Parameters:
-
content
Stringstring that should be interpreted as a table.
Returns:
true
on sucess
saveFile
-
fileName
Save the current state of the table to a file with the name fileName
.
See also: loadFile, loadFromString
Parameters:
-
fileName
Stringfile to be created or overwritten (Use path for using relative paths).
Returns:
true
on sucess
setValue
-
row
-
col
-
newValue
Change the value of the cell with in the column with the index col
and the row with the index row
. The new value can be either a numeric or a string.
See also: value
Parameters:
-
row
Integerindex of the value to retrieve (0<=row<rowCount).
-
col
Integercolumn index of the requested value (0<=col<colCount).
-
newValue
Variantnew value (either a string or numeric).
value
-
row
-
colName
Retrieve the value of the column colName
and the row with the index row
.
Parameters:
-
row
Integerrow index of the value to retrieve (0<=row<rowCount).
-
colName
Stringa column name
Returns:
the value (use Javascript's Number()
to convert to a number)
value
-
row
-
col
Retrieve the value of the column with the index col
and 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
Integerrow index of the value to retrieve (0<=row<rowCount).
-
col
Integercolumn index of the requested value (0<=col<colCount).
Returns:
the value (use Javascript's Number()
to convert to a number)
Properties
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).