How to get excel sheet column/row value? - coldfusion

I have a excel sheet
<cfspreadsheet action="read" src="test.xlsx" sheet=1 rows="2-7" format="html" name="csvData">
How would i get the value of specic column and output the value?
For example:
Actual
testtet test 9040416.9299998
test test tettest 6406129.51
I would like to get column 2, row 3 which is the value of 6406129.51.

This is how you can get the value of specific rows and column form excel.
<cfspreadsheet action="read" src="test.xlsx" rows="2-7" sheet="1" query="test"/>
<cfdump var="#test["col_2"][3]#">

Related

problem creating a select using enum values

i have mysql table and has a data like this
i have a table with two columns value and label and both the columns have the enum type
Data of value is:
-43200|-39600|-36000|-32400|-28800|-25200|-21600|-18000|-14400|-10800|-7200|-3600|0|3600|7200|10800|14400|18000|21600|25200|28800|32400|36000|39600|43200|46800
Data of Label is:
GMT-12:00|GMT-11:00|GMT-10:00|GMT-09:00|GMT-08:00|GMT-07:00|GMT-06:00|GMT-05:00|GMT-04:00|GMT-03:00|GMT-02:00|GMT-01:00|GMT|GMT+01:00|GMT+02:00|GMT+03:00|GMT+04:00|GMT+05:00|GMT+06:00|GMT+07:00|GMT+08:00|GMT+09:00|GMT+10:00|GMT+11:00|GMT+12:00|GMT+13:00
but as i have two columns one is for the value and other is for the label, how can i create a function which will create a select having a option value and text and create a dropdown,
if its a single column, i could have done a valuelist of valuearray and loop over it, but now its two columns, i am bit confused how to
any clue
You already know how to do this, you're just not thinking about it in context.
Two columns, two lists (or arrays).
You know you can loop over "a" list to create HTML.
So loop over BOTH lists to create HTML.
Reference each list item by position and you're good to go.
<cfset sValuesList = "-43200|-39600|-36000|etc.">
<cfset sLabelsList = "GMT-12:00|GMT-11:00|GMT-10:00|etc.">
<cfoutput>
<select>
<cfif listLen(sValuesList, "|") EQ listLen(sLabelsList, "|")>
<cfloop from="1" to="#listLen(sValuesList, "|")#" index="x">
<option value="#listGetAt(sValuesList, x, "|")#">
#listGetAt(sLabelsList, x, "|")#
</option>
</cfloop>
</cfif>
</select>
</cfoutput>

How can I get particular Row in a query variable using ColdFusion?

Take the following query example:
<cfquery name="Test" Datasource = "TestDB">
Select * from Table_Test
</cfquery>
Assume that the "Test" query returns 10 rows. I want to show single row on current time.
Note: I do not want to change the SQL statement.
If you know your row number, Test.columnName[RowNumber] will show you the value of the columnName in specified row number.
If you want one random row from the query:
<cfset start = randRange(1, Test.recordCount)>
<cfoutput>
#Test.name[start]# #Test.email[start]#<br>
</cfoutput>
No need to loop.
NOTE: It is more efficient to modify the query to get a random row.
How to request a random row in SQL?

How to add particular columns of a query to spreadsheet object in Coldfusion

I have a query object which contains 5 columns, but I only need to add 4 columns to the spreadsheet. When I am using <cfset spreadSheetAddRows(spreadSheetObj,qryObj)> it is adding all the columns to the spreadsheet. I cannot remove the 5th column from the query as it is required for some other purpose.
So How can I add only 4 columns from the query to spreadsheet?
Ex: My query contains 4 columns like "Id,Name,Roll,CGPA"
But my Excel sheet should contain only "Id,Name,Roll".
How to do this?
You could do a Query-of-Query to create a temporary query that you can then use to pump into your spreadSheetAddRows().
<cfquery name="local.spreadsheetQuery" dbtype="query">
SELECT Id,Name,Roll
FROM variables.originalQuery
</cfquery>
Then use <cfset spreadSheetAddRows(spreadSheetObj,local.spreadsheetQuery)> instead of <cfset spreadSheetAddRows(spreadSheetObj,variables.originalQuery)>.
Just add each row individually with SpreadsheetAddRow(spreadsheetObj, data [,row, column, insert]);
And for columns use SpreadsheetAddColumn(SpreadsheetObj, data[, startRow, startColumn, insert]);
SpreadsheetAddRow Documentation
SpreadsheetAddColumn Documentation

Database column undefined when dumped individually - ColdFusion

I have a query that returns DATE - ID - NAME - NOTE - TIME when I dump it by using <cfdump var="#q_data#">
I am trying to dump only the value DATE by using <cfdump var="q_data.date"> but it returns Element DATE is undefined in DATA.
This is my code:
<cfspreadsheet action="read" src="#directory#\#q_oBandDirectory.name#" query="q_data" headerrow="1" excludeHeaderRow = "true" columns = "1-5" >
<cfdump var="#q_data.date#"> <-- Does not work
<cfdump var="#q_data.ID#"> <--- Works as it should
<cfdump var="#q_data.NAME#"> <--- Works as it should
Is there anything special that I have to do in this situation because the query is created from a spread sheet tag?

Extract data from excel and create a custom CSV - ColdFusion

