CFSpreadSheet - format column not formatting the entire column - coldfusion

Using CF10 Standard to create a spreadsheet from a query. No matter what I've tried so far, the formatting for a specific column stops at row 32 (1 header row, 31 data), even though the entire sheet is populated to 186 rows.
<cfscript>
dfStyle=StructNew();
dfStyle.fgcolor="pale_blue";
dfStyle.dataformat="mm/dd/yyyy";
theSheet = SpreadSheetNew('mysheet');
SpreadSheetAddRow(theSheet,'SID,FIRST,LAST,ADDRESS1,ADDRESS2,CITY,STATE,ZIP,EMAIL,ADDED,PID');
SpreadSheetAddRows(theSheet,qry);
SpreadSheetFormatColumn(theSheet,dfStyle,10);
</cfscript>
I'm trying to get a consistent mm/dd/yyyy format on the 'ADDED' column. Instead, I'm getting that through row 31, and then getting dates like 41937.56594 on the of the rows.
In the formula bar I show "10/20/2014 12:25:23 PM" as the first value and "41932.552037037" as the 2nd value.
If I format the date in the query (i.e. date_format(sp_add,'%c/%e/%Y') AS spadd) I do get a nice date format all the way down the column, but the blue still stops at row 32.
Here is the cfoutput of the anonymized query - top row is formatted, bottom row loses formatting (background color - formatting dates in query). I also dropped the ADDRESS2 column from the query for now.

I'm not sure what the problem is, but try using the XML format option (the option to produce an xlsx file instead of the older style xls file). In your code add the "true" as a second argument to your spreadsheetnew() function call.
<cfscript>
dfStyle=StructNew();
dfStyle.fgcolor="pale_blue";
dfStyle.dataformat="mm/dd/yyyy";
theSheet = SpreadSheetNew('mysheet',TRUE);
SpreadSheetAddRow(theSheet,'SID,FIRST,LAST,ADDRESS1,ADDRESS2,CITY,STATE,ZIP,EMAIL,ADDED,PID');
SpreadSheetAddRows(theSheet,qry);
SpreadSheetFormatColumn(theSheet,dfStyle,10);
</cfscript>
This will solve the problem - though we don't know why :)

Related

power query Shifting row of cells to the right

I've imported a pdf file into power query. On a few of rows, the data has shifted 1 cell to the right and one those rows they are now not in the correct column. I can fix it in Excel, but is there a way to fix it in power query?
I ran into a similar challenge where the header row was displaced by one column when the exporting PDF content output to CSV.
I used the following strategy in Power Query:
Convert the affected row/record to a list.
Split it and reassemble it, i.e., sort it in the correct order.
Convert it back to a record and insert it back into the table.
Remove the old record
It looks something like this:
let
Source = Excel.CurrentWorkbook(){[Name="tblShiftLeft"]}[Content], // Fetches the table/range containing the row data that needs to be shifted.
#"Shifted Headers" = List.Combine({List.RemoveFirstN(Record.ToList(Source{0}),1), {""}}), // converts to list, splits, and reassembles
#"New Headers" = Record.FromList(#"Shifted Headers",Table.ColumnNames(Source)), // Creates a record from the list using the column names
#"New Headers Added" = Table.InsertRows(Source,1,{#"New Headers"}), // Insert the newly poisitioned header row.
#"Headers Adjusted" = Table.RemoveRows(#"New Headers Added",0) // Remove the old row
in
#"Headers Adjusted"
Hope this helps,

CFSpreadsheet not formatting dates

I think I have read just about every post on this topic and none of the proposed solutions works in my case so here goes.
I am using CF9 (upgrade not an option) for this project. I query a date field from a MSSQL database and use spreadsheetAddRows() to put the results into a spreadsheet (xls or xlsx, same result either way).
The date shows in excel as 2020-05-11 00:00:00.0 and isn't recognised as a date so the date formatting doesn't work.
I have tried using SpreadsheetFormatColumn (s, { dataformat="d-mmm-yy" }, 2); but this doesn't format the date either and has the exact same result in excel.
I have tried many variations of selecting convert(varchar, datecolumn, 101) from the database but these always just end up as text fields in excel as well so again, no date formatting and they sort in the wrong order.
Can anyone tell me what the correct format for a date is for CFSpreadsheet so that excel actually recognises it as a date?

Coldfusion query of queries with row numbers passed to second query

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

Export to Excel and formatting cells using ColdFusion 10

I have a query which I'd like to output in a spreadsheet in Excel. I'd like some of the cell columns to be formatted in a certain way, which is thousands grouping and in Number format so that sums, additions etc can be done on that row without any further alteration.
I have read through the documentation but it has left me a bit confused on how to output to Excel in the first place.
I started out with a comment but it will be easier to read as an answer.
What have you tried? Have you read through the CFSpreadsheet documentation? Should be pretty straight forward. One of the parameters to the CFSpreadsheet tag is 'query'. Why not start with that and see how it formats the columns for you by default to see what needs tweaking.
Here is an example taken directly from the referenced documentation page:
<cfquery name="courses" datasource="cfdocexamples">
SELECT CORNUMBER, DEPT_ID, COURSE_ID, CORNAME
FROM COURSELIST
</cfquery>
<cfscript>
//Use an absolute path for the files. --->
theDir=GetDirectoryFromPath(GetCurrentTemplatePath());
theFile=theDir & "courses.xls";
//Create an empty ColdFusion spreadsheet object. --->
theSheet = SpreadsheetNew("CourseData");
//Populate the object with a query. --->
SpreadsheetAddRows(theSheet,courses);
</cfscript>
<!--- Write the sheet to a file --->
<cfspreadsheet action="write" filename="#theFile#" name="theSheet" sheetname="courses" overwrite=true>
See the documentation for SpreadsheetFormatColumn, SpreadsheetFormatColumns, SpreadsheetFormatRow and SpreadsheetFormatRows to read about formatting particular cells.
You just need to use the cfspreadsheet tag to create the file, and you can format the cells with the spreadsheetFormat* functions. You can find an example of how to do this at Ray Camden's site.

Splitting column values with Sybase and ColdFusion

I need to trim the date from a text string in the function call of my app.
The string comes out as text//date and I would like to trim or replace the date with blank space. The column name is overall_model and the value is ford//1911 or chevy//2011, but I need the date part removed so I can loop over the array or list to get an accurate count of all the models.
The problem is that if there is a chevy//2011 and a chevy//2010 I return two rows in my table because of the date. So if I can remove the date and loop over them I can get my results of chevy there are 23 chevy models.
I have not used Sybase in a while, but I remember its string functions are very similar to MS SQL Server.
If overall_model always contains "//", use charindex to return the position of the delimiter and substring to retrieve the "text" before it. Then combine it with a COUNT. (If the "//" is not always present, you will need to add a CASE statement as well).
SELECT SUBSTRING(overall_model, 1, CHARINDEX('/', overall_model)-1) AS Model
, COUNT(*) AS NumberOfRecords
FROM YourTable
GROUP BY SUBSTRING(overall_model, 1, CHARINDEX('/', overall_model)-1)
However, ideally the "text" and "date" should be stored separately. That would offer greater flexibility and generally better performance.