As with every form of scripting, writing QlikView scripts carries with it the risk of introducing bugs and errors. In this section, we will look at some of the available options to find and solve bugs in your script.
Improper use of syntax is a common cause of errors. Fortunately, QlikView has a feature that will catch these errors as they happen: Syntax Check.
As we saw earlier, QlikView script has syntax highlighting. Whenever incorrect syntax is detected, the statement is underlined in a red squiggly from that point onward. In practice, this means that often the error was made in the line that appears before the red underlined text. The following screenshot shows a piece of script with a syntax error, see if you can see what the error is.
If you looked closely at the script in the previous picture, you will have noticed that there was a comma missing after the TableName
column. This causes the statement to be underlined in red from that point onward.
Besides...
Standardizing and organizing script
Have you ever experienced the following situation? A challenging problem presents itself. After many hours of thinking, developing, thinking some more, and developing some more, you have crafted a solution. "This is brilliant work!" you say to yourself, "It completely solves the problem, and in a very elegant way too."
Fast-forward a few months. A new business question presents itself and a small change to your original solution is required. You look at your original work, and after some poking around decide that you can make neither head nor tail of it."This is horrible work!" you say to yourself, "What was I thinking at the time?".
Did your script really go from brilliant to rubbish over the course of 6 months? Most likely not. You have just lost familiarity with the script. Fortunately, there are ways to ensure that you (and others) are able to quickly get up to speed when modifying existing QlikView script. The secrets are organizing your scripts and...
When developing QlikView documents, we often have to apply the same set of logic or transformations to different data. I have often observed QlikView developers taking a copy-paste approach for re-using a script. While this approach may initially work, it does make the script a lot harder to maintain. When something needs to be changed, you need to change it in each instance of the script, running the risk of different versions of the same transformation process.
In this section, we will look at two better approaches for re-using data. The first is the use of subroutines, which can be used to re-use script within an document. The second is the use of include files, which enables re-use of script between different documents.
A subroutine is a reusable block of script that can be called from other places in the QlikView script by using the CALL
statement. This block is formed using the SUB
and END SUB
control statements. Subroutines can contain parameters so that...
Managing file locations and connection strings
In our current example documents, we have always referred to the Data Files
folder for our source data. If, for any reason, this folder has to be moved somewhere else, we will have to manually change the source data path in many locations in many files.
Let's follow these steps to create an included script file to set the source data folder in a single location:
Open Notepad (by pressing Windows Key + R, typing in notepad
, and pressing Return) or any other text editor.
Enter the following script:
Save the file to the same folder as your QlikView document and call it Config.qvs
.
Close Notepad and return to QlikView's script editor window.
Go to the Include tab and place the cursor on the first line.
Go to Insert | Include Statement and select the Config.qvs
file.
We have now created an include file that sets the vFolderSourceData
variable to the path of the source data folder.
Note
In a real QlikView environment...
We have come to the end of this chapter on scripting. We have learned how to navigate the script editor. We have seen the most important script statements, and have applied them to our Airline Operations project. We also picked up a few tips and pointers for working with different data types in QlikView, and for debugging, standardizing, and organizing our scripts. We ended this chapter by looking at script re-using, which makes our scripts easier to maintain.
In short, we learned about the Script Editor window, what the most important script statements are and how to use them, operators and functions for dealing with various data types, and what options exist to debug a script.
We also learned how to organize and standardize our script and how to re-use your script within and between QlikView documents.
Now that we have learned the basics of QlikView scripting, in the next chapter we will apply this new knowledge while learning about data modeling best practices.