I am using CFSPREADSHEET to read an XLS file into a query. Then reordering the columns as necessary. However, the query-of-query I am using retains alphabetical column order.
The XLS file has the fields in the correct order, but when converted to a query the fields are alphabetized.
I output the results to a CSV using this nifty UDF and the fields are still alphabetized as proof that the q-of-q is not adhering to my request.
In order to correct this issue I'm trying to use a q-of-q to reformat the columns in their proper order.
<cfspreadsheet action="read" src="#local.tmpPath##local.tmpFile#" excludeheaderrow="true" headerrow="1" sheet="1" query="local.qTemp" />
<!--- Change column order around as necessary --->
<cfquery name="local.qTemp" dbtype="query">
SELECT
Name, Address, City
FROM
[local].qTemp
</cfquery>
<cfset local.newCSV = QueryToCSV(
query = local.qTemp,
fields = local.qTemp.columnlist,
createHeaderRow = true,
delimiter = '|'
) />
<cffile action="write" charset="windows-1252" file="#local.tmpPath#foo.csv" output="#replace(local.newCSV, """", "", "ALL")#" nameconflict="overwrite" />
The column order when I output is:
Address,City,Name
Instead of the query order:
Name,Address,City
I read this post about the topic, but it didn't address my issue specifically (unless I am reading it wrong).
(From comments...)
The QoQ actually has nothing to do with the problem. The same issue would occur with the original query. Unfortunately, query.columnList
ALWAYS lists columns in alphabetical order. It is a long standing gripe of many CF dev's. To get the "real" order you must use a different technique such as:
query.getColumnNames()
method (OR)Build your own list from the GetMetaData(query) results
columns = [];
getMetaData(qry).each(function(elem,index){
arrayAppend(columns, elem.name);
});
writeDump(var=arrayToList(columns), label="Real Column Order");