自定义报表函数 ------------------------- `@date20211212` `@version1.4.3+` ### 示例:新增函数->将字母转成大写 #### 1.定义函数 ``` import com.googlecode.aviator.runtime.function.AbstractFunction; import com.googlecode.aviator.runtime.type.AviatorObject; import com.googlecode.aviator.runtime.type.AviatorRuntimeJavaType; import org.jeecg.modules.jmreport.desreport.express.ExpressUtil; import java.util.Map; /** * 定义函数: * 1.定义getName方法,返回一个字符串 * 2.如果函数参数个数已知,继承AbstractFunction,重写call方法,call是一个多态方法,参数AviatorObject arg可以传多个 * 3.如果函数参数个数未知,继承AbstractVariadicFunction,重写variadicCall方法 **/ public class UpcaseFun extends AbstractFunction { @Override public String getName() { return "upCase"; } @Override public AviatorObject call(Map<String, Object> env, AviatorObject arg1) { // 参数用此方法获取 数字也这么获取 然后自己转 String str = ExpressUtil.getArgString(arg1, env); return AviatorRuntimeJavaType.valueOf(str.toUpperCase()); } } ``` #### 2.注册函数 ~~~ import com.googlecode.aviator.AviatorEvaluatorInstance; import org.jeecg.modules.jmreport.desreport.express.IJmExpressCustom; import org.springframework.stereotype.Component; /** * 注册函数: * 1.添加类注解@Component * 2.实现接口 IJmExpressCustom,重写方法 addFunction * 3.instance.addFunction(fun.getName(), fun); * 4.如果是运算函数(如:加减乘除),调用instance.addOpFunction */ @Component public class JmExpressCustomImpl implements IJmExpressCustom { @Override public void addFunction(AviatorEvaluatorInstance instance) { UpcaseFun fun = new UpcaseFun(); instance.addFunction(fun.getName(), fun); //OtherFun fun1 = new OtherFun (); //instance.addFunction(fun1.getName(), fun1); } } ~~~ #### 3.测试效果: - 设计页面: ![](https://img.kancloud.cn/5d/b2/5db2851c7af46771e9542c8b36b765cf_476x130.png) - 预览页面: ![](https://img.kancloud.cn/63/64/6364683b94e6070b4550fcafd5b8f801_488x150.png)