问题
I have a script that is reading CSV files from clients and is splitting the values into variables that will later on enter the database.
This is the error I am getting when a CSV comes in with less columns than the required minimum.
In function ListGetAt(list, index [, delimiters]), the value of index, 11, is not a valid as the first argument (this list has 10 elements). Valid indexes are in the range 1 through the number of elements in the list.
Expression
Invalid list index 11.
This is a sample of the code
<cfscript>
csvData = csvloaderCFC.loadCSVfile(csvfilename);
</cfscript>
<cfset i=0>
<!--- loop CSV Data ---->
<cfloop index="line" list="#csvData#" delimiters="#chr(10)##chr(13)#">
<!--- Ignore 1st Row ---->
<cfif i EQ 0>
<cfset i++>
<cfscript>
continue;
</cfscript>
</cfif>
<!--- Split CSV file --->
<cfscript>
line = csvloaderCFC.listfix(line);
ClientBrandID = (listgetAt('#line#',1) EQ 'NULL')?'':listgetAt('#line#',1);
SurveyType = (listgetAt('#line#',2) EQ 'NULL')?'':listgetAt('#line#',2);
Location= (listgetAt('#line#',3) EQ 'NULL')?'':listgetAt('#line#',3);
ClientContactID = (listgetAt('#line#',4) EQ 'NULL')?'':listgetAt('#line#',4);
FirstName= (listgetAt('#line#',5) EQ 'NULL')?'':listgetAt('#line#',5);
LastName= (listgetAt('#line#',6) EQ 'NULL')?'':listgetAt('#line#',6);
HomePhone= (listgetAt('#line#',7) EQ 'NULL')?'':listgetAt('#line#',7);
WorkPhone= (listgetAt('#line#',8) EQ 'NULL')?'':listgetAt('#line#',8);
CellPhone= (listgetAt('#line#',9) EQ 'NULL')?'':listgetAt('#line#',9);
Email = (listgetAt('#line#',10) EQ 'NULL')?'':listgetAt('#line#',10);
BirthDate= (listgetAt('#line#',11) EQ 'NULL')?'':listgetAt('#line#',11);
Zip= (listgetAt('#line#',12) EQ 'NULL')?'':listgetAt('#line#',12);
Gender= (listgetAt('#line#',13) EQ 'NULL')?'':listgetAt('#line#',13);
InquiryDate= (listgetAt('#line#',14) EQ 'NULL')?'':listgetAt('#line#',14);
ScheduledBy= (listgetAt('#line#',15) EQ 'NULL')?'':listgetAt('#line#',15);
ConsultServiceType= (listgetAt('#line#',16) EQ 'NULL')?'':listgetAt('#line#',16);
ConsultDate= (listgetAt('#line#',17) EQ 'NULL')?'':listgetAt('#line#',17);
ConsultantName= (listgetAt('#line#',18) EQ 'NULL')?'':listgetAt('#line#',18);
ServiceType= (listgetAt('#line#',19) EQ 'NULL')?'':listgetAt('#line#',19);
ServiceDate= (listgetAt('#line#',20) EQ 'NULL')?'':listgetAt('#line#',20);
ServiceProviderName= (listgetAt('#line#',21) EQ 'NULL')?'':listgetAt('#line#',21);
ServiceRevenue= (listgetAt('#line#',22) EQ 'NULL')?'':listgetAt('#line#',22);
LeadSource= (listgetAt('#line#',23) EQ 'NULL')?'':listgetAt('#line#',23);
</cfscript>
<!--- SQL Code begins here --->
I need to modify my code to be able to process smaller files regarding if they include all of the columns or not because those are valuable information as well.
回答1:
Stop using ListGetAt - this is not designed for the CSV file format - and instead use a proper CSV parser, such as OpenCSV.
Here's some sample code:
<cfscript>
fileReader = createobject("java","java.io.FileReader");
fileReader.init("c:\thefile.csv");
csvReader = createObject("java","au.com.bytecode.opencsv.CSVReader");
csvReader.init(fileReader, ",");
ArrayData = csvReader.readAll()
</cfscript>
( Taken from: http://www.bennadel.com/blog/1903-Parsing-CSV-Data-With-ColdFusion-s-CFHTTP-Tag.htm#comments_26608 )
回答2:
I prefer OpenCSV and it is the fastest in the tests I've performed. (That's my comment on Ben's blog)
If you require a ColdFusion-only solution, check out Ben's array method:
http://www.bennadel.com/blog/2041-UPDATE-Parsing-CSV-Data-Files-In-ColdFusion-With-csvToArray-.htm
It's not as fast as the dedicated Java class, but the end result is the same.
来源:https://stackoverflow.com/questions/18026591/listgetat-issues-when-parsing-csv-file