Qlik Community

QlikView Documents

Documents for QlikView related information.

Announcements
QlikWorld, June 24-25, 2020. Free virtual event for DI and DA gurus. Register Today

Useful Qlikview Javascript for Chrome Ajax client

Creator
Creator

Useful Qlikview Javascript for Chrome Ajax client

inspired by Useful Qlikview Macros

These javascript code snippets can be used in the ajax client in Chrome. (not webview since that is IE and making things work in IE is painful)

All of these are wrapped in anonymous function that are immediately called, so they can be used in a button 'open URL' action in the same window like below so you don't have to install any extensions. You have to modify to code to do more things together for it to be useful since single actions can be done without any javascript coding. 

image.png

 

1) Download a pivot table or straight table chart object as csv:

Spoiler
(function(){
    let convertObjectDataToCSV = function (data) {
        let result,columnDelimiter, lineDelimiter, strDataCell;
        columnDelimiter =  ',';
        lineDelimiter =  '\n';
        
        let wrapCSVCell = function(strCell){
            if( typeof strCell === "string") {
                    strCell = strCell.replace(/"/g, '""');
                    if (strCell.search(/("|,|\n)/g) >= 0){
                            strCell = '"' + strCell + '"';
                    }
            }
            return strCell;
        };
        
        result = '';
        result += data.HeaderRows.map(r => r.map(c => wrapCSVCell(c.text)).join(columnDelimiter)).join(lineDelimiter);
        result += lineDelimiter;
        result += data.Rows.map(r => r.map(c => wrapCSVCell(c.text)).join(columnDelimiter)).join(lineDelimiter);
        
        return result;
    };
    let saveAsCsv = function(filename, data) {
        let blob = new Blob([data], {type: 'text/csv'});
        let elem = window.document.createElement('a');
        elem.href = window.URL.createObjectURL(blob);
        elem.download = filename;        
        document.body.appendChild(elem);
        elem.click();
        window.URL.revokeObjectURL(elem.href);
        document.body.removeChild(elem);
    };

    let GetObject = function(doc,objectID){
        return new Promise(function(resolve){
            doc.GetObject(objectID,function(){
                this.DocumentMgr.RemoveFromManagers(this.ObjectMgr);
                this.DocumentMgr.RemoveFromMembers(this.ObjectMgr);
                resolve(this);
            });
        });
    };
    
    let GetObjectFullDataPage = async function (doc,objectID){
        let tableObj = await GetObject(doc,objectID);
        tableObj.Data.SetPagesize(tableObj.Data.TotalSize);
        return GetObject(doc,objectID);
    };
    
    let exportTableAsCSV = async function(doc, objectID, filename){
        saveAsCsv(filename,convertObjectDataToCSV((await GetObjectFullDataPage(doc,objectID)).Data));
    };

    exportTableAsCSV(Qv.GetCurrentDocument(),"CH01","export.csv");

}());
Labels (1)
Comments
Contributor II
Contributor II

I tried your example for downloading a straight table chart object as csv, but it doesn't work.

 

I get the following error, when I click "Check" in the Edit Module:

 

"Expected ';'" and it highlights the following:

let convertObjectDataToCSV = function (data) {

 

What am I doing wrong?

I want to have a macro button in my QlikView Application, so when the end user clicks on it from the Web View, then he gets a prompt to download a CSV extract of the straight table on any location he wants to.

 

0 Likes
Creator
Creator

It doesn't go into the edit module. You just put it in the url like in the screenshot.

0 Likes
Version history
Revision #:
4 of 4
Last update:
‎2020-03-09 10:59 PM
Updated by:
 
Contributors