Contact
Blog
Lab

jsCSV

Sometimes the simplicity of Comma Separated Value files is too good to pass up. While data formats on the web are plentiful, and some, like JSON are specifically designed to integrate into existing web technologies in as straight forward a manner as possible, there are always cases where simplicity, size, and utility win out.

jsCSV provides the tools to load, parse, search, rearrange, and build CSV documents in a fast, Object Oriented manner. Files can be loaded from a remote document (via AJAX), or parsed directly from a String. The resulting object contains methods for pulling apart the data, and finding the values you need quickly and with as little work as possible.

Features:
Using jsCSV in your own project is easy, just download the most recent version (see links above), and include JavaScript file in your page. Keep in mind that jsCSV depends upon the Prototype library, so you need to first include the Prototype library:
<head>
	<script src="prototype.js" type="text/javascript"></script>
	<script src="csv.js" type="text/javascript"></script>
</head>
Once you've loaded the library you can begin working with CSV documents and their contents:
<script type="text/javascript">
	csvp = new CsvReader({hasHeaders: true, mode: 'quoted'});
	csvp.from_url('http://www.example.com/files/test_quoted.csv',
		{
			onSuccess: displayCSV
		}
	);

	function displayCSV(acsv) {
		var text = "";
		
		// create a new document with only those rows where the column 'fname' contains 'aaa'
		csv = acsv.where(
			{
				'fname': 'aaa'
			}
		);
		
		// createa new document with only the 'fname' and 'lname' columns, and then iterate over
		// the rows of that document with the 'each' method.
		csv.cols('fname', 'lname').each(
			function (row) {
				text += 'f: ' + row.get('fname') + ', l: ' + row.get('lname') + "\n";
			}
		);
		$('csvArea').update(text);
	}
</script>
Home