Cognos Analytics 11.1.7IF9
Searching the web for "Cognos report copy" produces many results about copying a report spec to the clipboard. That's not what this question is about.
Using the interactive viewer in Cognos Analytics means users can't select text to copy from the report and paste into other apps. A user has approached me with the requirement to copy individual values from a report output to the clipboard.
Running the report to the HTML output format is preferred because of some interactive capabilities in the report. Running to PDF would create an additional workload.
I can write web page that includes a script to make clicking on items I choose copy them to the clipboard:
(Adapted from https://www.w3schools.com/jquery/tryit.asp?filename=tryjquery_hide)
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script>
$(document).ready(function(){
var arr = ["city", "country"];
arr.forEach(function(e){
$("span[lid='" + e + "']").click(function() {
navigator.clipboard.writeText($(this).html());
});
});
});
</script>
</head>
<body>
<span lid="city">Click me to copy my City contents to the clipboard.</span><br /><br />
<span lid="country">Click me to copy my Country contents to the clipboard.</span><br /><br />
<span lid="region">Region contents are not copied</span><br /><br />
<span>I also do nothing</span><br /><br />
</body>
</html>
But I'm not having success with this in Cognos. I have attempted this by creating a sample report...
<report xmlns="http://developer.cognos.com/schemas/report/15.5/" useStyleVersion="11.6" expressionLocale="en-us">
<drillBehavior/>
<layouts>
<layout>
<reportPages>
<page name="Page1">
<style>
<defaultStyles>
<defaultStyle refStyle="pg"/>
</defaultStyles>
</style>
<pageBody>
<style>
<defaultStyles>
<defaultStyle refStyle="pb"/>
</defaultStyles>
</style>
<contents>
<list horizontalPagination="true" name="List1" refQuery="Query1" rowsPerPage="9999">
<noDataHandler>
<contents>
<block>
<contents>
<textItem>
<dataSource>
<staticValue>No Data Available</staticValue>
</dataSource>
</textItem>
</contents>
<style>
<CSS value="padding:16px;"/>
</style>
</block>
</contents>
</noDataHandler>
<style>
<CSS value="border-collapse:collapse"/>
<defaultStyles>
<defaultStyle refStyle="ls"/>
</defaultStyles>
</style>
<listColumns>
<listColumn>
<listColumnTitle>
<style>
<defaultStyles>
<defaultStyle refStyle="lt"/>
</defaultStyles>
</style>
<contents>
<textItem>
<dataSource>
<dataItemLabel refDataItem="City"/>
</dataSource>
</textItem>
</contents>
</listColumnTitle>
<listColumnBody>
<style>
<defaultStyles>
<defaultStyle refStyle="lc"/>
</defaultStyles>
</style>
<contents>
<textItem name="city">
<dataSource>
<dataItemValue refDataItem="City"/>
</dataSource>
</textItem>
</contents>
</listColumnBody>
</listColumn>
<listColumn>
<listColumnTitle>
<style>
<defaultStyles>
<defaultStyle refStyle="lt"/>
</defaultStyles>
</style>
<contents>
<textItem>
<dataSource>
<dataItemLabel refDataItem="Year"/>
</dataSource>
</textItem>
</contents>
</listColumnTitle>
<listColumnBody>
<style>
<defaultStyles>
<defaultStyle refStyle="lc"/>
</defaultStyles>
</style>
<contents>
<textItem>
<dataSource>
<dataItemValue refDataItem="Year"/>
</dataSource>
</textItem>
</contents>
</listColumnBody>
</listColumn>
<listColumn>
<listColumnTitle>
<style>
<defaultStyles>
<defaultStyle refStyle="lt"/>
</defaultStyles>
</style>
<contents>
<textItem>
<dataSource>
<dataItemLabel refDataItem="Revenue"/>
</dataSource>
</textItem>
</contents>
</listColumnTitle>
<listColumnBody>
<style>
<defaultStyles>
<defaultStyle refStyle="lm"/>
</defaultStyles>
</style>
<contents>
<textItem>
<dataSource>
<dataItemValue refDataItem="Revenue"/>
</dataSource>
</textItem>
</contents>
</listColumnBody>
</listColumn>
</listColumns>
<sortList>
<sortItem refDataItem="City"/>
<sortItem refDataItem="Year"/>
</sortList>
</list>
<customControl path="/CognosScripts/Copy.js">
<configuration>{
"SpansToCopy": [
"city"
]
}</configuration>
</customControl>
</contents>
</pageBody>
<XMLAttributes>
<XMLAttribute output="no" name="RS_legacyDrillDown" value="0"/>
</XMLAttributes>
</page>
</reportPages>
</layout>
</layouts>
<XMLAttributes>
<XMLAttribute output="no" name="RS_CreateExtendedDataItems" value="true"/>
<XMLAttribute output="no" name="listSeparator" value=","/>
<XMLAttribute output="no" name="decimalSeparator" value="."/>
<XMLAttribute output="no" name="RS_modelModificationTime" value="2015-11-25T21:38:24.820Z"/>
</XMLAttributes>
<queries>
<query name="Query1">
<source>
<model/>
</source>
<selection>
<dataItem aggregate="none" rollupAggregate="none" name="City">
<expression>[Sales (query)].[Branch].[City]</expression>
<XMLAttributes>
<XMLAttribute output="no" name="RS_dataType" value="3"/>
<XMLAttribute output="no" name="RS_dataUsage" value="0"/>
</XMLAttributes>
</dataItem>
<dataItem aggregate="total" name="Revenue">
<expression>[Sales (query)].[Sales].[Revenue]</expression>
<XMLAttributes>
<XMLAttribute output="no" name="RS_dataType" value="2"/>
<XMLAttribute output="no" name="RS_dataUsage" value="2"/>
</XMLAttributes>
</dataItem>
<dataItem aggregate="none" rollupAggregate="none" name="Year">
<expression>[Sales (query)].[Time].[Year]</expression>
<XMLAttributes>
<XMLAttribute output="no" name="RS_dataType" value="1"/>
<XMLAttribute output="no" name="RS_dataUsage" value="0"/>
</XMLAttributes>
</dataItem>
</selection>
</query>
</queries>
<modelPath>/content/folder[@name='Samples']/folder[@name='Models']/package[@name='GO sales (query)']/model[@name='model']</modelPath>
<reportName>copy</reportName>
</report>
...and adding a custom control:
define( ["jquery"], function() {
"use strict";
var log = function (label, message) {
console.log(" **** Copy : " + label + " : " + message);
};
function Copy() {};
Copy.prototype.initialize = function( oControlHost, fnDoneInitializing ) {
log("Copy", "Control.initialize" );
/*
Sample Configuration:
{
"SpansToCopy": ["city", "country"]
}
If I name a text item in the report, the resulting span is given an attribute named "lid".
*/
this.controlHost = oControlHost;
this.oConfig = this.controlHost.configuration;
$(document).ready(function(){
this.oConfig.SpansToCopy.forEach(function(e){
$("span[lid='" + e + "']").click(function() {
navigator.clipboard.writeText($(this).html());
});
});
});
fnDoneInitializing();
};
Copy.prototype.draw = function( oControlHost ) {};
return Copy;
});
I'm getting...
jQuery.Deferred exception: e is not a function TypeError: e is not a function
...when the $(document).ready
function runs.
Is there some incompatibility between current jQuery code and the version of jQuery Cognos is using?
Is there some scope issue I'm not seeing?
Other than reverting to the compatible viewer, is there a better way to enable the user to copy from the report output?