Custom functions for report development

Source: Internet
Author: User

For report development, in many cases, the function can meet the needs of most users of the report production, Finereport is no exception. However, in some special areas, special functions may be required, in which case the Finereport provides a custom function mechanism that allows the user to define functions according to the business needs themselves, but these functions must satisfy the function definition rules.

Let's take a look at Finereport's function definition rules: functionname (Para,para,...), where functionname is the function name and Para is the parameter.

Each function is defined as a class, and the class must implement the function interface, which first takes the class by reflection of the functions name, and then calls its run (object[] agrs) method. The following is an example of the SUM function.

Principle of Sum function

As can be seen by the program, the sum class is used to operate the SUM function, he inherits the Abstractfunction class, and Abstractfunction implements the function interface.

When the function is operation, the class of the function is obtained according to the function name, such as SUM (2,4,true), the function first obtains the sum class according to the function name, and then calls the Sum class's run (object[] args) method, in which args holds the parameters of the SUM function. Operations can be obtained from the args in the parameters. If the execution result is sum (2,4,true) =2+4+1=7.

The code used by the SUM function:

Packagecom.fr.report.script;importjava.lang.reflect.array;importcom.fr.report.script.core.farray; importcom.fr.report.script.core.functionhelper;public class sum extendsabstractfunction {          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]);                    }                    returnfunctionhelper.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 {                                        Returndouble.parsedouble (Obj.tostring ());                             }  catch  (NUMBERFORMATEXCEPTION&NBSP;EXP)  {                                       return 0;                             }                    }                     return 0;         }}

Implementation steps

Writing custom Functions

The following is a simple example of a custom function that illustrates the use of a custom function. We define a function Stringcat, whose function is to concatenate all the parameters in the form of strings.

The STRINGCAT function uses rules for STRINGCAT (Para,para,para ...);

Where para is the parameter of the function, the number is not limited.

Abstractfunction realizes the function of this interface by the overview, so stringcat can inherit the Abstractfunction class directly, the complete code is as follows:

package com.fr.function; importcom.fr.script.abstractfunction; public class  Stringcatextends 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 here that the function Stringcat (Para,para,para ...) is used. , the class stringcat of the function is obtained according to the function name, and the parameter is passed to the args object array in the class, and the run function of the class is executed.

In the run function, it is implemented to concatenate the incoming arguments as strings. and returns the resulting string.

Compiling custom functions

Put the compiled stringcat.class into the classes directory below the Finereport installation directory Web-inf, because Stringcat.java belongs to package com.fr.function, so Stringcat.class needs to be put into classes The \com\fr\function directory.

Registering a custom function

You can use this function after the class that generated the function needs to be registered in the designer. Open Server | Function Manager, select the Stringcat class that you just defined, such as

650) this.width=650; "src=" http://img.blog.csdn.net/20160721110639418 "style=" border:none; "/>

Function names can be customized, as defined here as Stringcat;

You can also add a description of the function, as shown in

Using a custom function

Once you have registered your custom function, you can use it directly when you make the report, using the same method as the built-in function.

Create a new report, define two report parameters Para1, Para2, types are string and shape, the default values are empty strings and 0

650) this.width=650; "src=" http://img.blog.csdn.net/20160721110704544 "style=" border:none; "/>

Write the formula in any cell in the blank report: =stringcat ($para 1, $para 2) (note: When writing a formula, add $ to the parameter name to indicate that this is the argument used)

Click Page Break preview in the parameter control, write the parameter value as PARA1: Finereport,para2 is: 123.

Click the query to see the results

650) this.width=650; "src=" http://img.blog.csdn.net/20160721110719967 "style=" border:none; "/>

Description Stringcat formula can be used normally.


Custom functions for report development

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.