Question

I'm doing the following query of queries. The first dump of the query shows the entire excel sheet, the second dump shows the results of the second query.

I'm doing a validation check of the excel sheet to make sure there are no more pit bulls breeds brought into the system and I need to be able to tell the user which row on the excel sheet has the pit-bull.

How do I get the row numbers from the first row to appear in the second row? So the user can make changes on the excel sheet. Do I have to resort to editing the excel document when it was first uploaded into the server and add a row number column to it? There is probably a better way to accomplish this.

  <cffunction name="validateExcelSheet" access="public" output="yes" returnType="void" 
hint="check dogs">

    <cfspreadsheet
    action="read"
    src="#SESSION.theFile#"
    headerrow= "1"
    excludeHeaderRow = "true"
    query = "allData"
    rows = "1-#lastRow#" />


<cfscript>    
 pitBullcheck = new Query(
        sql ="SELECT * FROM allData where breed like 'Pit%' ",
        dbtype = "query",
        allData = allData);
        pitBullresult = pitBullcheck.execute().getResult();
   </cfscript>

</cffunction>

Here's a tag based version of cfquery

<cfquery name="pitBullresult" dbtype="query">
SELECT *
FROM allData
WHERE breed LIKE 'Pit'
</cfquery>
Was it helpful?

Solution

That is not something you can do with cfspreadsheet. CFSpreadsheet only returns the cell values. It does not provide the physical row numbers, within the spreadsheet, that contained those values.


Also, something else to keep in mind is that CFSpreadsheet only returns "logical" (ie populated) rows/cells. That is not the same as the "physical" row numbers 1,2,3,... and column headers A,B.C.... that you see in Excel. Since users can enter values anywhere within a spreadsheet, logical and physical are not always the same thing.

For example, create a blank spreadsheet. Then enter values in cells A2 and A25. Now run your code above. While you might expect the resulting query to contain twenty-five (25) records, it will only contain two (2), because only two cells were populated.

   query
   Row  | COL_1
    1   | Value in cell A2 (physical row 2)
    2   | Value in cell 25 (physical row 25)

I think best you could do with cfspreadsheet is to loop through the query and display the relative row number within the results ie query.currentRow. If the populated data always starts within the first few rows, that might be good enough for your purposes. If not, it could get a bit confusing ...

That said, technically you could get the real physical row numbers. However, it requires much lower level code, which quite honestly ... seems like a lot of work, for very little gain.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top