Hi, everybody…
this is my first post!
i’m creating a datagrid to read and publish data coming from other users in a .csv format…
csv is like text (comes from excel), so the file is like:
AAAAA;BBBBB;100;200;CCCV
importing the data i create the array pushing in the strings, where all the fields are strings (infact 100 and 200 are exported as string and not as number, but i can’t change the csv files)
so, sorting the columns, i’m using NUMERIC but this doesn’t work…
i think i’ll be useful to refresh the data with a new array, like “AAAA”;“BBBB”;100;200;“CCCV”, knowing where are the numeric columns…
but i don’t know how (or maybe there’s a better solutions, the strings are about 2.000 with a dozen columns, so…)
this is the class i’m using to load the csv:
/*
PUBLIC METHODS:
onData() - Returns the raw data as a string.
onLoad()
parseCSV(rawData) - Takes the raw data as a string parameter and returns the data as an array
PUBLIC PROPERTIES:
columns [Array][Optional] - Default is to use the first row in the CSV file as the column headers
qualifier [String][Optional] - If left blank, no qualifier will be used
data [Array] - The data property is populated after the onLoad event has fired
*/
class CSV {
// Variable to hold the data after the CSV data is parsed
private var csvData:Array;
// Declare the method that the user can override to get a callback when the data is loaded and parsed
public var onLoad:Function;
// The user can define this array prior to parsing the CSV data to manually define the column headers
public var columns:Array;
// The user can define this is the CSV data is using a qualifier
public var qualifier:String = '';
function CSV() {}
private function removeQualifier(originalString:String, qualifier:String):String {
var modifiedString = originalString;
if(modifiedString.charAt(0) == qualifier) {
modifiedString = modifiedString.substring(1);
}
if(modifiedString.charAt(modifiedString.length - 1) == qualifier) {
modifiedString = modifiedString.substring(0, (modifiedString.length - 1));
}
return modifiedString;
}
public function load(csvPath:String):Void {
// Use the LoadVars class to load in the CSV file as a String
var csvLoad:LoadVars = new LoadVars();
csvLoad._parent = this;
// Overwrite the LoadVar.onData method to get the file's contents before its parsed into the LaodVars class
csvLoad.onData = function(rawData:String) {
// Pass the raw data to the onData method
this._parent.onData(rawData);
}
// Load the file based on the path passed in
csvLoad.load(csvPath);
}
public function onData(rawData:String):Void {
// Populate the private csvData parameter with the parsed CSV data
csvData = this.parseCSV(rawData);
// Call the onLoad method
this.onLoad();
}
// This method takes the raw CSV data as its parameter and returns the parsed data as an array
public function parseCSV(rawData:String):Array {
var ii:Number, columnArray:Array, rowObject:Object;
// Create an empty array to hold the parsed CSV data
var returnArray:Array = new Array();
// Determine the row delimiter for this file
var rowDelim:String = (rawData.indexOf('
‘) > -1) ? ’
’ : (rawData.indexOf(’\r’) > -1) ? ‘\r’ : ’
';
// Create the column delimiter based on the qualifier
var columnDelim:String = qualifier + ‘;’ + qualifier;
// Split the raw CSV data into an array of the rows
var rowsArray:Array = rawData.split(rowDelim);
// If the Column array is not pre-defined by the user then the parser will automatically use the first row as the column headers
if(!columns.length) {
// Use the first row of the CSV string as the column headers
columns = removeQualifier(rowsArray.shift().toString(), qualifier).split(columnDelim);
}
// Loop through the rows
for(var i:Number = 0; i < rowsArray.length; i++) {
// Split the row string into an array
columnArray = removeQualifier(rowsArray*.toString(), qualifier).split(columnDelim);
// Make sure this row has the same number of columns as the column header array
if(columnArray.length == columns.length) {
// Create an empty Object to hold the row’s column data
rowObject = new Object();
// Within the row we loop through the columns
for(ii = (columnArray.length - 1); ii >= 0; ii–) {
// Add the column data to the row object, using the column header as the object’s key
rowObject[columns[ii]] = columnArray[ii];
}
// We append the column object to the end of the main array
returnArray.push(rowObject);
}
}
return returnArray;
}
public function get data():Array {
return csvData;
}
}