Custom Function Introduction

  • Last update:February 20, 2025
  • Overview

    Application Scenario

    Abundant built-in functions are provided in FineBI, which are sufficient to meet your normal needs in template design. However, in some special fields, you may require special functions. In this case, you can define custom functions in FineBI according to your business needs. However, these functions must comply with FineBI function definition rules.

    iconNote:
    Only extracted data is supported.

    Applied Scope

    Definition Rule

    FineBI function definition rules: Functionname(Para,Para,...), where Functionname is the function name and Para is the parameter.

    In FineBI, each function is defined as a class, which must help to realize the Function as an interface. When performing the calculation, the class that performs the function is obtained through reflection based on the function name, and then its run(Object[] args) method is called.

    Take the SUM function as an example. The SUM class is used to perform calculations of the SUM function, and the class inherits the AbstractFunction class, which implements the Function interface.

    When performing the calculation, the function first obtains the class that performs the function based on the function name. For example, the function SUM(2,4,true) first obtains the SUM class based on the function name, and then call the run(Object[] args) method of the SUM class. The args array contains the parameters of the SUM function, and the parameters can be retrieved from the args array for calculation. For example, the execution result is as follows: SUM(2,4,true)=2+4+1=7.

    Code used by the SUM function:

    package com.fr.report.script;
    import com.fr.general.FArray;
    import com.fr.script.AbstractFunction;
    public class CSUM extends AbstractFunction {
    public Object run(Object[] args) {
    double result = 0;
    for (int i = 0; i < args.length; i++) {
    if (args[i] == null) {
    continue;
    }
    result += parseObject(args[i]);
    }
    return result;
    }
    private double parseObject(Object obj) {
    if (obj instanceof Number) {
    return ((Number) obj).doubleValue();
    else if (obj instanceof Boolean) {
    return ((Boolean) obj).booleanValue() ? 1 : 0;
    else if (obj instanceof FArray) {
    FArray array = (FArray) obj;
    double sum = 0;
    for (int i = 0; i < array.length(); i++) {
    sum += parseObject(array.elementAt(i));
    }
    return sum;
    else if (obj != null) {
    try {
    return Double.parseDouble(obj.toString());
    catch (NumberFormatException exp) {
    return 0;
    }
    }
    return 0;
    }
    }
    显示代码

    Compiling Environment Preparation

    For details, see Compiling a Java Program.

    Example

    Take a simple custom function as an example to explain the use of the custom function.

    Define a function called StringCat, which is used to combine all the parameters into a character string.

    For details, see: Custom Function for Combination as Character String.

    Notes

    1. The custom function name is based on the class file name.

    2. The custom function file name only supports English, including uppercase, lowercase, and mixed case input.

    3. The added functions are displayed in Other Functions.

    4. Mixed case input is currently not supported.

    附件列表


    主题: Advanced Data Analysis
    Previous
    Next
    • Helpful
    • Not helpful
    • Only read

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

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

    不再提示

    7s后關閉

    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