1 接口定义
public abstract class CommonFunction extends TolerantFunction {
/**
* 被调用函数方法
* @param arguments
* @return
*/
abstract public Object call(Object[] arguments);
}
说明
- CommonFunction用于实现通用函数功能的抽象类,已实现将函数调用时,包含的所有参数表达式,通过引擎执行转换成最终的参数值。
- 通过实现抽象方法call,以实现函数的主体功能。函数对应的参数值,可直接从arguments中按对应位置获取。
2 开发步骤
1) 新建JAVA工程或使用现有项目的工程。
2) 引用开发包到工程中。(注:v6.8.0以下版本开发包路径:安装目录\lczServer\WEB-INF\lib\hr-excel-xxx.jar;v6.8.0及以上版本开发包路径:安装目录\lczServer\WEB-INF\lib\hr-script-6.8.x.jar)
3) 创建自定义函数实现类,继承CommonFunction。
(建议:为了便于统一管理,建议将自定义函数包名定义为:com.datanew.excel.script.function.custom。)
4) 根据需求实现call方法逻辑。通过测试后,将编译的.class文件复制到:
安装目录\lczServer\WEB-INF\classes\com\datanew\excel\script\function\custom目录中。
3 函数使用
函数定义
通过设计器中的服务器资源->自定义函数,管理和维护自定义函数
注:函数类名,包括类的完整包路径和类名。
4 函数说明
与现有系统函数一样,可以应用在报表中任意支持表达式的地方。
5 实例说明
package com.datanew.excel.script.function.custom;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import com.datanew.excel.common.FunctionUtil;
import com.datanew.excel.script.function.CommonFunction;
/**
* 自定义函数实现示例
*
* 函数说明:
* fun1(nYear,nMonth,nWeek[,ntype])
* 返回nYear年nMonth月第nWeek周的第一天日期
* ------------------------------------------------------------------
* 参数说明:
* @param nYear:整形,支持表达式,所要计算的年
* @param nMonth:整形,支持表达式,所要计算的月,值不超过12
* @param nWeek : 整形,支持表达式,所要计算的第几周。如果超过该月的周数,则结果为计算最后一周。
* @param ntype :可选参数,值为0或1,默认为0 表示周一为周的第一天,1表示周日为周的第一天
* -----------------------------------------------------------------
* 返回值类型:日期类型
*
* @author DataNew
*
*/
public class Fun1 extends CommonFunction {
/**
* 函数调用实现
*/
public Object call(Object[] arg0) {
// 参数个数检查
if (arg0 == null || arg0.length < 3 ) {
throw new NullPointerException(ERROR_ARGS);
}
// 获取参数值
int year = FunctionUtil.toInteger(arg0[0]);
int month = FunctionUtil.toInteger(arg0[1]);
int week = FunctionUtil.toInteger(arg0[2]);
int type = 0;
if (arg0.length > 3){ // 获取可选参数值
type = FunctionUtil.toInteger(arg0[3]);
}
// 计算起始日期
Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, month-1);
c.set(Calendar.WEEK_OF_MONTH, week);
int weekDay = c.get(Calendar.DAY_OF_WEEK) == 1 ? 8 : c.get(Calendar.DAY_OF_WEEK);
c.add(Calendar.DATE, (type == 0 ? Calendar.MONDAY : Calendar.SUNDAY) - weekDay);
Date start = c.getTime();
return start;
}
/**
* main
* @param args
*/
public static void main(String[] args) {
Fun1 fun1 = new Fun1();
Date start = (Date)fun1.call(new Object[]{"2013","7","2"});
System.out.print(new SimpleDateFormat("yyyy-MM-dd").format(start));
}
}
作者:柳杨 创建时间:2023-06-07 18:21
最后编辑:柳杨 更新时间:2025-04-22 15:31
最后编辑:柳杨 更新时间:2025-04-22 15:31
