//*** This code is copyright 2003-2004 by Gavin Kistner, !@phrogz.net //*** It is covered under the license viewable at http://phrogz.net/JS/_ReuseLicense.txt //*** Reuse or modification is free provided you abide by the terms of that license. //*** (Including the first two lines above in your source code mostly satisfies the conditions.) // Takes a single string stored in CSV format (as produced by MS Excel) // and returns an array of arrays representing the rows and columns. // // If the second (optional) parameter is passed a truth value, // the first row will be interpretted as column names and the columns in each // row will be able to be accessed by that property. // // This script properly handles commas, double-quotes and line-returns inside fields, // provided those fields are delimited with double-quotes. function ParseCSV(csv,useHeaders){ var row=[],rows=[row],rc=1,m,l,h,c=/([^,\n]*)([,\n])("?)/g,q=/((?:.|\n)*?[^"](?:"")*)"([,\n])("?)/g,re=c; csv.charAt(0)=='"' && (re=q) && (re.lastIndex=1); while (m=re.exec(csv)){ row[row.length]=m[1].replace(/""/g,'"'); l=re.lastIndex; (re=(m[3]=='"')?q:c).lastIndex=l; (m[2]=='\n') && (rows[rc++]=row=[]); } (l