SAP ABAP开发常用FORM集
*&---------------------------------------------------------------------*
*& 包括 YZ_ZLL_FORMS
*&---------------------------------------------------------------------*
TYPES: abap_bool.
CONSTANTS:
abap_true TYPE abap_bool VALUE 'X',
abap_false TYPE abap_bool VALUE ''.
CONSTANTS:gc_green TYPE icon VALUE '@5B@',
gc_red TYPE icon VALUE '@5C@',
gc_yellow TYPE icon VALUE '@5D@'.
************************************************************************
* 宏定义:增加Range表行
* &1:Range对象,&2:sign,&3:option,&4:low,&5:high
************************************************************************
DEFINE zmac_range_add_item.
&1-sign = &2.
&1-option = &3.
&1-low = &4.
if &4 is not initial.
&1-high = &5.
endif.
append &1.
clear &1.
END-OF-DEFINITION.
************************************************************************
* 系统消息相关
************************************************************************
************************************************************************
* 宏定义:显示系统消息
************************************************************************
DEFINE zmac_show_sys_msg.
message id sy-msgid type sy-msgty number sy-msgno
with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
END-OF-DEFINITION.
* 过程:获取系统消息
FORM zfrm_get_sys_msg CHANGING fc_msg.
CALL FUNCTION 'MESSAGE_TEXT_BUILD'
EXPORTING
msgid = sy-msgid
msgnr = sy-msgno
msgv1 = sy-msgv1
msgv2 = sy-msgv2
msgv3 = sy-msgv3
msgv4 = sy-msgv4
IMPORTING
message_text_output = fc_msg.
ENDFORM. "frm_get_system_message
* 过程:获取BAPI返回消息
FORM zfrm_get_bapi_ret_msg USING fs_return TYPE bapiret2 CHANGING fc_msg.
CALL FUNCTION 'MESSAGE_TEXT_BUILD'
EXPORTING
msgid = fs_return-id
msgnr = fs_return-number
msgv1 = fs_return-message_v1
msgv2 = fs_return-message_v2
msgv3 = fs_return-message_v3
msgv4 = fs_return-message_v4
IMPORTING
message_text_output = fc_msg.
ENDFORM. "frm_get_return_message
* 状态栏,显示进度信息
FORM zfrm_gui_progress USING value(fv_text) TYPE string.
CALL FUNCTION 'SAPGUI_PROGRESS_INDICATOR'
EXPORTING
text = fv_text.
ENDFORM. "FRM_GUI_PROGRESS
* 状态栏,显示进度信息
FORM zfrm_gui_progress_percent USING value(fv_text) value(fv_percent).
"p_text = '数据处理中,请稍后......'.
CALL FUNCTION 'SAPGUI_PROGRESS_INDICATOR'
EXPORTING
percentage = fv_percent " 0-100之间
text = fv_text.
ENDFORM. "FM_GUI_PROGRESS_IND
************************************************************************
* 数据转换相关
************************************************************************
* 通用转换
FORM zfrm_convert USING p_type p_put input CHANGING output.
DATA: lv_function_name TYPE rs38l_fnam.
* FUNCTIONAME = |CONVERSION_EXIT_{ P_TYPE }_{ P_PUT }|.
CONCATENATE 'CONVERSION_EXIT_' p_type p_put INTO lv_function_name.
CONDENSE lv_function_name NO-GAPS.
CALL FUNCTION lv_function_name
EXPORTING
input = input
IMPORTING
output = output.
ENDFORM. "zfrm_convert
* 编号去前置零
FORM zfrm_code_no_lzero CHANGING fc_code.
* 方式一:函数转换
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT'
EXPORTING
input = fc_code
IMPORTING
output = fc_code.
** 方式二:新语法,显示转换(不改变值内容)
* FC_CODE = |{ FC_CODE ALPHA = OUT }|.
ENDFORM. " CONVERSION_EXIT_ALPHA_OUTPut
* 编号加前置零
FORM zfrm_code_with_lzero CHANGING fc_code.
* 方式一:函数转换
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
input = fc_code
IMPORTING
output = fc_code.
** 方式二:新语法,显示转换(不改变值内容)
* FC_CODE = |{ FC_CODE ALPHA = IN }|.
ENDFORM. "ZFRM_CODE_WITH_LZERO
* 编号前置零处理:+前置0,-前置0
FORM zfrm_code_deal_lzero USING value(fu_flag) CHANGING fc_code .
CASE fu_flag .
WHEN '+'.
PERFORM zfrm_code_with_lzero CHANGING fc_code.
WHEN '-'.
PERFORM zfrm_code_no_lzero CHANGING fc_code.
ENDCASE.
ENDFORM. "ZFRM_CODE_DEAL_LZERO
* 物料号去前置零
FORM zfrm_matnr_no_lzero CHANGING fc_matnr.
CALL FUNCTION 'CONVERSION_EXIT_MATN1_OUTPUT'
EXPORTING
input = fc_matnr
IMPORTING
output = fc_matnr
EXCEPTIONS
length_error = 1
OTHERS = 2.
IF sy-subrc <> 0.
ENDIF.
ENDFORM. " CONVERSION_EXIT_ALPHA_OUTPut
* 物料号加前置零
FORM zfrm_matnr_with_lzero CHANGING fc_matnr.
CALL FUNCTION 'CONVERSION_EXIT_MATN1_INPUT'
EXPORTING
input = fc_matnr
IMPORTING
output = fc_matnr
EXCEPTIONS
length_error = 1
OTHERS = 2.
IF sy-subrc <> 0.
ENDIF.
ENDFORM. "ZFRM_MATNR_WITH_LZERO
* 物料号前置零处理:+前置0,-前置0
FORM zfrm_matnr_deal_lzero USING value(fu_flag) CHANGING fc_matnr .
CASE fu_flag .
WHEN '+'.
PERFORM zfrm_matnr_with_lzero CHANGING fc_matnr.
WHEN '-'.
PERFORM zfrm_matnr_no_lzero CHANGING fc_matnr.
ENDCASE.
ENDFORM. "ZFRM_MATNR_DEAL_LZERO
* 内外部单位转换(转显示值)
FORM zfrm_unit_output USING value(fu_unit) CHANGING fc_unit.
CALL FUNCTION 'CONVERSION_EXIT_CUNIT_OUTPUT'
EXPORTING
input = fu_unit
language = sy-langu
IMPORTING
output = fc_unit.
ENDFORM. " CONVERSION_EXIT_CUINT_OUTPut
* 内外部单位转换(转内部值)
FORM zfrm_unit_input USING fu_unit CHANGING fc_unit.
CALL FUNCTION 'CONVERSION_EXIT_CUNIT_INPUT'
EXPORTING
input = fu_unit
language = sy-langu
IMPORTING
output = fc_unit.
ENDFORM. " CONVERSION_EXIT_CUINT_OUTPut
***************************字符串处理******************************************
* 拼接字符串,对 fc_msg_con 追加 fv_symbol fv_message
FORM zfrm_concatenate_with_symbol USING value(fv_message)
value(fv_symbol)
CHANGING value(fc_msg_con).
IF fc_msg_con IS INITIAL.
fc_msg_con = fv_message.
ELSE.
CONCATENATE fc_msg_con fv_message INTO fc_msg_con SEPARATED BY fv_symbol.
ENDIF.
ENDFORM. "FM_CONCARENATE_MESSAGE
************************************************************************
* 数值相关
************************************************************************
* 数字去尾0,负号前置
FORM zfrm_num_nozero USING value(fv_input) CHANGING fc_output.
DATA: l_inp TYPE string,
l_len TYPE i.
MOVE fv_input TO l_inp.
CONDENSE l_inp.
l_len = STRLEN( l_inp ).
l_len = l_len - 1.
IF l_inp+l_len = '-'. "最后一位
l_inp = l_inp(l_len). "取最后一位之前的
CONCATENATE '-' l_inp INTO l_inp. "负号前置
ENDIF.
SHIFT l_inp RIGHT DELETING TRAILING '0'.
SHIFT l_inp RIGHT DELETING TRAILING '.'.
CONDENSE l_inp.
fc_output = l_inp.
ENDFORM. "zfrm_num_nozero
* 数字去尾0,去+号,并指定负号位置
* 入参:fv_input,输入的数字;fv_loc,负号的未知,L-左置,其他-右置
FORM zfrm_num_nozero_ex USING value(fv_input) value(fv_loc)
CHANGING fc_output.
DATA: lv_input TYPE string,
lv_num TYPE string,
lv_num1 TYPE string,
lv_num2 TYPE string,
lv_minus TYPE c.
lv_input = fv_input.
CONDENSE lv_input NO-GAPS.
SPLIT lv_input AT '.' INTO lv_num1 lv_num2.
REPLACE ALL OCCURRENCES OF '+' IN lv_num2 WITH ''. " 去正号
REPLACE ALL OCCURRENCES OF '-' IN lv_num2 WITH ''.
IF sy-subrc = 0.
lv_minus = '-'.
ENDIF.
SHIFT lv_num2 RIGHT DELETING TRAILING '0'. " 小数部分去尾0
CONDENSE lv_num2.
CONCATENATE lv_num1 '.' lv_num2 INTO lv_num.
SHIFT lv_num RIGHT DELETING TRAILING '.'. " 整数去小数点
IF lv_num = '0'.
CLEAR: lv_num.
ELSE.
IF lv_minus IS NOT INITIAL.
TRANSLATE fv_loc TO LOWER CASE.
IF fv_loc = 'l'.
CONCATENATE lv_minus lv_num INTO lv_num.
ELSE.
CONCATENATE lv_num lv_minus INTO lv_num.
ENDIF.
ENDIF.
CONDENSE lv_num NO-GAPS.
ENDIF.
fc_output = lv_num.
ENDFORM. "zfrm_num_nozero_ex
*& 四舍五入(不准确)
FORM zfrm_rounding_decimals USING value(fv_data) " 传入小数
value(fv_dec) TYPE i " 保留小数位数
CHANGING fc_data . " 结果
DATA: lv_data TYPE p DECIMALS 10.
CALL FUNCTION 'HR_NZ_ROUNDING_DECIMALS'
EXPORTING
value_in = fv_data
conv_dec = fv_dec
IMPORTING
value_out = lv_data
EXCEPTIONS
no_rounding_required = 1
decimals_greater_than_10 = 2
rounding_error = 3
OTHERS = 4.
IF sy-subrc <> 0.
*MESSAGE ID SY-MSGID TYPE 'W' NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4 .
ENDIF
.
" 去尾0
PERFORM zfrm_num_nozero USING lv_data CHANGING fc_data.
ENDFORM. "FM_ROUNDING_DECIMALS
*&---------------------------------------------------------------------*
*& Form FM_FLTP_TO_CHAR
*& 科学计数法FLTP 转为 字符串CHAR
*&---------------------------------------------------------------------*
FORM zfrm_fltp_to_char USING value(fv_input)