Extract the data from QlikView onPostReload:
Requirement: Generate a .csv file of a table/chart on the QlikView application automatically after scheduled reloads. Procedure: The Qlikview Publisher does a good job at scheduling the reload and distribution tasks. However, it disables the On Post Reload triggers to execute a macro to extract a table/chart data to an external file. To overcome this roadblock we need to run the Qlikview application reload using a batch process on the windows server, the Qlikview Desktop and the distribution service module. At first lets take a look at the macro that will generate our table extract file. To accomplish this task we can use one of the many export routines available in the API Guide. In this example, we will use the Export routine as shown below.
Sub ExportToCSV set sObject1 = ActiveDocument.GetSheetObject ("CH01") sObject1.Export "C:\Users\<Username>\Desktop\Sales_Table.csv", ",", 65001 End Sub
The above macro needs to be executed after the application is reloaded using a Run Macro action and On Post Reload Trigger. The above macro can also be customized to print a different separator by updating the (,) portion of the script on line 5. The number 65001 is to format the document in the UTF-8, this can be changed to other codepages as shown in the following link at Wikipedia. Link to codepages: http://en.wikipedia.org/wiki/Code_page Set the trigger to run the above macro as shown in the screen shot below:
The next step is to check a few settings on the User Preferences, so that the application runs fine in the batch mode and the data formatting is kept in the extracted files. 1. General Properties: Uncheck the Keep Progress Open after Reload.
2. Export Tab: Choose Full Formatting and Utf-8.
3. Security tab: Check the Module, Launch and File options and uncheck the others.
Once, these settings are completed, we need to create the batch file that will open the QV app in the desktop client, reloads it and extracts the data into desired file type and locations. The minimalistic batch file is as shown below.
@echo off C:\Program Files\QlikView\qv.exe -r "C:\Users\<Username>\Desktop\Extract.qvw echo Error code: %ERRORLEVEL% Exit
However, we can add a little more functionality to keep track of the batch process start time and end time as shown in the Appendix. Note: 1. It is necessary that the account, under which the batch process will be run, needs to have a write access to the extract file location. 2. To use the batch distribution process the following value in "QlikViewDistributionService.exe.config", available in "D:\Program Files\QlikView\Distribution Service\", needs to be set to true <add key="EnableBatchMode" value="false"/> while executing on a server.
APPENDIX:
Full batch file:
@echo off REM ************************************************************************************* REM Program Varaibles REM ------------------------------------------------------------------------------------REM For running on server use the AppName as below: REM "D:\Program Files\QlikView\Distribution Service\QVDistributionService.exe" REM For running on Desktop use the following for AppName: REM "C:\Program Files\QlikView\qv.exe" set AppProgram1=C:\Program Files\QlikView\qv.exe echo The AppProgram is set to: %AppProgram1% REM ------------------------------------------------------------------------------------REM Set the QvwName & ProcessName to the Qlikview App that is being reloaded & Distributed REM In the present case, Use "Sales Reports.qvw" set QvwName=Extract.qvw echo The QvwName is set to: %QvwName% set ProcessName=Extract.qvw echo The ProcessName is set to: %ProcessName% REM ------------------------------------------------------------------------------------REM The QvwPath is the path to the document that we intend to reload. REM On Server: "D:\QlikView\PublisherSourceDocuments\QvwFrontEnd" set QvwPath=C:\Users\*****\Desktop\ echo The QvwPath is set to: %QvwPath% REM ************************************************************************************* REM REM The following value in "QlikViewDistributionService.exe.config", available in REM "D:\Program Files\QlikView\Distribution Service\", needs to be set to true REM <add key="EnableBatchMode" value="false"/> REM while executing on server REM REM ************************************************************************************* REM Execution REM ------------------------------------------------------------------------------------REM REM THE COMMAND TO Distribute SERVER: REM "%AppProgram%" -rp="%QvwPath%\%QvwName%" -out="%QvwPath%\%ProcessName%.log" REM REM THE COMMAND TO RELOAD ON LAPTOP REM "%AppProgram%" -r "%QvwPath%\%QvwName%" REM echo Execution started on %date% at %time% >>"%QvwPath%\%ProcessName%_BatchStart.log" "%AppProgram1%" -r "%QvwPath%\%QvwName%" echo Error code: %ERRORLEVEL% on %date% at %time% >> "%QvwPath%\%ProcessName%_BatchEnd.log" Exit