Loading

ABAP中常用的功能函数

1.负号前置:

DATA lv_data(6) TYPE c VALUE '12345-'.
CALL FUNCTION 'CLOI_PUT_SIGN_IN_FRONT'
  CHANGINGc
    value         = lv_data .  "-12345"

2.读取长文本:

# 方式一
"定义长文本参数"
DATA: LV_ID       TYPE  THEAD-TDID,          "id"
      LV_LANGUAGE TYPE  THEAD-TDSPRAS,       "language"
      LV_NAME     TYPE  THEAD-TDNAME,        "name"
      LV_OBJECT   TYPE  THEAD-TDOBJECT,      "object"
      LT_LINES    TYPE  TLINE TABLE OF WITH HEADER LINE,  "长文本内容返回结果"
      LV_MSG      TYPE  STRING.

"设置参数值"
LV_ID       = ''.
LV_LANGUAGE = SY-LANGU.
LV_NAME     = ''.
LV_OBJECT   = ''.

"调用读取长文本函数:READ_TEXT"
CALL FUNCTION 'READ_TEXT'
  EXPORTING
*   CLIENT                        = SY-MANDT
    id                            = LV_ID
    language                      = LV_LANGUAGE
    NAME                          = LV_NAME
    OBJECT                        = LV_OBJECT
*   ARCHIVE_HANDLE                = 0
*   LOCAL_CAT                     = ' '
* IMPORTING
*   HEADER                        =
*   OLD_LINE_COUNTER              =
  TABLES
    lines                         = LT_LINES
 EXCEPTIONS
   ID                            = 1
   LANGUAGE                      = 2
   NAME                          = 3
   NOT_FOUND                     = 4
   OBJECT                        = 5
   REFERENCE_CHECK               = 6
   WRONG_ACCESS_TO_ARCHIVE       = 7
   OTHERS                        = 8.

"调整长文本格式"
IF sy-subrc = 0.
  LOOP AT LT_LINES.
      CONCATENATE LV_MSG LT_LINES-TDLINE ';' INTO LV_MSG SEPARATED BY SPACE.
  ENDLOOP.
ENDIF.
    
# 方式二
DATA:lv_str  TYPE STRING.
"读取长文本---通过id
CALL METHOD cl_eso_extraction_tools=>extract_long_text_by_id
  EXPORTING
    iv_langu        = sy-langu "语言
    iv_text_id      = 'F01'   "文本标识
    iv_name         =  '4500000151' "文本名
    iv_object       =  'EKKO'      "文本对象
  IMPORTING
    ev_search_terms = lv_str.
WRITE :lv_str.

3.添加前导零

DATA lv_data TYPE aufk-aufnr.
lv_data = '10000001'.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
  EXPORTING
    input  = lv_data    "10000001"
  IMPORTING
    output = lv_data.   "000010000001'
	
# 在7.4以后的新版本可使用 lv_data = |{ lv_data = ALPHA = IN }|.添加前导零

4.去除前导零

DATA lv_data TYPE aufk-aufnr.
lv_data = '000010000001'.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT'
  EXPORTING
    input  = lv_data    "000010000001"
  IMPORTING
    output = lv_data.   "10000001'
	
# 在7.4以后的新版本可使用 lv_data = |{ lv_data = ALPHA = OUT }|.去除前导零

5.显示加载进度百分比效果

CALL FUNCTION 'SAPGUI_PROGRESS_INDICATOR'
 EXPORTING
   PERCENTAGE       = 65                    '显示进度百分比'
   TEXT             = '数据处理中。。。' .    '显示文本'

6.根据结构生成ALV展示数据字段目录:

DATA gt_fieldcat TYPE lvc_t_fieldcat.
CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
 EXPORTING
*   I_BUFFER_ACTIVE              =
   I_STRUCTURE_NAME             = 'ZSTRUCTURE'  "结构名称"
   I_CLIENT_NEVER_DISPLAY       = 'X'
*   I_BYPASSING_BUFFER           =
*   I_INTERNAL_TABNAME           =
  CHANGING
    CT_FIELDCAT                  = gt_fieldcat. "ALV字段目录内表"

7.通用表锁-加锁&解锁

# 加锁
DATA lv_tabname TYPE rstable-tabname.
DATA lv_msg     TYPE bapimessage.
CALL FUNCTION 'ENQUEUE_E_TABLE'
 EXPORTING
   MODE_RSTABLE         = 'E'
   TABNAME              = lv_tabname
 EXCEPTIONS
   FOREIGN_LOCK         = 1
   SYSTEM_FAILURE       = 2
   OTHERS               = 3.

