Monday, 13 October 2014

Java Script to export cognos report into Excel & PDF

 

    Export Cognos Report into Excel and PDF   using Java script.

 

Requirement:  User wants a Button to Export Report HTML output into Excel & PDF both on a click.
Solution: Using HTML Items and Java scripting this can be achieved.

Put two HTML Items in the report where you want to create Export Button.

Copy the below mentioned code and paste into HTML items accordingly.

Script for HTML 1:
<SCRIPT>
<!--
function reRunExcel(){
//while Running report
Try
{
oCV_NS_.getRV().viewReport('spreadsheetML');
}
catch(err) {}
//while Testing report
try
{
oCVRS.getRV().viewReport('spreadsheetML');
}
catch(err) {}
}
//-->
</SCRIPT>
<A href="javascript:reRunExcel();"> </a>

Script for HTML 2:
<html>
<head>
<script language="javascript">
function gotourl()
{var obj=document.all['OutputFormat'];
var value="PDF";
window.onload(gCognosViewer.getRV().viewReport (value));
function reRunExcel(){
//while Running report
try
{
oCV_NS_.getRV().viewReport('spreadsheetML');
}
catch(err) {}
}
}
</script>
</head>
<body>
</body>
<button onclick="gotourl(); reRunExcel();">Export to Excel/PDF </Button>

On running this report you will get a Button to Export report into Excel & PDF.


Thursday, 9 October 2014

Java Script for IBM Cognos Multi Select Tree Prompt

Restrict Multi Select Tree Prompt Different Levels


Introduction


This document describes a technique for Cognos report developers  wish to utilize a multi select Tree Prompt but require that the value submitted be restricted to a designated level within a hierarchy and user will get warning message in case of wrong selection.

Requirement


Requirement is that user should select value of a single level of hierarchy say either Month or Quarter but not Month and Quarter. This functionality is not available in Cognos.


This requirement can be fulfilled using Java Script.


Create simple list report.
Use GO Data Warehouse (analysis) -->Sales and Marketing (analysis)-->Advertising-->Time



Create a multi select optional Tree Prompt above the list report using same Time dimension. Drag two HTML items before and after tree prompt




Give the name of Tree Prompt as ‘T’.



Open first HTML item and put the below

HTML1:

<script>
function test()
{
                var tree =  window.treeT;
                var selectedNodes = getSelected(tree.getRootNode());
                var fist_val=selectedNodes[0];
                var len=selectedNodes.length;

                var bl=1;
                for (i=1;i<len;i++)
                                {
                                if(selectedNodes[i]==fist_val)
                                {
                                bl=1;
                                }
                                else
                                {
                                bl=0;

                                alert("You Can Not Select Two Different Levels");
                                break;
                               
                           

}
if(bl==0)
{
break;
}

                                }
if(bl==1)
{
promptButtonFinish();
}
               
}
function getSelected(node)
{
                if (!node) {
                                node = this.getRootNode();
                }

                var selectedNodes = new Array();

                if (node.hasSelectedChildren()) 
                {
                                //get any children
                                var children = node.getChildren();

                                //move recursively and find all children
                                for (var iChildCounter = 0; iChildCounter < children.length; iChildCounter++)
                                {
                                                var theSelectedNodes = this.getSelected(children[iChildCounter]);
                                                for (i=0;i<theSelectedNodes.length;i++)
                                                {
                                                                selectedNodes[selectedNodes.length] = theSelectedNodes[i];
                                                }
                                }
                }
                if (node.isSelected())
                {
                                selectedNodes[selectedNodes.length] = node.getLevel();
                }

                return selectedNodes;
};
</script>



Now open second HTML item and put the below mentioned code:

HTML2:

<input type="button" value="Submit" onClick="test()"/>


Now run the report, it will appear as below. Select Quarter value of any year and different Year value and click on Submit.
Pop up message will come



Now select any value of same level and click submit


The report runs and filters the report based on the tree prompt selection.