Coldfusion output string not defined - coldfusion

When I execute the code below, resources_gathered is a string, but I get the following error ('Rx222y280.....' is the string in the column resources_gathered):
visualize_resources.cfm?subject_id=395229:105 Uncaught ReferenceError: Rx222y208Rx224y208Rx224y210Rx244y231Rx246y231Rx246y233Rx233y244Rx217y243R is not defined
<cfquery name="getField" datasource="exmind" result="result">
select resources_gathered, completed_fields from dbo.sf
where subject_id=#subject_id#
</cfquery>
<cfset record = getField>
resourcesGathered = JSON.stringify(eval(<cfoutput>#record.resources_gathered#</cfoutput>));
//resourcesGathered = (<cfoutput>#record.resources_gathered#</cfoutput>).toString();
alert(resourcesGathered);
I have tried formatting the string differently before it gets inserted into the table, but I get a reference error anytime I have to read it. Below you can see how I insert it into the table (all code below this point happens in another html file):
<cfquery datasource="exmind">
update dbo.sf
set resources_gathered = <cfqueryparam value="#resources_gathered#"
cfsqltype="cf_sql_nvarchar">
where subject_id = #subject_id#
</cfquery>
The table is correct when I do this, so I am unsure what I need to change to be able to use this data in another html file. I am able to insert/select integers (such as completed_fields) which have been converted to strings, but not this more complex string. Below you can see how the string gets created. It is passed to the query html using a cfform.
for (i=0; i<resources.length; i++) { //resources is an array
if (resources[i].visibility == true){
resourcesFormString += "x";
resourcesFormString += resources[i].x.toString(); //x and y are ints
resourcesFormString += "y";
resourcesFormString += resources[i].y.toString();
resourcesFormString += "R";
}
}
document.getElementById("resources_gathered").value = resourcesFormString;

Related

Spreadsheet Script, match with wildcard and different cases

I am having trouble executing the Spreadsheet script below.
I think there are two mistakes but I do not know how to fix.
Could anyone help it to fix it?
1:Wildcard
if(original_date=='....-..-..')
2:if synteax
if(original_date=='....-..-..')
{condition="matched"}
Detail
On the spreadsheet, there are two columns.
The first columns have dates in a format as YYYY-MM-DD such as 2020-04-21.
But sometimes, they have different formats such as 04/21/2020.
The second columns are empty.
Only when the first column cell has the "YYYY-MM-DD" format, I want to copy the cell into the second cell in the second column.
*They have 10 rows.
Here is the script.
var sheet = SpreadsheetApp.getActive().getSheetByName('Sheet1')
for(let i=1; i<=10; i++)
{
original_date_range = sheet.getRange(i, 1);
original_date = original_date_range.getValue();
cleaned_date_range = sheet.getRange(i, 2);
var condition = "";
if(original_date=='....-..-..')
{condition="matched"}
switch(condition)
{
case "matched":
cleaned_date_range.setValue(original_date);
case "":
cleaned_date_range.setValue("");
break;
}
}
I found the solution by myself.
1.Wildcard
The date "2020-04-25" can be written as /\d{4}.\d{2}.\d{2}/
2.If syntax
(Wildcard).test(string to check) is going to give you true/false output
In summary,
if you want to execute different tasks through the verification of date format such as 2020-04-25,
var sheet1 = SpreadsheetApp.getActive().getSheetByName('Sheet1')
var limit = sheet1.getLastRow()
for(let i=4; i<=10; i++) //You can add more rows if you have
{
var orignal_date_cell = sheet1.getRange(i, 1);
var orignal_date_cell_value = orignal_date_cell.getValue();
var target_date_cell = sheet1.getRange(i,2);
if ((/\d{4}.\d{2}.\d{2}/).test(orignal_date_cell_value))
{
target_date_cell.setValue(orignal_date_cell_value);
}
}

SpreadsheetFormats not working as expected

I am able to populate data from a query into a spreadsheet. However, I am having problems getting "ranged" formatting to work properly. The formatting for specific column (date) and row (header) work fine. But SpreadsheetFormatColumns, ...Rows, ...CellRange is not. I need to set the font and fontsize to the whole dataset.
Here is what I have tried.
<cfscript>
//Current directory path.
theFile = GetDirectoryFromPath(GetCurrentTemplatePath()) & "GridDump.xls";
//Create a new Excel spreadsheet object and add the query data.
theSheet = SpreadsheetNew("Raw Data");
FormatDate.dataformat = "dd-mmm-yy";
//Get Row Count and Row Range
RC = toString(result.recordcount+1);
RR = "1-" & RC;
//Get Column Count
CC = toString(ListLen(GridFieldNames));
//Get Column Letter
CL = chr(CC + 64);
//Get Column Range (Nummerical)
CRN = "1-" & CC;
//Get Column Range (Alphabetical)
CRA = "A-" & CL;
//Set Sheet Format
WholeSheet = StructNew();
WholeSheet.font="Consolas";
WholeSheet.fontsize=12;
//Set header Row Format
HeadRow = StructNew();
HeadRow.bold="true";
//Insert the Header Row
SpreadsheetAddRow(theSheet,GridFieldNames);
//Insert the Data
SpreadsheetAddRows(theSheet,result);
//Format the Data
SpreadsheetFormatCellRange(theSheet,WholeSheet,1,1,RC,CC);
//SpreadsheetFormatRows(theSheet,WholeSheet,RR);
//SpreadsheetFormatColumns(theSheet,WholeSheet,CRN);
SpreadsheetFormatRow(theSheet,HeadRow,1);
//Header Row
SpreadsheetFormatColumn(theSheet,FormatDate,1);//Date Column
SpreadsheetAddFreezePane(theSheet,0,1);//Top Row Only
//SpreadSheetAddAutofilter(theSheet,"A1:J1");
</cfscript>
Here are the results
I'm getting the same result for all three of the "ranged" formatting functions. The format stops part way through the spreadsheet. I expect the whole dataset to accept any of the ranged function formats.
I got the same result with CF 2018,0,04,314546. Could just be a limitation of XLS format.
Switching to XLSX worked fine for me:
theSheet = SpreadsheetNew("Raw Data", true);
YMMV, but what also worked with CF2018 was using SpreadsheetFormatColumns() instead of SpreadsheetFormatCellRange().

Using cfloop with queryfilter function

I am new to ColdFusion and trying to use cfloop for the below code:
<cfscript>
var origRate = 0;
var toRate = 0;
rates = myQuery.filter(function (obj) {
return (obj.code == arguments.origCode || obj.code ==
arguments.toCode)
})
</cfscript>
I modified below, the original code and inserted the above new code to avoid the inline sql queries:
<cfquery name="rates" dbtype="query">
select code, rate
from myQuery
where code = <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.origCode#" />
or code = <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.toCode#" />
</cfquery>
I tried using cfloop without changing to the previous code as below but it is not working:
<cfloop query="rates">
<cfscript>
if (code == arguments.origCode) origRate = rate;
if (code == arguments.toCode) toRate = rate;
</cfscript>
</cfloop>
Once the second block of code was inserted by commenting out the first block of code above, it did not load the page. If anyone has an idea, I really appreciate it. Thank you in advance!
There were some missing details about the application and data, so I made a couple of assumptions. It appears that you have a query object that you want to filter and pull rates from for an origCode and a toCode. Without knowing more about your data structure and what you plan to do with it, I can only make some general suggestions. I still maintain that it would be much better to filter in the query, but I understand the limitation. Since you have to filter inside your application, both the bulk of the base data you initially return and the processing to filter those records will negatively impact the performance.
First thing I did was to set up a fake query object. This is where my first assumption comes into play. I assumed that your code won't be any duplicated in your table, and that the code will have a rate associated with it.
myQuery = queryNew(
"code, rate",
"integer, integer",
[
{ "code" : 1 , "rate" : 10 } ,
{ "code" : 2 , "rate" : 15 } ,
{ "code" : 3 , "rate" : 20 } ,
{ "code" : 4 , "rate" : 25 } ,
{ "code" : 5 , "rate" : 30 }
]
);
I would not recommend a Query of Query here, because it's a lot of overhead for something that can be accomplished fairly easily.
I created a function that you can pass in your origCode and the toCode, and it will return you a structure of the origRate and the toRate. I included some comments in the code, so you will be able to see what I was doing. The bulk of the function is using the filter() closure to filter the query records down. If you are able to filter through SQL, you'll be able to eliminate this block.
function returnNewRates( required Numeric origCode, required Numeric toCode ) {
local.ratesStruct = { "origRate":-1, "toRate":-1 } ;
// This will be our query. If we _have_ to use an existing query, pass it in and duplicate() it. (Pass by Reference!)
local.qry = duplicate( myQuery ) ;
/////////////
// Closure to filter the query. This should be done in SQL.
// https://helpx.adobe.com/coldfusion/cfml-reference/coldfusion-functions/functions-m-r/queryfilter.html
local.filteredQuery = qry
.filter( function (obj) {
return ( obj.code == origCode || obj.code == toCode ) ;
} ) ;
// Now assign new rates. NOTE: The query shouldn't return more than 2 rows. We can validate if needed.
for ( var r IN filteredQuery ) {
if( r.code == arguments.origCode ) { ratesStruct.origRate = r.rate ; }
if( r.code == arguments.toCode ) { ratesStruct.toRate = r.rate ; }
}
return ratesStruct ;
}
To assign the origRate and toRate, we first create a ratesStruct return value to hold the structure of the rates. After we filter our query, we just loop through those filtered results and check to see if the code in the row matches with our input variables. Another one of my assumptions was that the database would return no more than two records (one origCode and one toCode, or neither). If it is possible to return more than one row for a code, then the output codes will be overwritten by the last related row in the query. If there are other rows appropriate for sorting, then they can be used and only select the top row for the needed rate. I also defaulted the returned rates to a -1 to signify that no rate was found for the code. That can be changed if needed.
After that, I just ran a few tests to make sure we didn't get any wonkiness. Code is at https://trycf.com/gist/c3b87ca7c508562fd36f3ba6c73829c7/acf2016?theme=monokai.
And again, I think this can all probably be done within the database itself. Probably by giving you access to a stored procedure that you can pass origCode and toCode to.
If you are receiving an error about an invalid construct it is because the version of CF does not support the == operator. For Adobe ColdFusion, until recently the only supported equals operators have been eq, is or various comparison functions depending on the variables and intentions involved.
<cfloop query="rates">
<cfscript>
if (code eq arguments.origCode) origRate = rate;
if (code eq arguments.toCode) toRate = rate;
</cfscript>
</cfloop>

cfspreadsheet save as .csv, Excel says "The file format and extension of FILE.csv don't match."

I've created a cold fusion page to output a client list from MYSQL into a CSV file for easy uploading to SalesForce.com
I can generate the file with all the correct information. However, when I try to open it with excel I get the error:
"The file format and extension of 'SalesForceDailyLeads-20160613125138.csv' don't match. The file could be corrupted or unsafe. Unless you trust its source, don't open it. Do you want to open it anyway?" I can open it(excel for MAC), but it appears to me that CFSpreadsheet is not creating a legit .csv file and is instead making a xlsx.
<cfset FileCSV = "SalesForceDailyLeads-#dateformat(getBatch.BATCH,"yyyymmdd")##timeformat(getBatch.BATCH,"HHmmss")#.csv" >
<cfset filename = "/SF/#fileCSV#">
<cfset s = spreadsheetNew() >
<cfset spreadsheetAddRow(s, "FIRST, LAST, MIDDLE, STREET, CITY, ZIP, STATE")>
<cfinclude template="SFgetList.cfm">
<cfset spreadsheetAddRows(s, getList)>
<cfspreadsheet
action="write"
overwrite = "true"
format ="csv"
name ="s"
filename ="#filename#"
>
If I make an XLS file I have no issues like I do with CSVs. Is this a problem with the code, CFSpreadsheet, or excel(for mac)? Can I fix it?
Use cffile, not cfspreadsheet to create the files. Per the documentation:
The cfspreadsheet tag writes only XLS[X] format files. To write a CSV
file, put your data in a CSV formatted string variable and use the
cffile tag to write the variable contents in a file.
Thanks! Of course, right after I posted this I found the docs and saw that I was using it incorrectly and I used CFFILE instead. I used a script/function I found to do this. It took a little work however, to convert the query into CSV- luckily someone else already did it. In case anyone care to see it:
I got the querytoCSV script here:
https://gist.github.com/CreativeNotice/2775372
<cfscript>
/**
* queryToCsv
* Allows us to pass in a query object and returns that data as a CSV.
* This is a refactor of Ben Nadel's method, http://www.bennadel.com/blog/1239-Updated-Converting-A-ColdFusion-Query-To-CSV-Using-QueryToCSV-.htm
* #param {Query} q {required} The cf query object to convert. E.g. pass in: qry.execute().getResult();
* #param {Boolean} hr {required} True if we should include a header row in our CSV, defaults to TRUE
* #param {String} d {required} Delimiter to use in CSV, defaults to a comma (,)
* #return {String} CSV content
*/
public string function queryToCsv(required query q, required boolean hr = true, required string d = ","){
var colNames = listToArray( lCase(arguments.q.columnlist) );
var newLine = (chr(13) & chr(10));
var buffer = CreateObject('java','java.lang.StringBuffer').Init();
// Check if we should include a header row
if(arguments.hr){
// append our header row
buffer.append(
ArrayToList(colNames,arguments.d) & newLine
);
}
// Loop over query and build csv rows
for(var i=1; i <= arguments.q.recordcount; i=i+1){
// this individual row
var thisRow = [];
// loop over column list
for(var j=1; j <= arrayLen(colNames); j=j+1){
// create our row
thisRow[j] = replace( replace( arguments.q[colNames[j]][i],',','','all'),'""','""""','all' );
}
// Append new row to csv output
buffer.append(
JavaCast( 'string', ( ArrayToList( thisRow, arguments.d ) & iif(i < arguments.q.recordcount, "newLine","") ) )
);
}
return buffer.toString();
};
</cfscript>
<cfinclude template="getDups.cfm">
<cfinclude template="SFgetList.cfm">
<cfset FileCSV = "SalesForceDailyLeads-#dateformat(getBatch.BATCH,"yyyymmdd")##timeformat(getBatch.BATCH,"HHmmss")#.CSV" >
<cfset filename = "/mnt/nas-share/data/feed/SF/#fileCSV#">
<cfset qc = #queryToCsv(getList, false, ",")# >
<cfoutput>#qc#</cfoutput>
<cfset heads= "FIRST, LAST, MIDDLE, STREET, CITY, ZIP, STATE">
>
<cffile
action = "write"
file = #filename#
output = #heads#
addNewLine = "yes"
fixnewline = "no">
<cffile
action = "append"
file = #filename#
output = #qc#
addNewLine = "yes"
fixnewline = "no">

Coldfusion: Can't reference var from query result set

Using cfscript, trying to set the ID of the newly inserted question so that I can use it in my answer insert to build the relationship. I've done this a million times outside of cfscript. setName seems to be the proper method to call to create the query name.
I'm receiving the error that "theQuestionID" does not exist in qryQuestion
i = 1;
while ( structKeyExists( form, "question" & i ) )
{
q = new Query();
q.setDatasource("kSurvey");
q.setName("qryQuestion");
q.setSQL("
set nocount on
insert into question (question)
values('#form["question#i#"]#')
select ##IDENTITY AS theQuestionID
set NOCOUNT off
");
q.execute();
writeOutput("Question"&i&"<br>");
j = 1;
while ( structKeyExists( form, "question" & i & "_answer" & j) ) {
q = new Query();
q.setDatasource("kSurvey");
q.setSQL("
insert into answer (answer,questionid)
values('#form["question#i#_answer#j#"]#',#qryQuestion.theQuestionID#)
");
q.execute();
writeOutput("Answer"&j&"<br>");
j++;
}
i++;
}
Theres a better way to accomplish this without having to select ##identity (which in itself isn't the best way to get it from sql server, using scope_identity is the best practice way to do this in sql server. http://msdn.microsoft.com/en-us/library/ms190315.aspx
Fortunately ColdFusion makes this even easier:
<cfscript>
insertQuery = new query();
insertQuery.setDatasource("datasourcename");
insertQuery.setSql("insert into contact(firstname, lastname)
values('ryan','anklam')");
result = insertQuery.Execute();
theKey = result.getPrefix().generatedkey;
</cfscript>