Packaging the code in script files

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.

Getting ready

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

Getting ready

How to do it…

  1. Open a Notepad document.
  2. Copy the following subroutine script (a simplified version of the calendar code from the previous recipe) into the Notepad document:
    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
  3. Save the Notepad file as Calendar.qvs into the folder for which you created the data connection. Remember to change the Save as type to All Files, as shown:
    How to do it…
  4. Add the following line of code to your application:
    $(Include=[lib://Scripts/calendar.qvs]);
  5. You can now call the same subroutine without seeing the code, as in the previous example. In order to generate a calendar, use the following CALL statement in the script:
    CALL Calendar(01/01/2010,Floor(Today())) ;

How it works…

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.

See also

  • How to use sub routines in Qlik Sense®
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset