Formulas and scripts

Formulas

Formulas are lightweight expressions designed for calculations, formatting, and conditions directly inside labels and data fields.

Formulas are used in the program for the following purposes:

  • To display variable data in label elements. Variable data includes values from data source fields, calculated data fields, counters, and system values such as the current date.
    The string returned by the formula will be displayed by the label element when the label is printed or previewed.
    Also, a formula in a label element that displays an image can return the path to an image file or a link to an image.

  • To set various conditions. For example, to set visibility conditions for label elements/layers or to filter your data by certain conditions when importing data from your source.

  • As a data source for calculated data fields. A calculated data field is a data field configured with a formula. Such a data field will contain what this formula will return. For example, you can add a data field to the label print data and set a formula in the settings of this field, according to which this field will display modified data from another data field.

In formulas, you can use string, number, and date/time operations, as well as number and date/time formatting. Various functions are used for this.

To insert and edit formulas in the program, there is a formula designer, which is described in the Formula designer section.

Formulas look like:

[Initial value] [Function 1] [Function 2] ... [Function N] [/*Your comment*/]

A formula consists of an optional initial value followed by one or more functions.
Each function processes the result of the previous step.

The initial value can be any valid data type. It may be absent explicitly, then it will be considered an empty string.

Types of data in formulas

  • Text. Can be enclosed in single or double quotes. We recommend using single quotes ('...'), especially in API usage to avoid escaping issues.

    Examples: 'This is a text', "This is a text"

  • Number.

    Example: 123.4

  • Data field. Is enclosed in square brackets.

    Examples:

    • [Price]
    • [CurrentDate], here the virtual data field, which contains the current date.

Functions in formulas

Functions perform various actions: arithmetic operations, processing of strings and dates, formatting, etc.

Functions in a formula are executed sequentially from left to right.
The initial value of the formula acts as an input value for the first function, for all subsequent functions the input value is the result of processing from the previous function. That is, the first function in the formula processes the initial value, the second processes the result of the first, the third processes the result of the second, and so on.

Functions can include other formulas as arguments.

Examples of using functions:

  • String()

    The function converts the input value to a string.

  • +(10)

    The function adds the number 10 to the input value.

  • If(>10,1,0)

    The function returns 1 if the input value is greater than 10, otherwise it returns 0.

  • Round( If( [Price]>10,"1","0.5" ) )

    The function rounds the input value to 1 if Price data field contains a value greater than 10, otherwise it rounds the input value to 0.5.
    The formula If( [Price]>10,"1","0.5" ) is used as a function argument that determines the rounding accuracy.

Examples of formulas with functions:

  • 100 AddPercent( 5 )

    The AddPercent() function will add 5 percent to 100 and return the resulting value 105.

  • [Price] AddPercent( 12.3 ) Round( "0.5" )

    AddPercent() function will add 12.3 percent to the price of the item in the Price data field, then Round() function will round the resulting value to a precision of 0.5.

  • If([Name] Contains("pro") Or ([Name] Contains("corporate")),"It is Pro or Corporate","Other")

    In this example, the functions If, Contains and Or are used.
    The formula checks if Name field contains the words pro or corporate, then returns the string It is Pro or Corporate, otherwise returns the string Other.

    String comparisons are case-insensitive.

JavaScript and Pascal scripts

Scripts are full programming blocks used when formulas are not flexible enough, for example, for complex logic, external data access, or automation tasks.

JavaScript and Pascal scripts are used in the program for the following purposes:

  • To display variable data in label elements.

  • As a data source for label printing, where the script returns values used by label elements or printing logic.

  • As scripts to be executed when tasks are started.

Script examples:

Both scripts check if the Name field contains the words pro or corporate, then the string It is Pro or Corporate is returned, otherwise the string Other is returned.

  • JavaScript.

    let _ = azureLabel;
    let name = _.getFieldValue('Name');
    if (name.includes('Pro') || name.includes('Corporate')) {
        _.write('It is Pro or Corporate');
    }
    else {
        _.write('Other');
    }

  • Pascal script.

    var name: string;
    begin
        name := GetFieldValue('Name');
        if ( (pos('Pro', name) > 0) or (pos('Corporate', name) > 0) ) then
        begin
            Write('It is Pro or Corporate');
        end
        else
        begin
            Write('Other');
        end;
    end.