String splicing with custom function

  • Last update:  2022-04-15
  • 1. Overview

    1.1 Expected effect

    You can use the StringCat function to splice strings in BI. For example, splice the "area" and "hierarchical market" under the "AC-sales" into one field, as shown in the following figure:

    Note: the functions and fields in the formula box need to be selected by clicking the selection area on the left, and cannot be entered manually.

    1.2 Implementation ideas

    "AbstractFunction" implements the "Function" interface, so StringCat can directly inherit the "AbstractFunction" class.

    When using the function StringCat (Para, Para, Para,...), obtain the class StringCat that operates the function according to the function name, pass the parameters into the args object array in the class, and execute the run function of the class. In the run function, the incoming parameters are connected in the form of strings. And returns the resulting string.

    Note: please refer to file: "User defined function introduction" for more details of user defined function definition rules and precautions.

    1.3 Function introduction

    Function StringCat :the function is to connect all parameters in the form of string.

    StringCat's usage rules are StringCat(Para,Para,Para…….), where "Para" is an unlimited number of parameters of the function.

    2. Example: Method 1

    2.1 Write Java files

    Write StringCat.java. The function code is as follows.

    package com.fr.function;
    import com.fr.script.AbstractFunction;
    public class StringCat extends AbstractFunction {
    public Object run(Object[] args) {
    String result = "";
    Object para;
    for (int i = 0; i < args.length; i++) {
    para = args[i];
    result += para.toString();
    }
    return result;
    }
    }

    2.2 Compiling class files

    2.2.1 Compilation environment preparation

    For more details, see file: "Compiling Java programs".

    2.2.2 Generate class file

    Compile StringCat.java, generate the class StringCat.class.

    2.2.3 Import class file

    Put the compiled StringCat.classinto finebi installation directory%FineBI%\webapps\webroot\WEB-INF\classes\com\fr\function, as shown in the following figure:

    Note: if there is no corresponding directory under WEB-INF, you can create a new directory.

    3.png

    2.3 Use custom functions

    After  restarting FineBI, it can be used directly. The method is the same as the built-in function.

    For example, in adding "calculation indicators", you can see the previously customized function StringCat below other functions. Click this function to add it to the formula, as shown in the following figure:

    1649044688291654.png

    Splice the "area" and "hierarchical market" under the "AC-sales" into one field, and enter the formula: StringCat(${area}," ",${hierarchical Market}), as shown in the following figure:

    5.png

    Note: the functions and fields in the formula box need to be selected by clicking the selection area on the left, and cannot be entered manually.

    2.4 View the effects

    See Section 1.1 of this file for details.

    3. Example: Method 2

    Refer to section 2.1, 2.2.1, 2.2.2 of this file to compile and generate the StringCat.class.

    3.1 Import class file

    Store the StringCat.class file in the %FineBI%\webapps\webroot\WEB-INF\classes\com\fr\function , as shown in the following figure:

    6.png

    The file can be stored anywhere in the FineBI project directory, but it should correspond to the location under package in the java file. For example, this example is:

    package com.fr.function;

    Therefore, you need to store the class file under the %FineBI%\webapps\webroot\WEB-INF\classes\com\fr\function file. If the codes are:

    package com;

    Then you can store the file under the %FineBI%\webapps\webroot\WEB-INF\classes\com .

    3.2 Register custom functions

    After generating the class of this function, you need to register in FineReport designer before you can use this function.

    See the file "FineReport designer remotely connect to FineBI project", open "Server" → "Function Manager", add a function and name it as "StringCat", select the defined StringCat.class and save it, as shown in the following figure:

    7.png

    3.3 Use custom functions

    After  restarting FineBI, it can be used directly. The use method is the same as the built-in function.

    Note: if the remote connection is cancelled after registration, the user-defined function can still be used.

    3.4 View the effect

    See more details in section 1.1.

    附件列表


    主题: Advanced Data Analyis
    • Helpful
    • Not helpful
    • Only read

    滑鼠選中內容,快速回饋問題

    滑鼠選中存在疑惑的內容,即可快速回饋問題,我們將會跟進處理。

    不再提示

    10s後關閉

    Get
    Help
    Online Support
    Professional technical support is provided to quickly help you solve problems.
    Online support is available from 9:00-12:00 and 13:30-17:30 on weekdays.
    Page Feedback
    You can provide suggestions and feedback for the current web page.
    Pre-Sales Consultation
    Business Consultation
    Business: international@fanruan.com
    Support: support@fanruan.com
    Page Feedback
    *Problem Type
    Cannot be empty
    Problem Description
    0/1000
    Cannot be empty

    Submitted successfully

    Network busy