I have an excel file that I am trying to read and then display the values of the headers in a drop down. The first row in my excel file has all of the values (header names).
I used the code below, but what happens is that all the header names appear in a single line with commas. I want the headers to be separated, so that it will appear in the drop down with many <option>, instead of a single <option>. How do I do that?
<!-- Read the header values from excel -->
<cfset spreadsheet = "uploads/spreadsheet.xlsx">
<cfspreadsheet action="read" headerrow="1" src="uploads/spreadsheet.xlsx" query="excelHeader" rows="1" />
<cfset excelHeaders = excelHeader.columnList>
<!-- Display the header names as a dropdown -->
<select name="id_headers">
<option>
#excelHeaders#
</option>
</select>
You can try this code;
<!--- create new spreadsheet and populate with sample data --->
<cfset theSheet = SpreadsheetNew("Expenses")>
<cfset SpreadsheetSetCellValue(theSheet,"column1",1,1)>
<cfset SpreadsheetSetCellValue(theSheet,"column2",1,2)>
<cfset SpreadsheetSetCellValue(theSheet,"column3",1,3)>
<cfset SpreadsheetSetCellValue(theSheet,"column4",1,4)>
<!--- Write the spreadsheet to a file, replacing any existing file. --->
<cfset pathToFile = GetDirectoryFromPath(GetCurrentTemplatePath()) & "newSpreadsheet.xls">
<cfspreadsheet action="write" filename="#pathToFile#" name="theSheet" overwrite=true>
<!--- Read spreadsheet into query object --->
<cfspreadsheet action="read" headerrow="1" src="#pathToFile#" query="excelHeader" rows="1">
<!--- Display the header names as a dropdown --->
<cfoutput>
<select name="id_headers">
<cfloop list="#excelHeader.columnList#" index="option">
<option>#option#</option>
</cfloop>
</select>
</cfoutput>
You can run this code snippet in trycf
Related
I would like to show multiple tabs, i.e. multiple sheets, from an xls file in the browser using Coldfusion. I have the logic pretty worked out, but I'm not sure if there is a way to do it. My logic would be as follows:
<cfspreadsheet
action="read"
format="html"
src="test.xls"
name="Spreadsheet"
<cfloop index="i" from="1" to="5"> <!--- or however many tabs there are --->
sheet="#i#"
</cfloop>
>
<table>
<cfoutput>
#Spreadsheet#
</cfoutput>
</table>
Now I know this code doesn't work, so I was looking for a similar solution. Thank you all in advance.
Try putting cfloop around cfspreadsheet
<cfloop from="1" to="5" index="i">
<cfspreadsheet
action="read"
format="html"
src="test.xlsx"
sheet="#i#"
name="spreadsheet">
<cfdump var="#spreadsheet#"><br>
</cfloop>
Today I came across a very odd case while reading a vlue from a spreadsheet and trying to filter them on a condition and a create a spreadsheet from the filtered data. Here are my steps
Read Excel sheet
<cfspreadsheet action="read" src="#local.sFilePath#" excludeHeaderRow="true" headerrow ="1" query="local.qExcelData" sheet="1" />
Create a holding Query
<cfset local.columnNames = "LoanNumber,Product," />
<cfset local.qSuccessData = queryNew(local.columnNames,"VarChar,VarChar") />
Filter the Excel returned query on a condition and add the valid ones into the new Holding query
<cfloop query="local.qExcelData" >
<cfif ListFind(local.nExceptionRowList,local.qExcelData.currentrow) EQ 0>
<cfset queryAddRow(local.qSuccessData) />
<cfset querySetCell(local.qSuccessData, 'LoanNumber', local.qExcelData['Loan Number']) />
<cfset querySetCell(local.qSuccessData, 'Product', local.qExcelData['Product']) />
</cfif>
</cfloop>
Create the new spreadsheet
<cfspreadsheet action="write" query="local.qSuccessData" filename="#local.sTempSuccessFile#" overwrite="true">
However I am getting the following content in my excel sheet
Loannumber Product
coldfusion.sql.column#87875656we coldfusion.sql.column#89989ER
Please help on this to get it work.
I believe the query loop is not mapping values to the Holding-Query properly.
Please modify your loop as below:
<cfloop query="local.qExcelData" >
<cfif ListFind(local.nExceptionRowList,local.qExcelData.currentrow) EQ 0>
<cfset queryAddRow(local.qSuccessData) />
<cfset querySetCell(local.qSuccessData, 'LoanNumber', local.qExcelData['Loan Number'][currentRow]) />
<cfset querySetCell(local.qSuccessData, 'Product', local.qExcelData['Product'][currentRow]) />
</cfif>
</cfloop>
I'm getting an error when producing a multi-page PDF.
The pages attribute is not specified for the MERGE action in the cfpdf tag.
The line that is causing the issue is: <cfpdf action="merge" source="#ArrayToList(variables.pdfList)#" destination="promega.pdf" overwrite="yes" />
I tried looking in Adobe's documentation bug cannot find an attribute pages for the merge action. Thoughts?
<!--- Append PDF to list for merge printing later --->
<cfset ArrayAppend(variables.pdfList, "#expandPath('.')#\general.pdf") />
<cfset variables.userAgenda = GetAttendeeSchedule(
variables.event_key,
variables.badgeNum
) />
<!--- Field CFID is the id of the agenda item; use this for certificate selection --->
<cfif variables.userAgenda.recordcount>
<cfloop query="variables.userAgenda">
<cfset variables.title = Trim(variables.userAgenda.CUSTOMFIELDNAMEONFORM) />
<cfpdfform source="#expandPath('.')#\promega_certificate.pdf" destination="#cfid#.pdf" action="populate">
<cfset variables.startdate = replace(CUSTOMFIELDSTARTDATE, "T", " ") />
<cfpdfformparam name="WORKSHOP" value="#variables.title#">
<cfpdfformparam name="NAME" value="#variables.badgeInfo.FirstName# #variables.badgeInfo.LastName#">
<cfpdfformparam name="STARTDATE" value="#DateFormat(variables.startdate, "medium" )#">
</cfpdfform>
<!--- Append PDF to list for merge printing later --->
<cfset ArrayAppend(variables.pdfList, "#expandPath('.')#\#cfid#.pdf") />
</cfloop>
</cfif>
<cfif ArrayLen(variables.pdfList)>
<cfpdf action="merge" source="#ArrayToList(variables.pdfList)#" destination="promega.pdf" overwrite="yes" />
<!--- Delete individual files --->
<cfloop list="#ArrayToList(variables.pdfList)#" index='i'>
<cffile action="delete" file="#i#" />
</cfloop>
<cftry>
<cffile action="delete" file="#expandPath('.')#\general.pdf" />
<cfcatch></cfcatch>
</cftry>
<cfheader name="Content-Disposition" value="attachment;filename=promega.pdf">
<cfcontent type="application/octet-stream" file="#expandPath('.')#\promega.pdf" deletefile="Yes">
<cflocation url="index.cfm" addtoken="false" />
</cfif>
This happens when source is a single file rather than a comma separated list of files. I'm guessing that if it's a single file is is expecting to extract some pages rather than add.
I tried the following on my coldfusion 9 machine and it worked just fine:
<cfset strPath = GetDirectoryFromPath(GetCurrentTemplatePath()) />
<Cfset pdflist = arrayNew(1)>
<Cfset pdflist[1] = "#strPath#page1.pdf">
<Cfset pdflist[2] = "#strPath#page2.pdf">
<cfpdf action="merge" source="#ArrayToList(pdflist)#" destination="#strPath#merged.pdf" overwrite="yes" />
You could try to merge the pages like this and check whether you still get an error:
<cfset strPath = GetDirectoryFromPath(GetCurrentTemplatePath()) />
<Cfset pdflist = arrayNew(1)>
<Cfset pdflist[1] = "#strPath#page1.pdf">
<Cfset pdflist[2] = "#strPath#page2.pdf">
<cfpdf action="merge" destination="#strPath#merged.pdf" overwrite="yes">
<Cfloop from=1 to="#arraylen(pdflist)#" index="x">
<cfpdfparam source="#pdfList[x]#">
</cfloop>
</cfpdf>
I am trying to compare images and find if they are same or not. Images can have same name but the actual image might be different. The code that I have so far.
<cfset dirToReadFrom = #ExpandPath( '../properties-feed/unzipped/' )# />
<cfdirectory
action="list"
directory="#dirToReadFrom#"
listinfo="name"
name="qFile"
sort="asc"
filter="*.jpg"
/>
<cfset images = ArrayNew(1)>
<cfoutput query="qFile">
<cfset ArrayAppend(images, #qFile.name#)>
</cfoutput>
<cfset dirToCreate = #ExpandPath( './assets/images/resized/original/' )# />
<cfif not DirectoryExists(dirToCreate)>
<cfdirectory action = "create" directory = "#dirToCreate#" />
<cfoutput><p>Your directory has been created.</p></cfoutput>
</cfif>
<cfzip
action="unzip"
file="#ExpandPath( '../properties-feed/data.zip/' )#"
destination="#ExpandPath( './assets/images/resized/original/' )#"
overwrite="true"
/>
<cfset dirToReadFromOriginal = #ExpandPath( './assets/images/resized/original/' )# />
<cfdirectory
action="list"
directory="#dirToReadFromOriginal#"
listinfo="name"
name="qFileOriginal"
sort="asc"
filter="*.jpg"
/>
<cfset imagesLatest = ArrayNew(1)>
<cfoutput query="qFileOriginal">
<cfset ArrayAppend(imagesLatest, #qFileOriginal.name#)>
</cfoutput>
<!--- Loop over your current images --->
<cfloop query="qFileOriginal">
<!--- Check for a matching file name --->
<cfquery name="fileExists" dbtype="query">
SELECT
COUNT(*) AS num_Rec
FROM
qfile
WHERE
name = <cfqueryparam cfsqltype="cf_sql_varchar" value="#qFileOriginal.name#" />
</cfquery>
<!--- do we have a matching file name? --->
<cfif val(fileExists.num_rec)>
<cfimage action="read" name="newImage" source="#dirToReadFrom##qFile.name#"/>
<cfimage action="read" name="originalImage" source="#dirToReadFromOriginal##qFileOriginal.name#"/>
<cfset newImageBlob = ImageGetBlob(newImage) />
<cfset originalImageBlob = ImageGetBlob(originalImage) />
<!--- Compare --->
<cfif toString(newImageBlob) eq toString(originalImageBlob) >
Images are same
<cfelse>
DIFFERENT
</cfif>
</cfif>
</cfloop>
The code doesn't seem to be working. Can Anyone see what am I doing wrong?
Update 1 from comments
The result that I actually get is that the first images are same and the rest of images in files are different. But this is not correct as most of the images that I am comparing are same.
Update 2 from comments
It incorrectly identifies same images as being different. What I actually get is that the first two images are same and the rest is different. Which is not right as most of the images I have are same.
I've always just done this with BinaryEncode(), and then compare the resulting strings. You have to be careful though, as compression can make the files different even though they look (to the eye) exactly the same.
<cfquery name="writefile" datasource="#dsn#">
SELECT abc,def,pqr,stu,zex
FROM mytable
</cfquery>
<cfoutput>
<table>
<cfloop query="writefile">
<tr>
<cfloop list="#ArrayToList(writefile.getColumnNames())#" index="col">
<cffile action="write" file="d:\test.txt" output="#writefile[col][currentrow]#">
</cfloop>
</tr>
</cfloop>
</table>
</cfoutput>
I am using the above code to write a text file to a location using cffile.
But the text file is not containing all the results of the query. Please guide me.
Using cffile action="write" will reset the file each time.
Use action="append" to add content to a file without first blanking the file.
You should also consider building the string first, then writing to the file in a single action.
For example:
<cfset Content = "" />
<cfloop query="writefile">
<cfloop array=#writefile.getColumnNames()# index="col">
<cfset Content &= ' ' & writefile[col][currentrow] />
</cfloop>
<cfset Content &= chr(10) />
</cfloop>
<cffile action="write" file="d:\test.txt" output="#FileContent#" />
(Note: string concatenation used for simplicity - if performance matters, consider using StringBuilder and/or cfsavecontent instead.)