Query created from Query returned from cfspreadsheet not having proper values - coldfusion

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>

Related

Get array key and structure data from an array with structure

I've got an array that has a structure. Problem is that I can't output the array number and the structure data in the same loop, but if I have two different loops, the data will show without any errors.
The code below has two different loops showing how I can access the array number and structure data separately.
How do you output the array number and structure data in the same loop?
Code can be tested here https://cffiddle.org/
<!--- Create new structure --->
<cfset structRe = StructNew() />
<!--- Add data to structure --->
<cfset structRe.id = "14">
<cfset structRe.title = "Title">
<!--- Create new Array --->
<cfset arryRe = ArrayNew(1) />
<!--- Add structure to array --->
<cfset ArrayAppend(arryRe, structRe)>
<cfoutput>
<cfdump var="#arryRe#" />
</cfoutput>
<!--- Loop to access structure --->
<cfloop array ="#arryRe#" index="i">
<cfoutput>
#i.id# #i.title# <br />
</cfoutput>
</cfloop>
<!--- Loop to access array number --->
<cfloop array ="#arryRe#" item="item" index="i">
<cfoutput>
#i# <br />
</cfoutput>
</cfloop>
Simplifying your code a bit, and adding local scope (assuming you're in a function), here's another way to do this, too:
<cfset local.arrayRe = [
{"id": "14", "title": "Title Fourteen"}
, {"id": "15", "title": "Title Fifteen"}
] />
<cfloop array="#local.arrayRe#" index="local.i" item="local.stItem">
<cfoutput>
Item ##: #local.i#, ID: #local.stItem.id#, Title: #local.stItem.title#<br />
</cfoutput>
</cfloop>
Just figured it out.
<cfloop array ="#arryRe#" item="item" index="i">
<cfoutput>
#i# #item.id# #item.title#<br />
</cfoutput>
</cfloop>

Error with multiple PDF Generation in CF

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>

non-unique id from ews

I was using EWS to create a new calendar event. All of that seems to work just fine, but I wanted to add additional functionality to modify or delete the event. As a result, I was looking back at the unique ID I was storing, and I realized that the ID is always the same. Regardless of the event. If I create 10 in a row, they are always the same. Different dates, different content, etc - all the same ID. That is making me think that I am getting the unique ID correctly. Here is the code:
<cfobject type="Java" class="microsoft.exchange.webservices.data.ExchangeService" name="service">
<cfobject type="Java" class="microsoft.exchange.webservices.data.WebCredentials" name="credentials">
<cfobject type="Java" class="java.net.URI" name="uri">
<cfobject type="Java" class="microsoft.exchange.webservices.data.Appointment" name="appointment">
<cfobject type="Java" class="microsoft.exchange.webservices.data.SendInvitationsMode" name="invite">
<cfobject type="Java" action="create" class="microsoft.exchange.webservices.data.MessageBody" name="mb">
<cfobject type="Java" action="create" class="microsoft.exchange.webservices.data.BodyType" name="mbt">
<cfset service.init()>
<cfset credentials.init("[username]","[password]")>
<cfset service.setCredentials(credentials) />
<cfset uri.init("[url]")>
<cfset service.setUrl(uri) />
<cfset appointment = appointment.init(service) />
<cfset appointment.setStart(createDate(Year(now()), Month(now()), day(now())) />
<cfset appointment.setEnd(createDate(Year(now()), Month(now()), day(now()))) />
<cfset appointment.setSubject(" Test Appointment") />
<cfset appointment.setIsAllDayEvent(true) />
<cfset mb.setBodyType(mbt.Text) />
<cfset mb.setText("Loaded by [name] on #dateFormat(now(), "mm/dd/yyyy")#") />
<cfset appointment.setBody(mb) />
<cfset appointment.Save(invite.SendToNone) />
<!--- Both give the same results --->
ID: <cfdump var="#appointment.getId()#" />
ID: <cfdump var="#appointment.UniqueId.ToString()#" />
<!---
Result is always
AAMk ... AAA=
--->
I'm sure I'm just missing something silly in how I'm retrieving the IDs. Can anyone point me in the right direction?
Thanks!
EDIT *
I should also point out that when I add contacts ...
[contact mangling code]
<cfset contact.Save() />
ID: <cfdump var="#contact.getId()#" />
I get the exact same ID as the calendar events. Makes me think I'm actually getting an ID some something more high level like the service or something.

How to compare two images in ColdFusion

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.

Write Coldfusion query results to a text file not working

<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.)