I am receiving excel files from our client and I need to create a csv file from them. My issue is that the values in the excel files do not match our standards to create the csv file just by copying it. I need to get the values and put them in the correct order while I'm creating the file. I read through the livedocs for CF but I couldn't find anything the worked.
I was thinking to create a struct and pull the data out in it and then create the csv file based on my struct but I haven't done anything like this before and I am not sure if it is even possible. I also thought of using listGetAt instead of the struct but still haven't tried it.
Anyone did something like this before? I'm trying to hardcode as less values as possible because I see this becoming a future problem if we get a second client with the same issue.
Update (I changed my code around a lot the past few days so this is what I have at the moment)
<cfset DataDirectory = "E:\testfolder\test.xlsx">
<cfspreadsheet action="read" src="#DataDirectory#" excludeHeaderRow="true" headerrow="1" query="queryData">
<cfquery name="contact" datasource="#ds#">
select ClientBrandID
from ClientBrands
where surveyReferralID IN
(select surveyReferralID from clientAdmin where username = '#res#')
</cfquery>
<cfscript>
dataFile = structNew();
StructInsert(datafile,"ClientBrandID",contact.ClientBrandID);
StructInsert(datafile,"surveyType", queryData.surveyType);
</cfscript>
<cfscript>
///We need an absolute path, so get the current directory path.
theFile= "E:\testfolder\NewTest.csv";
//Create a new Excel spreadsheet object and add the query data.
theSheet = SpreadsheetNew("PatientData");
SpreadsheetAddRow(theSheet, "ClientBrandID,SurveyType,Location,ClientContactID,FirstName,LastName,HomePhone,WorkPhone,CellPhone,Email"); <!---This is the header of the new CSV --->
SpreadsheetAddRows(theSheet, "datafile.clientbrandid, datafile.surveytype"); <!--- This is my latest failed attempt. Tried to pass in data from my struct, probably the quotes are the issue but haven't tried it without them --->
</cfscript>
<!--- Write the spreadsheet to a file, replacing any existing file. --->
<cfspreadsheet action="write" filename="#theFile#" format="csv" name="theSheet" overwrite=true >
All of these operations will need to be in a cfloop in order to go through all of the files in my test folder. Right now I am hardcoding a single file to fix the issue at hand before I do anything else. Also, I am missing another loop in between that will need to go through all the values of the file. It should be something like <cfloop query='queryData'> which is the query that Im getting from cfspreadsheet.
You could make it accept any order but you'll still need to hard code the expected column headers into an array or something and considering it is just for your internal use I would just add the data back to the CSV in the order it's supposed to be in. Last time I made a spreadsheet from a query I did it like this. Perhaps it will help.
<cfscript>
sMySpreadSheet = spreadsheetNew();
column = 1;
<!--- header row --->
<!--- Remember what is expected from SpreadsheetSetCellValue --->
<!--- SpreadsheetSetCellValue(spreadsheetObj, value, row, column) --->
spreadsheetSetCellValue(sMySpreadSheet ,"First Name",1,column); column++;
spreadsheetSetCellValue(sMySpreadSheet ,"Last Name",1,column); column++;
spreadsheetSetCellValue(sMySpreadSheet ,"DOB",1,column); column++;
spreadsheetSetCellValue(sMySpreadSheet ,"Phone Number",1,column); column++;
spreadsheetSetCellValue(sMySpreadSheet ,"Number of Kids",1,column); column++;
<!--- data rows --->
<!--- if you're not using a header row with real titles you can just use the
default col_x
example: spreadsheetSetCellValue(sMySpreadSheet , queryData.col_1[q], q+1, column); column++; --->
for(q=1; q LTE queryData.recordCount; q++){
column = 1;
spreadsheetSetCellValue(sMySpreadSheet , queryData.first[q], q+1, column); column++;
spreadsheetSetCellValue(sMySpreadSheet , queryData.last[q], q+1, column); column++;
spreadsheetSetCellValue(sMySpreadSheet , queryData.dob[q], q+1, column); column++;
spreadsheetSetCellValue(sMySpreadSheet , queryData.phoneNumber[q], q+1, column); column++;
spreadsheetSetCellValue(sMySpreadSheet , queryData.kidCount[q], q+1, column);
}
<!--- make it purdy (optional) --->
spreadsheetFormatRow(queryData, {fgcolor="light_cornflower_blue"},1);
</cfscript>
If you want to simply add to a CSV you can do something like this (wrap your stuff in " if you have them qualified):
<cfsetting enableCFoutputOnly = "Yes">
<cfsaveContent variable = "myCSV">
<cfset newline = #chr(13)#&#chr(10)#>
<cfoutput>First Name,Last Name,DOB,Phone Number,Number of Kids#newline#</cfoutput>
<cfoutput query="queryData">#queryData.first#,#queryData.last#,#queryData.dob#,#queryData.phoneNumber#,#kqueryData.idCount##newline#/cfoutput>
</cfsaveContent>
</cfsetting>
<cffile action = "append" file = "[your file]" output = "#myCSV#">
If cfsavecontent causes white spaces issues and cfsetting doesn't help, here is another alternative.
<cfset myCSV = "First Name,Last Name,DOB,Phone Number,Number of Kids#newline#">
<cfloop query="queryData">
<cfset myCSV &= "#queryData.first#,#queryData.last#,#queryData.dob#,#queryData.phoneNumber#,#queryData.kidCount##newline#">
</cfloop>
Here are some suggestions based on the edited question. I'm making them even though the edited question does not make it clear what you are trying to achieve.
Regarding, "All of these operations will need to be in a cfloop in order to go through all of the files in my test folder", you can use cfdirectory on that folder. It will return a ColdFusion query through which you can loop.
The rest of your code looks more complicated than it needs to be. Reading the spreadsheet gives you a query object - so far so good.
Your database query, named contact looks supicious. You refer to a variable named res but it's not clear where that comes from. If it comes from the spreadsheet, then a value list might be more appropriate. By the way, use query parameters.
It seems you want your csv file to combine data from the spreadsheet and the database query. If that's the case, a query of queries might be useful.
If it were me, I'd use cffile action="write" to create my csv file. I would also surround each value with double quotes in case any of them contained commas.