乐趣区

关于abap:abap-正则表达式验证EXCEL数据问题

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 外面的值来验证代码。
退出移动版