Script files are complete blocks of code that are stored in external files such as .qvs
or .txt
and they can be included in your application with a single reference. They are conceptually similar to the subroutines that are covered in another recipe in this chapter. However, there is a subtle difference in the usage. QVS
simply stands for QlikView Script File.
Everything from data sources, expressions, and visualizations can be governed centrally and the script files can be leveraged in a similar way to help build standards in backend data preparation across multiple applications.
Open a new QlikSense application and create a data connection into a folder where you want to store your script files. As shown in the following example
SUB Calendar(vMinDate,vMaxDate) Let vDiff=vMaxDate-vMinDate+1; Calendar: Load DateID, Year(DateID) As Year, Month(DateID) As Month, Date(DateID) As Date, Day(DateID) As Day, Week(DateID) As Week; Load RecNo()-1+$(vMinDate) As DateID AutoGenerate($(vDiff)); END SUB
Calendar.qvs
into the folder for which you created the data connection. Remember to change the Save as type to All Files, as shown:$(Include=[lib://Scripts/calendar.qvs]);
CALL
statement in the script:CALL Calendar(01/01/2010,Floor(Today())) ;
We have replaced a page of code with just one line using a script file. If you have a code that can be packaged and reused across applications, it makes sense to store this code into a script file for others to use. Doing this reduces the complexity and keeps the focus of new developers on the backend code and on the matter that is relevant to that application.
It's worth pointing out that you could copy the code in the preceding point directly into the script editor and it will still get created and be ready for use. We save this code as a QVS file so that we can load the code using the $(Include…)
statement. Loading the script from external files using the $(Include…)
statement is a good method of reusing blocks of script across applications or using them for source control.