jtpereyda
jtpereyda

Reputation: 7395

ColdFusion Query -- Get index of column by column name

I have a column name and, for the sake of SpreadsheetSetCellFormula, I want to get the index of that column from its name. This is important for expandability, as columns may be added or taken away in the future.

When I use queryName.ColumnList, ColdFusion automatically alphabetizes the list. However, passing it into SpreadsheetAddRows dumps the columns in original order. How can I get the index of a column from its name?

Upvotes: 0

Views: 1845

Answers (3)

marta.joed
marta.joed

Reputation: 366

This doesn't directly answer your question, however this may be VERY useful in your situation (I have used it in a similar situation)

This is taken from: http://existdissolve.com/2010/11/quick-coldfusion-goodness/

columns = arrayToList(myquery.getMeta().getcolumnlabels())

gives you a list of the columns in their original order, with their original case sensitivity (not all upper case)

Upvotes: 2

Mark
Mark

Reputation: 2562

<!--- get the column list, in the original order, as a coldfusion compatible array --->
<cfset variables.columnArray = createObject("java","java.util.Vector").init(
   createObject("java","java.util.Arrays").asList(
      query.getColumnList()
   )
)/>
<!--- get the index of the column. note that this is case sensitive. --->
<cfset variables.myColumnIndex = variables.columnArray.indexOf("MY_COLUMN")/>

No looping required.

Upvotes: 4

Sean Coyne
Sean Coyne

Reputation: 3884

You can use the getMetaData() function, which you can read about here. This function will return an array of Columns in the correct order, including some other information. You would use it as follows:

getMetaData(queryName)

Upvotes: 1

Related Questions