trying to wrap a structure with another set of structure - struct

I am trying to wrap a structure with another structure, but definitely i am missing something
i want to show it like this
account: {
"name": "Example Account",
"details": "https://www.example.com"
}
i am trying this function, but i am missing something, probably i should use structmap, not sure
<cffunction name="SO">
<cfargument name="name" required="true" type="struct">
<cfargument name="data" required="true" type="struct">
<cfloop collection="#arguments.Data#" index="i">
<cfset arguments.Name['#lcase(i)#'] = arguments.Data[i]>
</cfloop>
<cfreturn arguments.Name>
</cffunction>

When I first looked at this, I thought you essentially wanted to have a new struct with a "name" (containing a string) and a "data" containing a struct.
Your initial setup is essentially
<cfset name = "Test" >
<cfset data = { details: "detail1" , details2: "detail2" } >
When dealing with most data structures in CF, I usually find it much easier and cleaner to work with CFSCRIPT instead of tags. So I came up with
<cfscript>
function SO ( required String name, required Struct data ) {
var retval = { name:"",data:{} } ;
retval.name = arguments.name ;
arguments.data.map( function(key, val) {
retval.data[key.lcase()] = val ;
} ) ;
return retval;
}
writedump( SO(name,data) ) ;
</cfscript>
Trying to unwind this, it appears that all you're trying to do is add a "name" value to your "data" struct. This can easily be done with
<cffunction name="SO_tags">
<cfargument name="name" required="true" type="string">
<cfargument name="data" required="true" type="struct">
<cfset retval2 = arguments.data >
<cfset retval2.name = arguments.name>
<cfreturn retval2>
</cffunction>
<cfset structure = {"account":SO_tags(name,data)}>
<cfdump var = #structure#>
Or using script, it can come down to just a single line.
<cfscript>
function SO3 ( required String name, required Struct data ) {
return arguments.data.append( {"name":arguments.name} ) ;
}
writedump( {"account":SO3(name,data)} ) ;
</cfscript>
Depending on how this fits into the rest of your code, you may not even need a function.
https://cffiddle.org/app/file?filepath=b6e92a83-ebc9-40eb-a712-f402d7f9ed85/f4fb9f8e-3b60-4a64-8c02-2cd106736df0/4e00abae-b949-4934-86e8-870745259127.cfm
EDIT: Changed output to nest it within an "account" structure.

Related

Function returns undefined but works inside function - NodeJS [duplicate]

I am calling a function with the following code:
var resultz = nsEditor.updateStringCall(debtID, column2Change, value2Change, 4, 10);
The function that is being called but doesn't return a value
updateStringCall: function(pdebtID, pcolumn2Change, pvalue2Change, pmin, pmax){
try {
var resultz2 = 0;
$.ajax({
type: "POST",
url: "Components/MintLibraries.cfc",
dataType: "json",
cache: false,
data: {
method: 'StringUpdate',
val2Change: pvalue2Change.trim(),
debtID: pdebtID,
column2Change: pcolumn2Change,
greaterThan: pmin,
lesserThan: pmax,
returnFormat: "json"
},
success: function(response) {
resultz2 = 1;
return resultz2;
}, //end done
error: function(jqXHR, textStatus, errorThrown){
resultz2 = 0;
return resultz2;
}); //end ajax call
} catch (e) {
resultz2 = 0;
return resultz2;
} //end trycatch
}, // end updateStringCall
This function uses the .ajax to call a coldfusion cfc method StringUpdate:
<cffunction name="StringUpdate" access="remote" output="false" returntype="any" >
<cfargument name="val2Change" type="string" required="true" />
<cfargument name="debtID" type="string" required="true" />
<cfargument name="column2Change" type="string" required="true" />
<cfargument name="greaterThan" type="numeric" required="false" />
<cfargument name="lesserThan" type="numeric" required="false" />
<cfset var debt2Pass = int(val(arguments.debtID)) />
<cfset var updQuery = "" />
<cfset var retValue = 0 />
<cfset var iVal = 0 /><cfset var valError = '' />
<cftry>
<cfquery name="updQuery" datasource="#application.datasource#" result="qResults" >
Update dmf set #arguments.column2Change# =
<cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.val2Change#"/>
where DebtID =
<cfqueryparam cfsqltype="cf_sql_smallint" value="#arguments.debtID#"/>
</cfquery>
<cfset retValue = 1 />
<cfcatch type="Any" >
<cfset thrown = ThrowCFError(405,'debug', 'Error updating ' & arguments.DebtID,arguments.val2Change & ', ' & arguments.debtID & ',' & arguments.column2Change) />
<cfset retValue = 0 />
</cfcatch>
</cftry>
<cfreturn retValue />
</cffunction>
The query runs successfully but the js function doesn't return a value (it shows up as undefined). Even if it gets an error, I would think I have accounted enough for that to at least get a return value.
Ideas?
The function is not returning a value because the returns in your method are embedded inside callback functions. They are only returning from those callbacks, not from your main function. Consider, for example, your "success" callback:
success: function(response) {
resultz2 = 1;
return resultz2;
}
This return is just returning from the success function... It doesn't bubble up to your updateStringCall function.
The reason it has to work this way is because ajax calls are asynchronous. Your method returns immediately while the request happens in the background. So in order to get the return value, you have to pass in a callback that has access to the value when it's ready, and then call the callback from within the ajax callbacks instead of returning a value.
So change your method definition to this:
// Note I added a "callback" parameter
function(pdebtID, pcolumn2Change, pvalue2Change, pmin, pmax, callback) { ... }
And then in your ajax callbacks, call it instead of returning a value:
success: function(response) {
resultz2 = 1;
callback(resultz2);
},
error: function(jqXHR, textStatus, errorThrown) {
resultz2 = 0;
callback(resultz2);
});
Finally, pass in a callback when you call this method:
nsEditor.updateStringCall(debtID, column2Change, value2Change, 4, 10, function(resultz) {
// Do something with resultz
});
One final note, if you are going to have that blanket try/catch in there, you should change that to use the callback also:
catch (e) {
resultz2 = 0;
callback(resultz2);
}
But really, I'd recommend just taking that try/catch out altogether. I can't see how that can possibly be helpful here. It will only hide actual problems with code that already has a better structure for error handling. I suspect you just put in there to debug this return problem, but if it was there already, just take it out.
This is the world of event-driven functional programming! It's a very common sort of structure in javascript.
Ajax calls are asynchronous. You'll have to work with your result from within the success callback of the $.ajax().
Add async: false like this:
$.ajax({
type: "POST",
async: false,
url: "Components/MintLibraries.cfc",
dataType: "json",
cache: false,
})

