2019-10-18利用正则表达式解决问题excel上传等数据验
当我们启用excel上传时,我们以前采用了字段内容校验,这种方法特别大量的耗损了abap的时间,做这种重复的数据检查工作,在碧桂园项目,领导提出了只检查数据类型,于是做了一个版本,我在泰康做了第二个版本升级,今天又做第三个版本,进行升级。最近面试老是被人问有没有作过什么特别的东西,哎,说实话写代码太多了,真心都觉得一般般,答不出来什么是特别的,很特别的东西,只是需要点时间而已,其它都是扯蛋。
FUNCTION ZTEST_XIAOY_CHECKTF.
*"----------------------------------------------------------------------
*"*"本地接口:
*" IMPORTING
*" REFERENCE(IV_TABNAME) TYPE TABNAME
*" TABLES
*" IT_DATA
*" ET_MESSAGE STRUCTURE ZSHR_MESSAGE
*"----------------------------------------------------------------------
DATA: dyn_table TYPE REF TO data,
”gs_display type ref to data,
lo_descr type ref to cl_abap_structdescr,
lt_fields type ddfields,
lv_zz type string,
ls_fields like line of lt_fields,
ls_message type zshr_message,
lv_text type string,
lv_line type char20,
dyn_wa TYPE REF TO data.
FIELD-SYMBOLS: <dyn_table> TYPE table,
<dyn_wa> TYPE ANY.
*IV_TABNAME 是我们excel se11定义的结构或者表
CREATE DATA dyn_table TYPE TABLE OF (IV_TABNAME).
ASSIGN dyn_table->* TO <dyn_table>.
CREATE DATA dyn_wa LIKE LINE OF <dyn_table>.
ASSIGN dyn_wa->* TO <dyn_wa>.
*<dyn_table> = IT_DATA[].
ASSIGN IT_DATA[] to <dyn_table>.
*下面是获取结构字段属性 当然有个简单的办法,直接从dd03l读取就行
lo_descr ?= cl_abap_typedescr=>describe_by_data( <dyn_wa> ).
lt_fields = lo_descr->get_ddic_field_list( p_langu = 'E'
p_including_substructres = abap_true ).
*sort lt_fields by tabname fieldname.
loop at <dyn_table> ASSIGNING <dyn_wa>.
lv_line = sy-tabix.
CONDENSE lv_line.
loop at lt_fields into ls_fields.
ASSIGN COMPONENT ls_fields-fieldname of STRUCTURE <dyn_wa> to FIELD-SYMBOL(<field>).
if <field> is ASSIGNED.
case ls_fields-inttype.
when 'D'.
if <field> is not initial.
replace all occurrences of '\' in lv_text with space.
replace all occurrences of '-' in lv_text with space.
replace all occurrences of '/' in lv_text with space.
condense: lv_text no-gaps.
lv_text = <field> .
lv_zz = '^(?:(?:(?:(?:(?:1[6-9]|[2-9]\d)(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00)))(?:0229))|' &&
'(?:(?:(?:1[6-9]|[2-9]\d)\d{2})(?:(?:(?:0[13578]|1[02])31)|(?:(?:0[13-9]|1[0-2])(?:29|30))|(?:(?:0[1-9])|(?:1[0-2]))(' &&
'?:0[1-9]|1\d|2[0-8]))))$'.
if cl_abap_matcher=>matches(
pattern = lv_zz
text = lv_text ) ne abap_true.
ls_message-msgty = 'E'.
concatenate 'Excel第' lv_line '字段' ls_fields-fieldname '数据不是日期格式YYYYMMDD' into ls_message-msgtx SEPARATED BY space.
append ls_message to ET_MESSAGE.
“exit .
clear:ls_message.
endif.
<field> = lv_text.
endif.
when 'N' or 'I' .
if <field> is not initial.
lv_text = <field> .
replace all occurrences of ',' in lv_text with space.
condense: lv_text no-gaps.
if cl_abap_matcher=>matches(
pattern = '-?[0-9]\d*'
text = lv_text ) ne abap_true.
ls_MESSAGE-msgty = 'E'.
* concatenate ls_fields-fieldname '数据不是整数类型的' into ls_MESSAGE-msgtx.
concatenate 'Excel第' lv_line '字段' ls_fields-fieldname '数据不是整数类型的' into ls_message-msgtx SEPARATED BY space.
append ls_MESSAGE to ET_MESSAGE.
”exit .
clear:ls_MESSAGE.
endif.
<field> = lv_text.
endif.
when 'P'.
if <field> is not initial.
lv_text = <field> .
replace all occurrences of ',' in lv_text with space.
condense: lv_text no-gaps.
if cl_abap_matcher=>matches(
* pattern = '^(([1-9]\d*)(\.\d{1,2})?)$|^(0\.0?([1-9]\d?))$'
pattern = '^[0-9]+.?[0-9]*$'"肖勇更改正则表达式 20190422
text = lv_text ) ne abap_true.
ls_message-msgty = 'E'.
* concatenate ls_ztehrdt02-fielc '数据不是数字类型的' into ls_message-msgtx.
concatenate 'Excel第' lv_line '字段' ls_fields-fieldname '数据不是数字类型的' into ls_message-msgtx SEPARATED BY space.
append ls_message to et_message.
“ exit .
clear:ls_message.
endif.
<field> = lv_text.
endif.
when 'C'.
* clear:len.
data(len) = strlen( <field> )."先判断长度
if len > ls_fields-leng.
ls_message-msgty = 'E'.
* concatenate ls_ztehrdt02-fielc '数据长度过长' into ls_message-msgtx.
concatenate 'Excel第' lv_line '字段' ls_fields-fieldname '数据长度过长' into ls_message-msgtx SEPARATED BY space.
append ls_message to ET_MESSAGE.clear:ls_message.
endif.
clear:len.
endcase.
endif.
endloop.
ENDLOOP.
ENDFUNCTION.
测试用的小段代码
select * into CORRESPONDING FIELDS OF TABLE lt_mara from mara UP TO 10 ROWS.
CALL FUNCTION 'ZTEST_XIAOY_CHECKTF'
EXPORTING
IV_TABNAME = 'MARA'
TABLES
IT_DATA = lt_mara[]
ET_MESSAGE = ET_MESSAGE.
我们在debug的时候可以改变 lt_mara里面的值来验证代码。