Custom functions for report development
For Report development, in many cases, built-in functions can meet the report production requirements of most users, and FineReport is no exception. However, some special functions may be required in some special fields. In this case, FineReport provides a custom function mechanism that allows you to define some functions based on your business needs, however, these functions must meet the function definition rules.
First, let's take a look at the FineReport Function Definition rules: Functionname (Para, Para,...), where Functionname is the function name and Para is the parameter.
Every Function is defined as a class. This class must implement the Function interface. This class is first obtained through Function name reflection during operation, then, call its run (Object [] agrs) method. The following uses the SUM function as an example.
Principle of SUM function
The program can see that the SUM class is used to calculate the SUM Function. It inherits the AbstractFunction class, And the AbstractFunction implements the Function interface.
When performing a function operation, first obtain the class that computes the function based on the function name, for example, SUM (, true). Then, the function obtains the SUM Class Based on the function name, then call the run (Object [] args) method of the SUM class. The args stores the parameters of the SUM function. During calculation, you can obtain the parameters from args for calculation. For example, the execution result is SUM (2, 4, true) = 2 + 4 + 1 = 7.
Code used by the SUM function:
package com.fr.report.script;import java.lang.reflect.Array;import com.fr.report.script.core.FArray;import com.fr.report.script.core.FunctionHelper;public class SUM 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 FunctionHelper.parsePrimitiveDouble(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;}}
Steps
Compile User-Defined Functions
The following uses a simple UDF example to describe how to use a UDF. We define a function StringCat, which is used to connect all parameters in the form of strings.
The use rule of the StringCat function is StringCat (Para, Para, Para .......);
In this example, Para is a parameter of this function and the number of parameters is unlimited.
According to the overview, the Function interface is implemented by AbstractFunction. Therefore, StringCat can directly inherit the Function class. The complete 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;}}
Note that the StringCat (Para, Para, Para...) function is used .....) Obtain the StringCat class for this function according to the function name, pass the parameter to the args object array in the class, and execute the run function of this class.
In the run function, the input parameters are connected as strings. Returns the final string.
Compile custom functions
Compile StringCat. class put in the FineReport installation directory under the classes directory under the WEB-INF, because StringCat. java is a package com. fr. function, so StringCat. class must be placed in the classes \ com \ fr \ function directory.
Register a UDF
The class that generates the function must be registered in the designer before it can be used. Open the server | function manager, and select the StringCat class just defined, as shown in figure
The function name can be customized, for example, StringCat;
You can also add instructions for using this function, as shown in
Use custom functions
After registering a custom function, you can directly use it when creating a report. The usage is the same as that of the built-in function.
Create a report and define two report parameters: para1 and para2. The types are string and integer. The default values are null and 0.
Write the formula = StringCat ($ para1, $ para2) to any cell in the blank report (note: add $ before the parameter name when writing the formula, indicating that this is a used parameter)
Click Preview by page. In the parameter control, write the parameter value, for example, para1 is FineReport and para2 is 123.
Click query to view the results.
It indicates that the StringCat formula can be used normally.