Amazon Pay - Signing requests using Coldfusion

I'm trying Amazon pay API using Coldfusion. In the authorization, I'm continuously getting the "InvalidRequestSignature" error. I've followed every step given in this document correctly.
I can run this successfully in the node SDK. I've got the x-amz-pay-date and x-amz-pay-idempotency-key from that response and checked in the ColdFusion by giving those statically for those headers. The string generated in ColdFusion is same as the string generated in node SDK like this: AMZN-PAY-RSASSA-PSS\nfb972741819aa4124204361d8bf6524b83aca4bc7e2313c4c9da5111dea8743c
I don't know what is happening on the ColdFusion.
This is my canonical request string
POST
/v2/checkoutSessions
accept:application/json
content-type:application/json
x-amz-pay-date:2021-09-23T16:28:54Z
x-amz-pay-host:pay-api.amazon.eu
x-amz-pay-idempotency-key:c2e281a1f01c63529d67b838e4bfcffd
x-amz-pay-region:uk
accept;content-type;x-amz-pay-date;x-amz-pay-host;x-amz-pay-idempotency-key;x-amz-pay-region
fa427e08f2b6e529b67275f6e9210101b5538252c42f4f4c7408aafda5050043
FYI, if you see this in ColdFusion, you can not see the line break.
This is my StringToSign:
AMZN-PAY-RSASSA-PSS\nfb972741819aa4124204361d8bf6524b83aca4bc7e2313c4c9da5111dea8743c
This is my error:
{"reasonCode":"InvalidRequestSignature","message":"Unable to verify signature, signing String [AMZN-PAY-RSASSA-PSS\nc9a4938f623a2904fdea51cg07cd6e6b20ff350c80f20445ba3d5a89fc4077eb] , signature [o7QA2WUEzE6qy7giGirM62LuhxgjFPuzf1+oCnJSgMOTltotF5Gxp2t/oWtms+9myFtsSvT6N0ZNViUckFL3XUPjvDF8QTH+dWKugL2h8147MAN9yt2wORgGYExEk+oqNeUmfMhz0AXZu+wdRsKHs97AbDrMmj9lAbM0Orh5p2xvBVFf5PMYhpSe0a50i7QL+GYSipwNmAXEQOualpkpnVjk6lXhewSMyef2p0XZxnJDE1cyr6iBtWSwCBn4gk17yQGmifJb8joPgYEh3BPQLmHTXOSubJSYxEJZcHYYG35DPIIFqyJEQsJGRlfkVLaS27xjHpzZOTNgyZLMuS1jXQ==]"}
I'm not sure whether I'm making a mistake in the RSA-SHA256 encryption algorithm or not. I'm following Ben Nadel's RSA-Encrypted Signature Generation.
I can not find any solution for ColdFusion. That's why I asked here. Thanks in advance.
Here is my code:
<cffunction name="createCanonicalRequest" access="private" returnType="string">
<cfargument name="requestMethod" type="string" required="true" />
<cfargument name="originalURI" type="string" required="true" />
<cfargument name="queryParams" type="struct" required="false" default={} />
<cfargument name="requestHeaders" type="struct" required="false" default={} />
<cfargument name="requestPayload" type="string" required="false" default="" />
<cfset var canonicalRequestMethod = arguments.requestMethod>
<cfset var trimmedURI = len(trim(originalURI)) ? originalURI : "/" & originalURI>
<cfset var canonicalURI = replace( encodeRFC3986( trimmedURI ), "%2F", "/", "all")>
<cfset var encodedParams = {}>
<cfscript>
structEach( queryParams, function(key, value) {
encodedParams[ encodeRFC3986(arguments.key) ] = encodeRFC3986( arguments.value);
});
</cfscript>
<cfset var encodedKeyNames = structKeyArray( encodedParams )>
<cfset arraySort( encodedKeyNames, "text" )>
<cfset var encodedPairs = []>
<cfscript>
for (var key in encodedKeyNames) {
arrayAppend( encodedPairs, key &"="& encodedParams[ key ] );
}
</cfscript>
<cfset var canonicalQueryString = arrayToList( encodedPairs, "&")>
<cfset var cleanedHeaders = {}>
<cfscript>
structEach( requestHeaders, function(key, value) {
var headerName = reReplace( trim(arguments.key), "\s+", " ", "all");
var headerValue = reReplace( trim(arguments.value), "\s+", " ", "all");
cleanedHeaders[ lcase(headerName) ] = headerValue;
});
</cfscript>
<cfset var sortedHeaderNames = structKeyArray( cleanedHeaders )>
<cfset arraySort( sortedHeaderNames, "text" )>
<cfset var cleanedPairs = []>
<cfscript>
for (var key in sortedHeaderNames) {
arrayAppend( cleanedPairs, key &":"& cleanedHeaders[ key ] );
}
</cfscript>
<cfset var canonicalHeaderString = arrayToList( cleanedPairs, Chr(10) ) & Chr(10)>
<cfset var canonicalSignedHeaderString = arrayToList( sortedHeaderNames, ";" )>
<cfset var canonicalPayloadChecksum = lcase( hash( requestPayload , "SHA256", "UTF-8" ) )>
<cfset var canonicalRequest = canonicalRequestMethod & Chr(10)
& canonicalURI & Chr(10)
& canonicalQueryString & Chr(10)
& canonicalHeaderString & Chr(10)
& canonicalSignedHeaderString & Chr(10)
& canonicalPayloadChecksum>
<cfset var requestDigest = lcase( hash( canonicalRequest , "SHA256", "UTF-8" ) )>
<cfreturn variables.amazonSignatureAlgorithm & Chr(10) & requestDigest />
</cffunction>
<cffunction name="encodeRFC3986" access="private" returnType="string">
<cfargument name="text" type="string" required="true" />
<cfset var encoded = encodeForURL(arguments.text)>
<cfset encoded = replace( encoded, "%7E", "~", "all" )>
<cfset encoded = replace( encoded, "+", "%20", "all" )>
<cfset encoded = replace( encoded, "*", "%2A", "all" )>
<cfreturn encoded />
</cffunction>
I'm using the above UDF to create the canonical request.
FYI, I'm using Coldfusion 2016.

