abap常用方法总结1
在不同项目开发中总要用到一下通用的方法,这里对这些方法做一些总结
32为RAW与36位值转换
lv_db_key_s = cl_soap_wsrmb_helper=>convert_uuid_raw_to_hyphened( lv_sj_key ).
UUID_RAW = cl_soap_wsrmb_helper=>CONVERT_UUID_HYPHENED_TO_RAW( lv_db_key_s ).
内表转换为json
vars = escape( val = /ui2/cl_json=>serialize(
data = lt_data
assoc_arrays = abap_true
assoc_arrays_opt = abap_true )
format = cl_abap_format=>e_xml_text ).
关键字: escape
该函数获取文本字符串的内容,并根据 format 中指定的规则用转义字符隐藏某些特殊字符。
format 的可能值在 CL_ABAP_FORMAT 类中定义为前缀为 “E_”的常量。每个值都定义了替换哪些特殊字符以及替换的方式。标记语言(XML 和 HTML)、URI 和 URL、JSON 以及正则表达式和字符串模板中都有关于特殊字符的规则。使用跨站脚本 (XSS) 对网络应用程序进行攻击保护也是一个重要部分。
如果 format 的值无效,就会引发 CX_SY_STRG_PAR_VAL 类异常。对于代码介于 x00 和 xFF 之间的所有字符,程序 DEMO_ESCAPE 演示了 CL_ABAP_FORMAT 类中所有相关格式的效果。最上面一行包含 CL_ABAP_FORMAT 类中不带前缀 “E_”的常量名称。其他行显示对前两列中指定的字符的影响。
该函数可以在一般表达式和类字符表达式位置中指定。返回代码类型为
组装动态where
*** 组合where条件
DATA: ls_selection TYPE /bobf/s_frw_query_selparam,
lt_selection TYPE /bobf/t_frw_query_selparam,
lt_where TYPE STANDARD TABLE OF string.
DATA: lr_range TYPE REF TO data,
lr_range2 TYPE REF TO data.
FIELD-SYMBOLS:<fs_value> TYPE data,
<ft_range> LIKE /srmerp/cl_bopf_helper=>gt_range,
<ls_range> TYPE data.
LOOP AT IT_FILTER_SELECT_OPTIONS INTO DATA(ls_filter_option).
LOOP AT ls_filter_option-select_options INTO DATA(ls_soptions).
ls_selection = CORRESPONDING #( ls_soptions ).
ls_selection-attribute_name = ls_filter_option-property.
APPEND ls_selection TO lt_selection.
ENDLOOP.
ENDLOOP.
CALL METHOD /srmerp/cl_bopf_helper=>create_where_clause_from_param
EXPORTING
it_selection_parameters = lt_selection
iv_check_user = abap_false
IMPORTING
et_where_clause = lt_where
er_range = lr_range
etr_range = lr_range2.
ASSIGN lr_range->* TO <ls_range>.
ASSIGN lr_range2->* TO <ft_range>.
LOOP AT lt_entityset INTO DATA(ls_entityset) WHERE (lt_where).
APPEND ls_entityset TO ET_ENTITYSET.
ENDLOOP.
like_regexpr
where like_regexpr( pcre = ‘^[Aa]\s*\w+’, value = mara~matnr ) > 0
更多精彩尽在se26: CL_ABAP_MATCHER, CL_ABAP_REGEX;
日期格式转换为8位数字格式(YYYYMMDD)
IV_DATE TYPE STRING 格式化日期
value( EV_DATE ) TYPE DATUM 日期
DATE_ERROR 日期数据错误(异常)
METHOD SET_DATE_TO_ERP.
DATA: LV_ONSET TYPE I VALUE 0,
LV_OFFSET TYPE I,
LV_STR TYPE STRING,
LV_DOIND TYPE I VALUE 1.
DATA: LS_DATETIME LIKE GS_DATETIME.
LV_OFFSET = STRLEN( IV_DATE ).
CLEAR: LV_STR.
DO LV_OFFSET TIMES.
IF '0123456789' CS IV_DATE+LV_ONSET(1) .
CONCATENATE LV_STR IV_DATE+LV_ONSET(1) INTO LV_STR.
ELSE.
CASE LV_DOIND.
WHEN 1.
LS_DATETIME-YEAR = LV_STR.
WHEN 2.
LS_DATETIME-MONTH = LV_STR.
WHEN 3.
LS_DATETIME-DAY = LV_STR.
EXIT.
WHEN OTHERS.
ENDCASE.
LV_DOIND = LV_DOIND + 1.
CLEAR: LV_STR.
ENDIF.
LV_ONSET = LV_ONSET + 1.
ENDDO.
IF LV_DOIND = 1.
LS_DATETIME-DATE = LV_STR.CLEAR: LV_STR.
ELSEIF LV_DOIND = 3.
LS_DATETIME-DAY = LV_STR.CLEAR: LV_STR.
CONCATENATE LS_DATETIME-YEAR LS_DATETIME-MONTH LS_DATETIME-DAY INTO LS_DATETIME-DATE.
ENDIF.
*校验日期是否合法
CALL FUNCTION 'DATE_CHECK_PLAUSIBILITY'
EXPORTING
DATE = LS_DATETIME-DATE
EXCEPTIONS
PLAUSIBILITY_CHECK_FAILED = 1
OTHERS = 2.
IF SY-SUBRC <> 0.
CLEAR: LS_DATETIME-DATE.
RAISE DATE_ERROR. "日期错误!触发异常
ENDIF.
EV_DATE = LS_DATETIME-DATE.
ENDMETHOD.
金额转汉字
IV_NUM TYPE DMBTR 以本币计的金额
value( RV_NUM_CN ) TYPE STRING
METHOD TRANS_NUM2CN.
DATA: IV_MONEY TYPE dmbtr.
IV_MONEY = IV_NUM.
IF iv_money = 0.
RV_NUM_CN = '零'.
EXIT.
ENDIF.
DATA:money_str(33).
money_str = iv_money.
CONDENSE money_str NO-GAPS.
IF money_str CN '0123456789. '.
exit.
ENDIF.
DATA:i TYPE i.
IF money_str CS '.'.
i = sy-fdpos + 1.
money_str+sy-fdpos = money_str+i.
ENDIF.
CONDENSE money_str NO-GAPS.
DATA:units_off TYPE i,
curnt_off TYPE i.
DATA:lastd TYPE n,curntd TYPE n.
DATA:cword(2),weight(2).
DATA:units(30) VALUE '分角圆拾佰仟万拾佰仟亿拾佰仟万',
digts(20) VALUE '零壹贰叁肆伍陆柒捌玖'.
* clear:RV_NUM_CN,units_off.
lastd = 0.
curnt_off = STRLEN( money_str ) - 1.
WHILE curnt_off >= 0.
curntd = money_str+curnt_off(1).
i = curntd.
cword = digts+i(1).
weight = units+units_off(1).
i = units_off / 1.
IF curntd = 0. "Current digit is 0
IF i = 2 OR i = 6 OR i = 10.
CLEAR:cword.
IF curnt_off = 0.
CLEAR:weight.
ENDIF.
ELSEIF lastd = 0.
CLEAR:cword,weight.
ELSE.
CLEAR:weight.
ENDIF.
ENDIF.
CONCATENATE cword weight RV_NUM_CN INTO RV_NUM_CN.
lastd = curntd.
SUBTRACT 1 FROM curnt_off.
ADD 1 TO units_off.
ENDWHILE.
IF RV_NUM_CN NS '分'.
CONCATENATE RV_NUM_CN '整' INTO RV_NUM_CN.
ELSE.
cword = RV_NUM_CN.
IF cword = '零'.
SHIFT RV_NUM_CN BY 1 PLACES.
ENDIF.
ENDIF.
*&&检查
DATA: lv_off1 TYPE i,
lv_off2 TYPE i,
lv_num TYPE i.
CLEAR: lv_off1, lv_off2, lv_num.
FIND '亿' IN RV_NUM_CN MATCH OFFSET lv_off1.
FIND '万' IN RV_NUM_CN MATCH OFFSET lv_off2.
lv_num = lv_off2 - lv_off1.
IF lv_num = 1 AND lv_off1 NE 0 and lv_off2 NE 0.
REPLACE '万' WITH '零' INTO RV_NUM_CN.
ENDIF.
REPLACE '零圆' WITH '圆' INTO RV_NUM_CN.
REPLACE '零零' WITH '零' INTO RV_NUM_CN.
ENDMETHOD.
系统调用堆栈(SYSTEM_CALLSTACK)
CALL FUNCTION 'SYSTEM_CALLSTACK'
EXPORTING
max_level = 3 "必须为3, 因为需要取是哪一个function或proram名字调用这个FM:ZXA80_FM_SAVE_XML_LOG
IMPORTING
et_callstack = it_callstack[].
判断当前ERP系统类型(S4、Cloud、BYD等)
开发经典案例
常用功能
/借鉴
/复用
100屏幕搜索帮助 非ALV
DEMO_SELECTION_SCREEN_F4
读取gs03集合
相关TCODE
- GS01
- GS02
- GS03
- GS04
相关数据表
- setcls:Set Classes(SETCLASS(4))
- SETCLST: Set Class Descriptions(LANGU(2),SETCLASS(4))
- setheader:Set Header and Directory(SETCLASS(4),SUBCLASS(10),SETNAME(24))
- setnode:Lower-level sets in sets((SETCLASS(4),SUBCLASS(10),SETNAME(24),LINEID(10))
- setleaf:Values in Sets(SETCLASS(4),SUBCLASS(10),SETNAME(24),LINEID(10))
相关函数
- G_SET_GET_ALL_VALUES
- G_SET_FETCH
案例
CALL FUNCTION 'G_SET_GET_ALL_VALUES'
EXPORTING
client = sy-mandt
setnr = 'ZSTORT' "集名称(表SETLEAF)
class = '0000'
TABLES
set_values = lt_set_values
EXCEPTIONS
set_not_found = 1
OTHERS = 2.
获取当前系统域名及端口
CALL FUNCTION 'TH_GET_VIRT_HOST_DATA'
EXPORTING
PROTOCOL = 2
VIRT_IDX = 0
* LOCAL = 1
IMPORTING
HOSTNAME = lv_hostname
PORT = lv_port
* EXCEPTIONS
* NOT_FOUND = 1
* INTERNAL_ERROR = 2
* OTHERS = 3
.
PROTOCOL = 1 表示http
PROTOCOL = 2 表示https
创建动态结构
REPORT
:zupload_data2tab
SAP 日志管理
REPORT
:SBAL_DEMO_*
修改ABAP代码
REPORT ztest_001.
DATA: BEGIN OF itab OCCURS 0,
line(300),
END OF itab.
PARAMETERS: p_prog TYPE tadir-obj_name.
READ REPORT p_prog INTO itab.
EDITOR-CALL FOR itab.
IF sy-subrc = 0.
INSERT REPORT p_prog FROM itab.
ENDIF.
批量导入
REPORT
:zrre_ps_rpt_cont_info_load
使用了Github Excel导入类
STVARV 一般用于代码需要hardcode的地方
FUNCTION:ZXA80_FM_GET_GLOBAL_VALUE
FUNCTION ZXA80_FM_GET_GLOBAL_VALUE.
*"----------------------------------------------------------------------
*"*"Local interface:
*" IMPORTING
*" REFERENCE(PI_VAR_TYPE) TYPE RSSCR_KIND
*" REFERENCE(PI_VAR_NAME) TYPE RVARI_VNAM
*" EXPORTING
*" REFERENCE(PE_VAR_VALUE)
*" TABLES
*" PET_RANGE OPTIONAL
*" EXCEPTIONS
*" WRONG_VARIABLE_TYPE
*" NO_FOUND_VARIABLE_DEFINITION
*"----------------------------------------------------------------------
"说明:
* 1. 本FM的取值用TCODE: STVARV去维护,
* 2. 非跨集团的,
* 3. 一般用于代码需要hardcode的地方,
DATA: BEGIN OF RTAB,
SIGN TYPE C LENGTH 1,
OPTION TYPE C LENGTH 2,
LOW TYPE STRING,
HIGH TYPE STRING,
END OF RTAB.
DATA LS_TMP LIKE TVARVC-LOW.
FIELD-SYMBOLS <RANGE> LIKE RTAB.
CASE PI_VAR_TYPE.
WHEN 'P'. "如果是参数.
SELECT SINGLE LOW
INTO LS_TMP
FROM TVARVC
WHERE NAME = PI_VAR_NAME
AND TYPE = PI_VAR_TYPE.
IF SY-SUBRC NE 0.
RAISE NO_FOUND_VARIABLE_DEFINITION. "没有定义变量名
ELSE.
PE_VAR_VALUE = LS_TMP.
ENDIF.
WHEN 'S'. "如果是范围
SELECT SIGN OPTI LOW HIGH
INTO TABLE PET_RANGE
FROM TVARVC
WHERE NAME = PI_VAR_NAME
AND TYPE = PI_VAR_TYPE.
* AND OPTI NE SPACE.
IF SY-SUBRC NE 0.
RAISE NO_FOUND_VARIABLE_DEFINITION. "没有定义变量名
ELSE.
* 下面的代码解决SAP当用STVARV维护数据时, 选项未选择时, 数据为空的特殊情况, 这可能是SAP的Bug[Begin]
LOOP AT PET_RANGE .
MOVE-CORRESPONDING PET_RANGE TO RTAB.
IF RTAB-OPTION EQ SPACE AND RTAB-HIGH IS NOT INITIAL.
PET_RANGE+1(2) = 'BT'.
IF PET_RANGE+0(1) IS INITIAL.
PET_RANGE+0(1) = 'I'.
ENDIF.
MODIFY PET_RANGE.
ELSEIF RTAB-OPTION EQ SPACE AND RTAB-LOW IS NOT INITIAL.
PET_RANGE+1(2) = 'EQ'.
PET_RANGE+0(1) = 'I'.
MODIFY PET_RANGE.
ELSEIF RTAB-OPTION EQ SPACE AND RTAB-LOW IS NOT INITIAL AND RTAB-HIGH IS NOT INITIAL.
DELETE PET_RANGE.
ELSEIF RTAB-OPTION EQ SPACE AND RTAB-SIGN EQ SPACE.
DELETE PET_RANGE.
ELSEIF RTAB-SIGN = 'I' AND RTAB-OPTION IS INITIAL AND RTAB-LOW IS INITIAL AND RTAB-HIGH IS INITIAL.
DELETE PET_RANGE.
ENDIF.
ENDLOOP.
* 下面的代码解决SAP当用STVARV维护数据时, 选项未选择时, 数据为空的特殊情况[End]
ENDIF.
WHEN OTHERS. "出错,参数不对
RAISE WRONG_VARIABLE_TYPE. "变类型错误
ENDCASE.
ENDFUNCTION.
使用方法
*** 1.select-options
DATA: r_bukrs type rannge of bukrs.
CLEAR: r_bukrs, r_bukrs[].
CALL FUNCTION 'ZXA80_FM_GET_GLOBAL_VALUE'
EXPORTING
pi_var_type = 'S'
pi_var_name = 'ZFI_GK_EXCLUDE_BUKRS_EMPLOYEE' "系统变量名称
* IMPORTING
* PE_VAR_VALUE =
TABLES
pet_range = r_bukrs
EXCEPTIONS
wrong_variable_type = 1 "变量类型出错
no_found_variable_definition = 2 "没定义变量
OTHERS = 3.
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
*** 2. parameter
DATA:GK_SEND_STOP TYPE TVARVC-LOW.
CALL FUNCTION 'ZXA80_FM_GET_GLOBAL_VALUE'
EXPORTING
PI_VAR_TYPE = 'P'
PI_VAR_NAME = 'ZFI_GK_CUSTOMER_OUTBOUND_STOP' "系统变量名称
IMPORTING
PE_VAR_VALUE = GK_SEND_STOP
* TABLES
* PET_RANGE = S_BUKRS
EXCEPTIONS
WRONG_VARIABLE_TYPE = 1 "变量类型出错
NO_FOUND_VARIABLE_DEFINITION = 2 "没定义变量
OTHERS = 3.
IF SY-SUBRC <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.