Reputation: 75
I am trying to use <cfspreadsheet>
to read data from a spreadsheet and insert that data into a database table. Let's say my table has 10 possible columns: FName,LName,MName, Address1,Address2,Address3,city,state,zip and email
. Users will be able to submit data using Excel, but they don't have to submit data for all 10 columns. They could just submit data for FName,Email
or FName, LName, City, Email
. My spreadsheet code should be able to read only the columns submitted in their Excel file, instead of reading all the blank columns.
I thought by using the "columnnames" attribute of <cfspreadsheet>
I can instruct cfspreadsheet to just read data from those column names, unfortunately I was wrong OR I did it wrong?
Here is my cfspreadsheet code:
<cfspreadsheet action="read"
src="#Trim(PathToExcelFile)#"
columnnames="#Trim(Form.UserColumnList)#"
excludeheaderrow="true"
query="FindData">
</cfspreadsheet>
Upvotes: 0
Views: 872
Reputation: 28873
user3779216 wrote: ... apparently each institution (there are 9 of them) will have a different excel layout. I may be able to create a UI for the users to map col names from their excel to col names from my table, such as, a drop down of column names from excel and a drop down of column names from my table and let user select which col from excel match which col from my table.
Actually that would make it dead simple. Just feed an ordered list of columns into cfspreadsheet, the way you were originally doing, and the query column names will automatically be mapped correctly.
For example, if the user uploaded the layout below:
.. and selected this column order:
Feeding that list into cfspreadsheet produces a query with desired column mappings, which you can use as normal. (If needed, add code to detect and skip optional header names in the first row.)
<cfspreadsheet action="read"
src="#variables.pathToFile#"
columnNames="#form.OrderedColumnNames#"
query="sheetData" />
<cfdump var="#variables.sheetData#">
NB: Obviously the real code should include validation of the input list. Ensure it contains all of the required columns and was not tampered with to include invalid characters, etcetera.
Upvotes: 0