Component attributes do not support complex content (mixed C# and markup) error message

I am working on validation on a form in a blazor server application.
I created the component below that I am using
#* Inherits from the original InputText component *#
#inherits InputText
#* Bind the oninput event *#
<input #attributes="AdditionalAttributes"
class="#CssClass"
value="#CurrentValue"
#oninput="EventCallback.Factory.CreateBinder<string>(this, __value => CurrentValueAsString = __value, CurrentValueAsString)" />
#code {
}
I am using the inputTextOnInput in this form
<EditForm EditContext="#EditContext">
<DataAnnotationsValidator />
<div class="mb-5">
<label for="projectnameinput" class="form-label">Name your project*</label>
<InputTextOnInput class="form-control form-control-lg cust #pNameValidation" id="projectnameinput" #bind-value="projectModel.ProjectName" #onkeyup=KeyboardEventHandler />
</div>
</EditForm>
since I created this I started getting the error message below
Component attributes do not support complex content (mixed C# and markup). Attribute: 'class', text: 'form-controlform-control-lgcustpNameValidation
Do you have an idea of what this implies?
You cannot use the class attribute in your new component if you have declared a specific parameter for that.
<InputTextOnInput class="form-control form-control-lg cust #pNameValidation" id="projectnameinput" #bind-value="projectModel.ProjectName" #onkeyup=KeyboardEventHandler />
you need to remove class from above and pass it via CssClass.
For some reason Blazor/Razor don't let you mix text literals and variables within an attribute value. So when you try and do
<InputTextOnInput class="form-control form-control-lg cust #pNameValidation" id="projectnameinput" #bind-value="projectModel.ProjectName" #onkeyup=KeyboardEventHandler />
the class statement is invalid because it contains literals "form-control", "form-control-lg" and "cust" and also a variable "#pNameValidation".
My solution to this problem was to create a little method on a component base class which allows mixing the two things.
public string Class(string classStr, params string[] objStrs)
{
var output = new StringBuilder();
output.Append(classStr);
output.Append(' ');
output.AppendJoin(' ', objStrs);
return output.ToString();
}
now all I have to do is to write my line like this
<InputTextOnInput class="#Class("form-control form-control-lg cust", pNameValidation)" id="projectnameinput" #bind-value="projectModel.ProjectName" #onkeyup=KeyboardEventHandler />
and by using params, you can also just add as many string variables as you want.

Create workbook with multiple sheets from Queries

I am using ColdFusion 2016 and I've discovered that the server that I'm using only has CF9, I'm pretty new to this. I've been working on updating existing code to fit what the users now want. So far I've managed, but this is beyond me. I've got a website that generates an excel workbook with one sheet. It uses HTML and a query to create it. Starts with the query name in A1 the report date in A3, the table headers in A5:H5 and then the data in A6:H53 (The exact length could vary, but always from column A - H). I'll post what is being used to create the workbook. What i want to do is use 3 more queries to add 3 more sheets to the workbook. I've tried adding a function that I found here and that didn't do any good. I tried modifying the existing code some to try and use all 4 queries, no joy.
Any help would be appreciated. Let me know if I need to add more detail.
Here's the code:(I added comments for what I added trying to get this to work)
<cfsilent>
<!--- *******************************************************************
Filename: execSummary_Excel.cfm, v1.0 03/07/2012
Created By: Original Writer
Description: Excel report export for Executive Summary Report.
Change History:
Date........Name...........Description of Change........................
08/01/2012 Original Writer Added committed column.
02/28/2013 Original Writer Added stateGM and GM.
*************************************************************************--->
<cfinvoke component="financial.financial" method="getExecSummary" returnvariable="qExecSummary">
<cfinvokeargument name="level" value="#URL.level#" />
<cfinvokeargument name="stateGM" value="#URL.stateGM#" />
<cfinvokeargument name="GM" value="#URL.GM#" />
<cfinvokeargument name="engVP" value="#URL.engVP#" />
<cfinvokeargument name="engDir" value="#URL.engDir#" />
</cfinvoke>
<!---Added this to test if I can get more than one sheet to the Workbook--->
<cfinvoke component="financial.financial" method="getExecSummary_OLD" returnvariable="qExecSummary_OLD">
<cfinvokeargument name="level" value="#URL.level#" />
<cfinvokeargument name="stateGM" value="#URL.stateGM#" />
<cfinvokeargument name="GM" value="#URL.GM#" />
<cfinvokeargument name="engVP" value="#URL.engVP#" />
<cfinvokeargument name="engDir" value="#URL.engDir#" />
</cfinvoke>
<!--- Get Report Date since qExecSummary is more complex than the other report queries --->
<cfquery name="qRpt_Date" datasource="#application.dsn#">
SELECT DISTINCT rpt_date
FROM fin_data
</cfquery>
<cfsetting requesttimeout="5000" />
</cfsilent>
<!---Added this as a function that should allow me to use multiple queries and create a workbook with more than one sheet--->
<cfscript>
//Create new workbook with one sheet
//by default that sheet is the active sheet
Workbook = SpreadsheetNew("ExecSummary");
//Add Data to the sheet
format1.bold="true";
formatNum.dataformat="0.00%";
Spreadsheetformatcell(Workbook,format1,1,1);
SpreadSheetSetCellValue(Workbook,"Executive Summary Report",1,1);
Spreadsheetformatcell(Workbook,format1,3,1);
SpreadSheetSetCellValue(Workbook,"#dateFormat(now(),'mm/dd/yyyy')#",3,1);
SpreadSheetSetCellValue(Workbook,"Data Date",5,1);
SpreadSheetSetCellValue(Workbook,"Level",5,2);
SpreadSheetSetCellValue(Workbook,"Name",5,3);
SpreadSheetSetCellValue(Workbook,"Description",5,4);
SpreadSheetSetCellValue(Workbook,"Budget",5,5);
SpreadSheetSetCellValue(Workbook,"Commited",5,6);
SpreadSheetSetCellValue(Workbook,"Spent YTD",5,7);
SpreadSheetSetCellValue(Workbook,"% Spent",5,8);
arr="Here";
writedump(arr);
//if (qExecSummary.recordCount) {
// rowNum = 6;
// arr="Here";
// writedump(rowNum);
//alert(qExecSummary.recordCount);
//for(dataRow in qExecSummary){
//SpreadSheetSetCellValue(Workbook,dateFormat(qRpt_Date.rpt_Date, 'mm/dd/yyyy'),rowNum,1);
//SpreadSheetSetCellValue(Workbook,dataRow.responsible,rowNum,2);
//SpreadSheetSetCellValue(Workbook,dataRow.name,rowNum,3);
//SpreadSheetSetCellValue(Workbook,dataRow.Description,rowNum,4);
//SpreadSheetSetCellValue(Workbook,dataRow.bud_sum,rowNum,5);
//SpreadSheetSetCellValue(Workbook,dataRow.committed,rowNum,6);
//SpreadSheetSetCellValue(Workbook,dataRow.Spent_YTD,rowNum,7);
/*if (qExecSummary.bud_sum NEQ 0){
Spreadsheetformatcell(Workbook,formatNum,rowNum,8);
//percentSpent="#(qExecSummary.Spent_YTD/qExecSummary.bud_sum)*100#";
SpreadSheetSetCellValue(Workbook,(dataRow.Spent_YTD/dataRow.bud_sum)*100,rowNum,8);
}
else {*/
//SpreadSheetSetCellValue(Workbook,0,rowNum,8);
//}
//rowNum++;
//}
//End of WriteOutput
//} else {
// SpreadSheetAddRows(Workbook,"No results for your criteria.");
//}
</cfscript>
<!---
<cffunction name="QueriesToXLS" access="public">
<cfargument name="queryArr" required="true">
<cfargument name="sheetNameArr" required="false">
<cfset tempPath="C:\Temp\ExecutiveSummary" & ".xls">
<cfset counter= 1>
<cfloop array="#ARGUMENTS.queryArr#" index="i" >
<cfset sheetName="Sheet#counter#">
<cfif isDefined("ARGUMENTS.sheetNameArr")>
<cfset sheetName=ARGUMENTS.sheetNameArr[counter]>
</cfif>
<cfspreadsheet action="update" filename="#tempPath#" query="i" sheetName="#sheetName#"/>
<cfset counter += 1>
</cfloop>
<cfreturn SpreadsheetRead(tempPath)>
</cffunction>
<cfset xlsData = QueriesToXLS([qExecSummary,qExecSummary],["ExecutiveSummary","ExecutiveSummaryOLD"])>
--->
<cfheader name="Content-Disposition" value='attachment; filename="execSummaryNew.xls"'>
<!---cfcontent type="application/msexcel" variable="#SpreadsheetReadBinary(xlsData)#" reset="true"--->
<cfcontent type="application/vnd.ms-excel"> <!---This is where the application type is being set to Excel--->
<!---html xmlns:x="urn:schemas-microsoft-com:office:excel">
<head>
</head>
<h2>Executive Summary Report</h2>
<cfoutput>
<p>#dateFormat(now(), 'mm/dd/yyyy')#</p>
</cfoutput>
<table id="tableOne" class="yui" cellspacing="0" cellpadding="5">
<thead>
<tr>
<th>Data Date</th>
<th>Level</th>
<th>Name</th>
<th>Description</th>
<th>Budget</th>
<th>Committed</th>
<th>Spent YTD</th>
<th>% Spent</th>
</tr>
</thead>
<cfif qExecSummary.recordCount>
<tbody>
<cfoutput query="qExecSummary">
<tr>
<td>#dateFormat(qRpt_Date.rpt_Date, 'mm/dd/yyyy')#</td>
<td>#qExecSummary.responsible#</td>
<td>#qExecSummary.name#</td>
<td>#qExecSummary.Description#</td>
<td>#qExecSummary.bud_sum#</td>
<td>#qExecSummary.committed#</td>
<td>#qExecSummary.Spent_YTD#</td>
<td><cfif qExecSummary.bud_sum NEQ 0>
#numberFormat((qExecSummary.Spent_YTD/qExecSummary.bud_sum)*100,"9.9")#%
<cfelse>
0
</cfif>
</td>
</tr>
</cfoutput>
</tbody>
<cfelse>
<tr>
<td colspan="9">No results for your criteria.</td>
</tr>
</cfif>
</table>
</html--->
EDIT
I have tried following the answer provided on the question referenced in the duplicate. I am not able to get it to work with the query that I have. I have added the following code in place of the script that I had:
EDIT 2
Updated this script, still not working. errors on the writeOutput().I'm not sure how to implement using a query to create the data for the rows?
<cfscript>
//Create new workbook with one sheet
//by default that sheet is the active sheet
Workbook = SpreadsheetNew("ExecSummary");
//Add Data to the sheet
format1.bold="true";
formatNum.dataformat="0.00%";
Spreadsheetformatcell(Workbook,format1,1,1);
SpreadSheetSetCellValue(Workbook,"Executive Summary Report",1,1);
Spreadsheetformatcell(Workbook,format1,3,1);
SpreadSheetSetCellValue(Workbook,"#dateFormat(now(),'mm/dd/yyyy')#",3,1);
SpreadSheetSetCellValue(Workbook,"Data Date",5,1);
SpreadSheetSetCellValue(Workbook,"Level",5,2);
SpreadSheetSetCellValue(Workbook,"Name",5,3);
SpreadSheetSetCellValue(Workbook,"Description",5,4);
SpreadSheetSetCellValue(Workbook,"Budget",5,5);
SpreadSheetSetCellValue(Workbook,"Commited",5,6);
SpreadSheetSetCellValue(Workbook,"Spent YTD",5,7);
SpreadSheetSetCellValue(Workbook,"% Spent",5,8);
if (qExecSummary.recordCount) {
rowNum = 6;
//writeOutput(query="qExecSummary");
for(dataRow in qExecSummary){
SpreadSheetSetCellValue(Workbook,dateFormat(qRpt_Date.rpt_Date, 'mm/dd/yyyy'),rowNum,1);
SpreadSheetSetCellValue(Workbook,dataRow.responsible,rowNum,2);
SpreadSheetSetCellValue(Workbook,dataRow.name,rowNum,3);
SpreadSheetSetCellValue(Workbook,dataRow.Description,rowNum,4);
SpreadSheetSetCellValue(Workbook,dataRow.bud_sum,rowNum,5);
SpreadSheetSetCellValue(Workbook,dataRow.committed,rowNum,6);
SpreadSheetSetCellValue(Workbook,dataRow.Spent_YTD,rowNum,7);
if (qExecSummary.bud_sum NEQ 0){
Spreadsheetformatcell(Workbook,formatNum,rowNum,8);
percentSpent="#(qExecSummary.Spent_YTD/qExecSummary.bud_sum)*100#";
SpreadSheetSetCellValue(Workbook,(qExecSummary.Spent_YTD/qExecSummary.bud_sum)*100,rowNum,8);
}
else {
SpreadSheetSetCellValue(Workbook,0,rowNum,8);
}
rowNum++;
}
//End of WriteOutput
} else {
SpreadSheetAddRows(Workbook,"No results for your criteria.");
}
</cfscript>
However it is showing errors on the line with the Output. I don't know what to change it to. I've searched for what is usable in the cfscript tags and found this, but there's nothing there that looks like it would help?
Made update to the <cfscript> now I'm getting this error:
FINAL EDIT
Here's the working script:
<cfsilent>
<!--- *******************************************************************
Filename: execSummary_Excel.cfm, v1.0 03/07/2012
Created By: Original Writer
Description: Excel report export for Executive Summary Report.
Change History:
Date........Name...........Description of Change........................
08/01/2012 Original Writer Added committed column.
02/28/2013 Original Writer Added stateGM and GM.
*************************************************************************--->
<cfinvoke component="financial.financial" method="getExecSummary" returnvariable="qExecSummary">
<cfinvokeargument name="level" value="#URL.level#" />
<cfinvokeargument name="stateGM" value="#URL.stateGM#" />
<cfinvokeargument name="GM" value="#URL.GM#" />
<cfinvokeargument name="engVP" value="#URL.engVP#" />
<cfinvokeargument name="engDir" value="#URL.engDir#" />
</cfinvoke>
<!---Added this to test if I can get more than one sheet to the Workbook--->
<cfinvoke component="financial.financial" method="getExecSummary331" returnvariable="qExecSummary331">
<cfinvokeargument name="level" value="#URL.level#" />
<cfinvokeargument name="stateGM" value="#URL.stateGM#" />
<cfinvokeargument name="GM" value="#URL.GM#" />
<cfinvokeargument name="engVP" value="#URL.engVP#" />
<cfinvokeargument name="engDir" value="#URL.engDir#" />
</cfinvoke>
<!--- Get Report Date since qExecSummary is more complex than the other report queries --->
<cfquery name="qRpt_Date" datasource="#application.dsn#">
SELECT DISTINCT rpt_date
FROM fin_data
</cfquery>
<cfsetting requesttimeout="5000" />
</cfsilent>
<!---Added this as a function that should allow me to use multiple queries and create a workbook with more than one sheet--->
<cfscript>
//Create new workbook with one sheet
//by default that sheet is the active sheet
Workbook = SpreadsheetNew("ExecSummary");
//Add Data to the sheet
//Formatting
format1.bold="true";
format1.fontsize=12;
format1.font="Calibri";
format2.bold="true";
format2.fontsize=18;
format2.font="Calibri";
formatNum.dataformat="0.0%";
//adding the Headers
Spreadsheetformatcell(Workbook,format2,1,1);
Spreadsheetformatcell(Workbook,format1,3,1);
Spreadsheetformatcell(Workbook,format1,5,1);
Spreadsheetformatcell(Workbook,format1,5,2);
Spreadsheetformatcell(Workbook,format1,5,3);
Spreadsheetformatcell(Workbook,format1,5,4);
Spreadsheetformatcell(Workbook,format1,5,5);
Spreadsheetformatcell(Workbook,format1,5,6);
Spreadsheetformatcell(Workbook,format1,5,7);
Spreadsheetformatcell(Workbook,format1,5,8);
SpreadSheetSetCellValue(Workbook,"Executive Summary Report",1,1);
SpreadSheetSetCellValue(Workbook,"#dateFormat(now(),'mm/dd/yyyy')#",3,1);
SpreadSheetSetCellValue(Workbook,"Data Date",5,1);
SpreadSheetSetCellValue(Workbook,"Level",5,2);
SpreadSheetSetCellValue(Workbook,"Name",5,3);
SpreadSheetSetCellValue(Workbook,"Description",5,4);
SpreadSheetSetCellValue(Workbook,"Budget",5,5);
SpreadSheetSetCellValue(Workbook,"Commited",5,6);
SpreadSheetSetCellValue(Workbook,"Spent YTD",5,7);
SpreadSheetSetCellValue(Workbook,"% Spent",5,8);
arr=server.ColdFusion.ProductVersion;
if (qExecSummary.recordCount) {
rowNum = 6;
do {
SpreadSheetSetCellValue(Workbook,dateFormat(qRpt_Date.rpt_date,'mm/dd/yyy'),rowNum,1);
SpreadSheetSetCellValue(Workbook,qExecSummary.responsible[rowNum-5],rowNum,2);
SpreadSheetSetCellValue(Workbook,qExecSummary.name[rowNum-5],rowNum,3);
SpreadSheetSetCellValue(Workbook,qExecSummary.Description[rowNum-5],rowNum,4);
SpreadSheetSetCellValue(Workbook,qExecSummary.bud_sum[rowNum-5],rowNum,5);
SpreadSheetSetCellValue(Workbook,qExecSummary.committed[rowNum-5],rowNum,6);
SpreadSheetSetCellValue(Workbook,qExecSummary.Spent_YTD[rowNum-5],rowNum,7);
if (qExecSummary.bud_sum[rowNum-5] NEQ 0){
Spreadsheetformatcell(Workbook,formatNum,rowNum,8);
SpreadSheetSetCellValue(Workbook,(qExecSummary.Spent_YTD[rowNum-5]/qExecSummary.bud_sum[rowNum-5]),rowNum,8);
}
else {
SpreadSheetSetCellValue(Workbook,0,rowNum,8);
}
rowNum++;
} while (rowNum - 6 LT qExecSummary.recordCount);
} else {
SpreadSheetAddRows(Workbook,"No results for your criteria.");
}
SpreadsheetCreateSheet(Workbook,"ExecSummaryTest");
SpreadsheetSetActiveSheet(Workbook,"ExecSummaryTest");
//Formatting
format1.bold="true";
format1.fontsize=12;
format1.font="Calibri";
format2.bold="true";
format2.fontsize=18;
format2.font="Calibri";
formatNum.dataformat="0.0%";
Spreadsheetformatcell(Workbook,format2,1,1);
Spreadsheetformatcell(Workbook,format1,3,1);
Spreadsheetformatcell(Workbook,format1,5,1);
Spreadsheetformatcell(Workbook,format1,5,2);
Spreadsheetformatcell(Workbook,format1,5,3);
Spreadsheetformatcell(Workbook,format1,5,4);
Spreadsheetformatcell(Workbook,format1,5,5);
Spreadsheetformatcell(Workbook,format1,5,6);
Spreadsheetformatcell(Workbook,format1,5,7);
Spreadsheetformatcell(Workbook,format1,5,8);
SpreadSheetSetCellValue(Workbook,"Executive Summary Report",1,1);
SpreadSheetSetCellValue(Workbook,"#dateFormat(now(),'mm/dd/yyyy')#",3,1);
SpreadSheetSetCellValue(Workbook,"Data Date",5,1);
SpreadSheetSetCellValue(Workbook,"Level",5,2);
SpreadSheetSetCellValue(Workbook,"Name",5,3);
SpreadSheetSetCellValue(Workbook,"Description",5,4);
SpreadSheetSetCellValue(Workbook,"Budget",5,5);
SpreadSheetSetCellValue(Workbook,"Commited",5,6);
SpreadSheetSetCellValue(Workbook,"Spent YTD",5,7);
SpreadSheetSetCellValue(Workbook,"% Spent",5,8);
arr=server.ColdFusion.ProductVersion;
if (qExecSummary331.recordCount) {
rowNum = 6;
do {
SpreadSheetSetCellValue(Workbook,dateFormat(qRpt_Date.rpt_date,'mm/dd/yyy'),rowNum,1);
SpreadSheetSetCellValue(Workbook,qExecSummary331.responsible[rowNum-5],rowNum,2);
SpreadSheetSetCellValue(Workbook,qExecSummary331.name[rowNum-5],rowNum,3);
SpreadSheetSetCellValue(Workbook,qExecSummary331.Description[rowNum-5],rowNum,4);
SpreadSheetSetCellValue(Workbook,qExecSummary331.bud_sum[rowNum-5],rowNum,5);
SpreadSheetSetCellValue(Workbook,qExecSummary331.committed[rowNum-5],rowNum,6);
SpreadSheetSetCellValue(Workbook,qExecSummary331.Spent_YTD[rowNum-5],rowNum,7);
if (qExecSummary331.bud_sum[rowNum-5] NEQ 0){
Spreadsheetformatcell(Workbook,formatNum,rowNum,8);
SpreadSheetSetCellValue(Workbook,(qExecSummary331.Spent_YTD[rowNum-5]/qExecSummary331.bud_sum[rowNum-5]),rowNum,8);
}
else {
SpreadSheetSetCellValue(Workbook,0,rowNum,8);
}
rowNum++;
} while (rowNum - 6 LT qExecSummary331.recordCount);
} else {
SpreadSheetAddRows(Workbook,"No results for your criteria.");
}
SpreadsheetSetActiveSheet(Workbook,"ExecSummary");
</cfscript>
<cfheader name="Content-Disposition" value='attachment; filename="execSummaryNew.xls"'>
<cfcontent type="application/msexcel" variable="#SpreadsheetReadBinary(Workbook)#" reset="true">
WriteOutput() is for displaying data on screen. To add data to a spreadsheet, you need to use spreadsheet functions. In your scenario, you can use a for/in loop to iterate through the query rows. Then use SpreadSheetSetCellValue(sheet, value, row, col) to populate the individual cells.
Update 1:
If you run the code below (from "Edit 2"), in a brand new .cfm script, you will see it works perfectly. So any errors must be coming from a different part of the code. If you are having trouble tracking down errors, I would recommend starting over with a clean slate. Create a brand new script with very simple code: just the minimum necessary to generate the spreadsheet. Omit anything extra like functions, downloads, etcetera and get the basic code working first.
<cfscript>
// FOR DEMO ONLY: Create manual queries
qRpt_Date = queryNew("");
queryAddColumn(qRpt_Date, "Rpt_Date", [now()]);
qExecSummary = queryNew("");
queryAddColumn(qExecSummary, "responsible", [1,12,13]);
queryAddColumn(qExecSummary, "bud_sum", [0,50,100]);
queryAddColumn(qExecSummary, "Spent_YTD", [0,50,100]);
queryAddColumn(qExecSummary, "Name", ["Name A","Name B","Name C"]);
queryAddColumn(qExecSummary, "Description", ["Description A","DescriptionB","Description C"]);
queryAddColumn(qExecSummary, "Committed", [0,50,100]);
//Create new workbook with one sheet
//by default that sheet is the active sheet
Workbook = SpreadsheetNew("ExecSummary");
//Add Headers
headerTextFormat.bold=true;
Spreadsheetformatcell(Workbook,headerTextFormat,1,1);
SpreadSheetSetCellValue(Workbook,"Executive Summary Report",1,1);
Spreadsheetformatcell(Workbook,headerTextFormat,3,1);
SpreadSheetSetCellValue(Workbook, dateFormat(now(),'mm/dd/yyyy'),3,1);
SpreadSheetSetCellValue(Workbook,"Data Date",5,1);
SpreadSheetSetCellValue(Workbook,"Level",5,2);
SpreadSheetSetCellValue(Workbook,"Name",5,3);
SpreadSheetSetCellValue(Workbook,"Description",5,4);
SpreadSheetSetCellValue(Workbook,"Budget",5,5);
SpreadSheetSetCellValue(Workbook,"Commited",5,6);
SpreadSheetSetCellValue(Workbook,"Spent YTD",5,7);
SpreadSheetSetCellValue(Workbook,"% Spent",5,8);
//Add data detail
if (qExecSummary.recordCount) {
// Start populating the spreadsheet on this row number
// Change as needed.
rowNum = 6;
// Loop through query rows
for(dataRow in qExecSummary) {
// add
SpreadSheetSetCellValue(Workbook,dateFormat(qRpt_Date.rpt_Date, 'mm/dd/yyyy'),rowNum,1);
SpreadSheetSetCellValue(Workbook,dataRow.responsible,rowNum,2);
SpreadSheetSetCellValue(Workbook,dataRow.name,rowNum,3);
SpreadSheetSetCellValue(Workbook,dataRow.Description,rowNum,4);
SpreadSheetSetCellValue(Workbook,dataRow.bud_sum,rowNum,5);
SpreadSheetSetCellValue(Workbook,dataRow.committed,rowNum,6);
SpreadSheetSetCellValue(Workbook,dataRow.Spent_YTD,rowNum,7);
if (qExecSummary.bud_sum != 0){
Spreadsheetformatcell(Workbook,formatNum,rowNum,8);
SpreadSheetSetCellValue(Workbook,(qExecSummary.Spent_YTD/qExecSummary.bud_sum)*100,rowNum,8);
}
else {
SpreadSheetSetCellValue(Workbook,0,rowNum,8);
}
rowNum++;
}
//End of WriteOutput
} else {
SpreadSheetAddRows(Workbook,"No results for your criteria.");
}
//For test purposes, save the results to a file
SpreadSheetWrite(Workbook, "c:/path/to/yourFile.xls", true);
WriteOutput("Done!");
</cfscript>
Update 2:
As noted in the comments, for..in query loops are not supported in CF9. If you are using an older version of CF, then you will need to use a from/to loop instead.
// Loop through query rows
for(x = 1; x lte yourQueryName.recordCount; x++) {
SpreadSheetSetCellValue(Workbook, yourQueryName.YourColumnName[x], theSheetRowNum, 1);
// ...
theSheetRowNum++;
}
Side note, personally I prefer cfscript. However, if you feel more comfortable with CFML you can always rewrite the example. Just get rid of the cfscript tags and replace:
<cfscript>
SomeFunctionName(....);
</cfscript>
with:
<cfset SomeFunctionName(....)>

How to get the value of Liferay input editor in a javascript variable

In my application i am using liferay-ui:input-editor .I want to get the value of input editor to a javascript variable, How to achieve that?? I have tried
<liferay-ui:input-editor />
<input name="<portlet:namespace />htmlCodeFromEditorPlacedHere" type="hidden" value="" />
function createPopUp(){
var url ="<%=fetchCandidateByIdForPhoneURL%>";
var type= "fetchCandidateInfo";
var candidateId = $('#candidateID').val();
var jobId = $('#JobList').val();
var text1 = $('#text1').val();
var text2 = $('#text2').val();
var text3 = $('#text3').val();
var interviewVenue = $('#interviewVenue').val();
var interviewCC = $('#interviewCC').val();
var interviewBCC =$('#interviewBCC').val();
var startDate = $('#start-date').val();
var interviewType = $('#interviewType').val();
var x ;
function <portlet:namespace />initEditor() {
return '<font style="font-weight: bold">scott was here</font>';
}
function <portlet:namespace />extractCodeFromEditor() {
var x = document.<portlet:namespace />fm.<portlet:namespace />htmlCodeFromEditorPlacedHere.value = window.<portlet:namespace />editor.getHTML();
alert(x);
}
But it is showing that
ReferenceError: _InterviewSchedule_WAR_InterviewScheduleportlet_initEditor is not defined error. How to resolve it and get the value in a javascript variable
Given the information provided in question, it seems that the javascript initialization function is missing for <liferay-ui:input-editor />. As pointed out in the tutorial here, which OP seems to be using (juding by variable names):
By default, the editor will call "initEditor()" to try and pre-populate the body of the editor. In this example, when the editor loads, it will have the value of "scott was here" in bold.
(...)
function <portlet:namespace />initEditor() {
return '<font style="font-weight: bold">scott was here</font>';
}
By default, the ck editor that Liferay uses will try to call the initEditor() javascript method to try and pre-populate the contents of the editor.
Therefore, you should define such a method, even if you return a blank string.
An example is given below:
<aui:script>
function <portlet:namespace />initEditor() {
return "<%= content %>";
}
</aui:script>
, where content is the string variable with the content you want to pass in when the editor is loaded. If you do not want to pass initial content then simply pass a black string.

Resources