IF sy-subrc <> 0.
  CONCATENATE '用户' sy-uname '当前正在操作该表!' INTO lv_msg.
  MESSAGE lv_msg TYPE 'E'.
ENDIF.

# 解锁
DATA lv_tabname TYPE rstable-tabname.
CALL FUNCTION 'DEQUEUE_E_TABLE'
  EXPORTING
    mode_rstable = 'E'
    tabname      = lv_tabname.

8.UUID32位生成器

DATA lv_id TYPE  sysuuid_c.
CALL FUNCTION 'ICF_CREATE_GUID'
  IMPORTING
    id = lv_id.    "C0A802DF9AA1606BF06B2B7E00000000"

9.动态设置ALV报表标题

DATA gv_title TYPE sytitle.
CALL FUNCTION 'RECA_GUI_SET_TITLEBAR'
    EXPORTING
     id_text1      =  gv_title.   "一般只需要一个参数,如果长度不够,则用下面的额参数进行拼接"
*     id_text2      =  gv_title
*     id_text3      =  gv_title
*     id_text4      =  gv_title.

10.不同物料单位数量比例转换

DATA:
     i_matnr  LIKE  mara-matnr,
     i_in_me  LIKE  mara-meins,
      i_out_me LIKE  mara-meins,
     i_menge  LIKE  ekpo-menge,
     e_menge  LIKE  ekpo-menge.
     CALL FUNCTION 'MD_CONVERT_MATERIAL_UNIT'
       EXPORTING
         i_matnr              = i_matnr        "物料"
         i_in_me              = i_in_me        "基本单位"
         i_out_me             = i_out_me       "待转换单位"
         i_menge              = i_menge		"待转换数量"
       IMPORTING
         e_menge              = e_menge        "转换后数量"
       EXCEPTIONS
         error_in_application = 1
         error                = 2
         OTHERS               = 3.

11.数字四舍五入转换

  CALL FUNCTION 'ROUND'
    EXPORTING
      decimals      = 2       " 保留多少位小数
      input         = lr_dat
      sign          = '+'     " + 向上取舍 - 向下取舍 (负数也一样)
    IMPORTING
      output        = lr_dat    " 输出返回结果
    EXCEPTIONS
      input_invalid = 1
      overflow      = 2
      type_invalid  = 3
      OTHERS        = 4.

12.BAPI返回消息转换

  DATA lv_msg TYPE string.
  LOOP AT lt_bapiret2 INTO DATA(ls_return) WHERE type CO 'AEX' .
    CALL FUNCTION 'MESSAGE_TEXT_BUILD'
      EXPORTING
        msgid               = ls_return-id
        msgnr               = ls_return-number
        msgv1               = ls_return-message_v1
        msgv2               = ls_return-message_v2
        msgv3               = ls_return-message_v3
        msgv4               = ls_return-message_v4
      IMPORTING
        message_text_output = lv_msg.
    CONCATENATE message lv_msg INTO message SEPARATED BY ';'.
  ENDLOOP.

13.调用表维护视图

  CALL FUNCTION 'VIEW_MAINTENANCE_CALL'
  EXPORTING
    action                       = 'U'      "操作模式:S-显示;U-更新;T-传输"
    view_name                    = 'ZMMT001'  "视图/表名"
  EXCEPTIONS
    client_reference             = 1
    foreign_lock                 = 2
    invalid_action               = 3
    no_clientindependent_auth    = 4
    no_database_function         = 5
    no_editor_function           = 6
    no_show_auth                 = 7
    no_tvdir_entry               = 8
    no_upd_auth                  = 9
    only_show_allowed            = 10
    system_failure               = 11
    unknown_field_in_dba_sellist = 12
    view_not_found               = 13
    maintenance_prohibited       = 14
    OTHERS                       = 15.
  IF sy-subrc <> 0.
* Implement suitable error handling here
  ENDIF.

14.重置系统运行时间

LOOP AT itab .
    lv_time = SY-TABIX MOD 10000.
    IF lv_time EQ 0.
      CALL FUNCTION 'TH_REDISPATCH'.
    ENDIF.
     ......
ENDLOOP.
posted @ 2021-11-10 13:48  Gjboy666  阅读(484)  评论(0编辑  收藏  举报