trcsvjs
A csv manager module for node and browser. This module wrapp papaparse library and add extra features.
See papaparse page for more information: https://www.npmjs.com/package/papaparse
Include/Declare
Include in Browser
<script type='text/javascript' src='PATH/TO/LIB/node_modules/papaparse/papaparse.min.js?'></script>
<script type='text/javascript' src='PATH/TO/LIB/trcsvjs.min.js?'></script>
Declare in NodeJs
npm install trcsvjs
var Trcsvjs = require('trcsvjs')
Creating a csv object
Library Trcsvjs implement a list of functions that allow to create a csv object
Trcsvjs.csvStringToJs(CSV_STRING[,config],callback)
Trcsvjs.getCsvFromFile(ELEMENT_FILE_FROM_DOM[,config],callback)
Trcsvjs.getCsvFromUrl(URL[,config],callback)
var csv = Trcsvjs.getEmptyCsv([config])
Config
Config use same parameters than papaparse. See papaparse page for more information: https://www.npmjs.com/package/papaparse
Usage of CSV object
After you create a csv object, you can manage it using its function. All reference to position for column and row start at 1..n
csv.initCsvFromArray(array);
var value = csv.getCell(pos, col);
var value = csv.getCell(pos, col);
csv.setCell(pos, col, value);
var text = csv.getCsvText(config);
csv.browseCsv(filename, config);
var row = csv.getRow(pos);
var obj = csv.getObjFromRow(pos, objspec);
var row = csv.getRowFromObj(pos, objspec);
csv.insertRow(row);
csv.insertRowInPos(row, pos);
csv.insertRowFromObj(obj, objspec);
csv.insertRowInPosFromObj(obj, pos, objspec);
Object Spec
You can create easly an object from a row using an object spec. An object spec is like:
{
"name_field_1":CONF_FIELD,
"name_field_2":CONF_FIELD,
}
If you have a row like ['foo','bar'] and convert in object using previous spec you receive an object:
{
"name_field_1" : 'foo',
"name_field_2" : 'bar'
}
It also accept deep objects, for example:
{
"a" : {
"a1" : {
"a11" : CONF_FIELD,
"a12" : CONF_FIELD
},
"a2" : CONF_FIELD
},
"b" : "CONF_FIELD
}
convert row [1,2,3,4] in:
{
"a" : {
"a1" : {
"a11" : 1,
""a12" : 2
},
"a2" : 2
},
"b" : "3
}
Also, if you use object spec to convert an object in array (row) the process is exactly the opposite
CONF_FIELD
Conf field is a special configuration that allow to automaticaly implement typical process to a value before converting to object/array. Valid values are:
- '': //Do nothing, pass throught value
- 'd': //Convert value from row in an integer and viceversa
- 'dz': //Convert value from row in an integer and viceversa. If value of cell is empty, assign 0
- 'f': //Convert value from row in a float and viceversa
- 'fz': //Convert value from row in a float and viceversa. If value of cell is empty, assign 0. Also detect ',' and convert in '.'
- 'dt': //Detect d/m/Y spanish and convert to Y-m-d
- 'b': Convert 1/0 values from excel into true/false in JS and viceversa
- 'sc': Implement a separated comma array. For example a value in cell like 1,2,3 is converted to [1,2,3] and viceversa