1. Overview
FineReport has provided a large number of built-in functions, which are sufficient to meet the user's reporting requirements under normal circumstances. However, in some special fields, some special functions may be required. In this case, FineReport provides a custom function mechanism. Users can define some functions according to their business needs, as long as these functions meet the FineReport function definition rules. FineReport function definition rules: functionName(Para, Para, ...).
In FineReport, each function is a class that implements the Function interface. While performing operations, first obtain this class through function name reflection, and then call the run(Object[] agrs) method. Let's take the built-in SUM function as an example.
2. SUM Function Principle
As you can see from the program, the SUM class is used to calculate the SUM function. It inherits the AbstractFunction class, and AbstractFunction implements the Function interface.
When a function is calculated, first obtain the class of the function based on the function name, such as SUM(2,4,true), this function first obtains the SUM class based on the function name. Then call the run(Object[] args) method of the SUM class. The parameters of the SUM function are stored in args, and the parameters can be obtained from args for calculation. For example, execute SUM(2,4,true)=2+4+1=7.
The code used in the SUM function:
package com.fr.report.script;
import com.fr.general.FArray;
import com.fr.script.AbstractFunction;
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 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;
}
}
3. Example
Here is a simple example to illustrate how to use custom functions. First, our requirement is to define a string splicing function StringCat. The usage rule of StringCat function is StringCat(Para,Para,Para…….); Para is the parameter of the function, and the number of parameters is unlimited. Detailed code reference:
3.1 Compile Custom Functions
Put the compiled StringCat.class into the classes directory under the installation directory of FineReport WEB-INF, because StringCat.java belongs to the package com.fr.function, so StringCat.class needs to be placed under %FR_HOME%\webapps\webroot\WEB -INF\classes\com\fr\function directory.
3.2 Register Custom Functions
After the class of the function is generated, it needs to be registered in the designer before the function comes into use. Open Server>Function Manager, select the StringCat class just defined, as shown in the following figure:
The function name can be customized, such as StringCat defined here; at the same time, you can add instructions for the function.
3.3 Use Custom Functions
After registering the custom function, it can be used directly when making reports. The usage method is the same as the built-in function
1)Template Design
Create a new report and define two report parameters para1 and para2, the types are string and integer, and the default values are empty string and 0 respectively.
Add parameters to the parameter panel, and write the formula in any cell of the blank report: =StringCat($p1,$p2)
Add $ before the parameter name when writing the formula, indicating that this is the parameter used
2)Template Preview
Click to preview the page. In the parameter widget, write parameter values such as p1 as: FineReport and p2 as: 123. Click the query to see the result:
4.Use the Designer to Directly Define Custom Functions
This method requires that javac compile without error under local cmd.
Select Menu Server> Function Manager, add a custom function name CustomFunction, click Edit, and enter the code:
After confirming that it is correct, click Compile. If the compilation is successful, click Save, and CustomFun.class will be generated in the %FR_HOME%\webapps\webroot\WEB-INF\classes\com\fr\function directory.