记录ABAP开发的日常——常用标准程序流程模板
作者:互联网
前言: 在 SAP 开发中,写程序是我们必不可少的流程,但由于 ABAP 程序是一个事件驱动型程序,所以在编写代码时,代码的顺序可能就会跟编程者的书写风格有关,不同的开发者具有不同的开发风格,导致编写的 ABAP 程序风格迥异,没有一个统一的标准,不便于解读。为了解决这一问题,开发者们开始推荐使用 ABAP 程序编写规范,在此便诞生了众多的 ABAP 程序编写标准模板,今天小度 ° 也给大家带来了一份 ABAP 模板:
ABAP 程序编写标准模板:
*&--------------------------「程序说明-START」-------------------------*
*& Report Z_X_XXXXX01
*& Module : Module \ SubModule
*& Created : ZHANGYH (2020.12.21)
*& Modified : ZHANGYH (2020.12.21)
*& Intention : 程序标题
* Detail(详细说明):
*&--------------------------「程序说明-END」--------------------------*
*&--------------------------「程序引入」------------------------------*
*INCLUDE Z_X_TOP . " TOP 子程序,用来声明全局变量
*--------以下三个子程序只有在多个程序调用同一逻辑时才建议使用---------*
*INCLUDE Z_X_O01 . " PBO 子程序
*INCLUDE Z_X_I01 . " PAI 子程序
*INCLUDE Z_X_F01 . " FORM 子程序
*&--------------------------「类型声明」------------------------------*
*TABLES:ITAB1.
* 定义ALV输出结构
TYPES:BEGIN OF OUTPUT,
COLOR(4) TYPE C, "行、单元格颜色"
ISEDIT TYPE C, "单元格是否编辑"
END OF OUTPUT.
* 定义输出内表
DATA: GT_OUTPUT TYPE TABLE OF OUTPUT,
GS_OUTPUT TYPE OUTPUT.
* 定义ALV数据选择内表
DATA: GT_CHECKED TYPE TABLE OF OUTPUT,
GS_CHECKED TYPE OUTPUT.
* 定义SmartForms抬头内表
DATA: GT_HEAD TYPE TABLE OF OUTPUT,
GS_HEAD TYPE OUTPUT.
* 定义SmartForms行项目内表
DATA: GT_DATA TYPE TABLE OF OUTPUT,
GS_DATA TYPE OUTPUT.
FIELD-SYMBOLS:<FS_OPT> TYPE OUTPUT.
* 定义AVL变量
DATA: GT_FIELDCAT TYPE LVC_T_FCAT,
GS_FIELDCAT TYPE LVC_S_FCAT,
GS_DECIMALS TYPE LVC_DECMLS,
GS_LAYOUT TYPE LVC_S_LAYO.
* 定义SMART FORMS变量
DATA: SSF_NAME TYPE TDSFNAME,
L_FM_NAME TYPE RS38L_FNAM,
CONTROL TYPE SSFCTRLOP,
OUTPUT_OPTIONS TYPE SSFCOMPOP,
JOB_OUTPUT_INFO TYPE SSFCRESCL.
*&------------------------「选择屏幕声明」----------------------------*
SELECTION-SCREEN:BEGIN OF BLOCK BK1 WITH FRAME TITLE TEXT-001 .
* 区间
SELECT-OPTIONS:
S_FIELD FOR ITAB-FIELD NO INTERVALS NO-EXTENSION .
SELECTION-SCREEN ULINE.
* 单值
PARAMETERS:P_FIELD AS CHECKBOX, "复选框"
P_RTN1 RADIOBUTTON GROUP G1, "单选按钮一"
P_RTN2 RADIOBUTTON GROUP G1. "单选按钮二"
SELECTION-SCREEN END OF BLOCK BK1.
*&------------------------「程序主进程」------------------------------*
INITIALIZATION.
* PERFORM INI_SELECTION_SCREEN. " 初始化选择屏上的变量
AT SELECTION-SCREEN OUTPUT.
* PERFORM FRM_INI_SCREEN. " 初始化选择屏幕
AT SELECTION-SCREEN .
* PERFORM FRM_INI_DATA. " 初始化全局变量
START-OF-SELECTION.
PERFORM FRM_PRECESS_DATA. " 主要数据处理逻辑
END-OF-SELECTION.
* PERFORM FRM_DISPLAY_DATA. " 数据展示输出
AT LINE-SELECTION.
* PERFORM FRM_PRECESS_LINE_SELECTED. " 行选择时的处理
AT USER-COMMAND.
* PERFORM FRM_PRECESS_USER_COMMAND. " 用自定义命令按钮时的处理
TOP-OF-PAGE.
* PERFORM FRM_PAGE_HEADER. " 页眉
END-OF-PAGE.
* PERFORM FRM_PAGE_FOOTER. " 页脚
*&---------------------------------------------------------------------*
*& Form INI_SELECTION_SCREEN
*&---------------------------------------------------------------------*
*& 初始化选择屏上的变量
*&---------------------------------------------------------------------*
FORM INI_SELECTION_SCREEN .
ENDFORM.
*&---------------------------------------------------------------------*
*& Form INI_DATA
*&---------------------------------------------------------------------*
*& 初始化全局变量
*&---------------------------------------------------------------------*
FORM FRM_INI_DATA .
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_PRECESS_DATA
*&---------------------------------------------------------------------*
*& 主要数据处理逻辑
*&---------------------------------------------------------------------*
FORM FRM_PRECESS_DATA .
PERFORM SEL_DATA_TO_ITAB. " 从透明表中取数据赋给内表
PERFORM CHECK_DATA_FROM_ITAB. " 内表数据进行效验
PERFORM CMP_DATA_FROM_ITAB. " 内表中数据计算处理
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_DISPLAY_DATA
*&---------------------------------------------------------------------*
*& 数据展示输出
*&---------------------------------------------------------------------*
FORM FRM_DISPLAY_DATA .
PERFORM FRM_SET_LAYOUT. " 设置ALV布局
PERFORM FRM_SET_FIELDCAT. " 设置ALV展示字段
PERFORM FRM_DISPLAY_ALV. " 设置ALV数据展示
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_PRECESS_LINE_SELECTED
*&---------------------------------------------------------------------*
*& 行选择时的处理
*&---------------------------------------------------------------------*
FORM FRM_PRECESS_LINE_SELECTED .
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_PRECESS_USER_COMMAND
*&---------------------------------------------------------------------*
*& 用自定义命令按钮时的处理
*&---------------------------------------------------------------------*
FORM FRM_PRECESS_USER_COMMAND .
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_PAGE_HEADER
*&---------------------------------------------------------------------*
*& 页眉
*&---------------------------------------------------------------------*
FORM FRM_PAGE_HEADER .
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_PAGE_FOOTER
*&---------------------------------------------------------------------*
*& 页脚
*&---------------------------------------------------------------------*
FORM FRM_PAGE_FOOTER .
ENDFORM.
*&---------------------------------------------------------------------*
*& Form SEL_DATA_TO_ITAB
*&---------------------------------------------------------------------*
*& 从透明表中取数据赋给内表
*&---------------------------------------------------------------------*
FORM SEL_DATA_TO_ITAB .
*&--------------------------「取主线数据」-----------------------------*
SELECT *
INTO CORRESPONDING FIELDS OF TABLE GT_OUTPUT
FROM ITAB1 AS A INNER JOIN ITAB2 AS B ON A~FIELD = B~FIELD UP TO 100 ROWS.
*&--------------------------「取支线数据」-----------------------------*
IF GT_OUTPUT IS NOT INITIAL.
* SELECT...
* LOOP AT GT_OUTPUT ASSIGNING <FS_OPT>.
* ENDLOOP.
ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form CHECK_DATA_FROM_ITAB
*&---------------------------------------------------------------------*
*& 内表数据进行效验
*&---------------------------------------------------------------------*
FORM CHECK_DATA_FROM_ITAB .
ENDFORM.
*&---------------------------------------------------------------------*
*& Form CMP_DATA_FROM_ITAB
*&---------------------------------------------------------------------*
*& 内表中数据计算处理
*&---------------------------------------------------------------------*
FORM CMP_DATA_FROM_ITAB .
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_INI_SCREEN
*&---------------------------------------------------------------------*
*& 初始化选择屏幕
*&---------------------------------------------------------------------*
FORM FRM_INI_SCREEN .
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_SET_LAYOUT
*&---------------------------------------------------------------------*
*& 设置ALV布局
*&---------------------------------------------------------------------*
FORM FRM_SET_LAYOUT .
GS_LAYOUT-ZEBRA = 'X'.
GS_LAYOUT-SEL_MODE = 'X'.
GS_LAYOUT-CWIDTH_OPT = 'X'.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_SET_FIELDCAT
*&---------------------------------------------------------------------*
*& 设置ALV展示字段
*&---------------------------------------------------------------------*
FORM FRM_SET_FIELDCAT .
*--方案一:
CLEAR GT_FIELDCAT.
* 调用生产ALV展示字段
PERFORM FRM_SET_FIELDS USING: 'CHECKBOX' '请选择' '' '' 'X' 'X',
'ZNUM' '序号' '' '' '' '' .
ENDFORM.
*--方案二:
CLEAR GT_FIELDCAT.
GT_FIELDCAT = VALUE # ( BASE GT_FIELDCAT ( FIELDNAME = 'CHECKBOX' REPTEXT = '请选择' NO_ZERO = '' NO_OUT = '' CHECKBOX = 'X' EDIT = 'X')
).
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_SET_FIELDS
*&---------------------------------------------------------------------*
*& 定义字段自动生成
*&---------------------------------------------------------------------*
FORM FRM_SET_FIELDS USING PV_FIELDNAME PV_REPTEXT PV_ZERO PV_OUT PV_CHECKBOX PV_EDIT.
INSERT VALUE #(
FIELDNAME = PV_FIELDNAME
REPTEXT = PV_REPTEXT
NO_ZERO = PV_ZERO
NO_OUT = PV_OUT
CHECKBOX = PV_CHECKBOX
EDIT = PV_EDIT
)
INTO TABLE GT_FIELDCAT.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_DISPLAY_ALV
*&---------------------------------------------------------------------*
*& 定义ALV输出函数
*&---------------------------------------------------------------------*
FORM FRM_DISPLAY_ALV .
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
EXPORTING
* I_INTERFACE_CHECK = ' '
* I_BYPASSING_BUFFER =
* I_BUFFER_ACTIVE =
I_CALLBACK_PROGRAM = SY-REPID
I_CALLBACK_PF_STATUS_SET = 'SET_PF_STATUS'
I_CALLBACK_USER_COMMAND = 'ALV_USER_COMMAND'
* I_CALLBACK_TOP_OF_PAGE = ' '
* I_CALLBACK_HTML_TOP_OF_PAGE = ' '
* I_CALLBACK_HTML_END_OF_LIST = ' '
* I_STRUCTURE_NAME =
* I_BACKGROUND_ID = ' '
* I_GRID_TITLE =
* I_GRID_SETTINGS =
IS_LAYOUT_LVC = GS_LAYOUT
IT_FIELDCAT_LVC = GT_FIELDCAT
* IT_EXCLUDING =
* IT_SPECIAL_GROUPS_LVC =
* IT_SORT_LVC =
* IT_FILTER_LVC =
* IT_HYPERLINK =
* IS_SEL_HIDE =
* I_DEFAULT = 'X'
I_SAVE = 'A'
* IS_VARIANT =
* IT_EVENTS =
* IT_EVENT_EXIT =
* IS_PRINT_LVC =
* IS_REPREP_ID_LVC =
* I_SCREEN_START_COLUMN = 0
* I_SCREEN_START_LINE = 0
* I_SCREEN_END_COLUMN = 0
* I_SCREEN_END_LINE = 0
* I_HTML_HEIGHT_TOP =
* I_HTML_HEIGHT_END =
* IT_ALV_GRAPHICS =
* IT_EXCEPT_QINFO_LVC =
* IR_SALV_FULLSCREEN_ADAPTER =
* IMPORTING
* E_EXIT_CAUSED_BY_CALLER =
* ES_EXIT_CAUSED_BY_USER =
TABLES
T_OUTTAB = GT_OUTPUT
EXCEPTIONS
PROGRAM_ERROR = 1
OTHERS = 2.
IF SY-SUBRC <> 0.
* Implement suitable error handling here
ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form SET_PF_STATUS
*&---------------------------------------------------------------------*
*& 定义工具栏按钮状态
*&---------------------------------------------------------------------*
FORM SET_PF_STATUS USING PT_EXCLUDE TYPE KKBLO_T_EXTAB .
* 声明按钮设置内表
* DATA:LT_EXCLUDE TYPE KKBLO_T_EXTAB WITH HEADER LINE.
* DATA:LS_EXCLUDE LIKE LINE OF LT_EXCLUDE.
* 添加按钮例子:
* LS_EXCLUDE = '&ADDLINE'.
* APPEND LS_EXCLUDE TO LT_EXCLUDE[].
* CLEAR LS_EXCLUDE.
* LS_EXCLUDE = '&DELLINE'.
* APPEND LS_EXCLUDE TO LT_EXCLUDE[].
* CLEAR LS_EXCLUDE.
* LS_EXCLUDE = '&SAVEMD'.
* APPEND LS_EXCLUDE TO LT_EXCLUDE[].
* CLEAR LS_EXCLUDE.
* SET PF-STATUS 'STANDARD' EXCLUDING LT_EXCLUDE[] .
* 如果条件满足:
* IF P_FILTER IS NOT INITIAL.
* SET PF-STATUS 'STANDARD' .
* ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form ALV_USER_COMMAND
*&---------------------------------------------------------------------*
*& 定义ALV按钮事件
*&---------------------------------------------------------------------*
FORM ALV_USER_COMMAND USING R_UCOMM LIKE SY-UCOMM RS_SELFIELD TYPE SLIS_SELFIELD .
* 设置ALV内容改变事件回调
* DATA: LR_GRID TYPE REF TO CL_GUI_ALV_GRID.
* DATA: LV_INDEX TYPE I.
* CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
* IMPORTING
* E_GRID = LR_GRID.
* CALL METHOD LR_GRID->CHECK_CHANGED_DATA.
* 设置按钮触发事件
* CASE R_UCOMM.
* WHEN '&IC1'. " ALV双击事件
* 获取当前双击的行目索引"
* READ TABLE GT_OUTPUT INDEX RS_SELFIELD-TABINDEX ASSIGNING <FS_OPT>.
* 判断是否是指定的字段?"
* IF RS_SELFIELD-FIELDNAME EQ 'FIELD1' OR RS_SELFIELD-FIELDNAME EQ 'FIELD2'.
* PERFORM FRM_ALV_EVENT_IC1.
* ENDIF.
* WHEN '&PRINT'.
* PERFORM FRM_ALV_EVENT_PRINT. " 调用SF打印事件子例程
* WHEN '&BDC'.
* PERFORM FRM_ALV_EVENT_BDC. " 调用BDC批导事件子例程
* WHEN '&BAPI'.
* PERFORM FRM_ALV_EVENT_BAPI. " 调用BAPI批导事件子例程
* WHEN '&FC01'.
* PERFORM FRM_ALV_EVENT_FC01. " 调用FC01按钮事件子例程
* WHEN '&FC02'.
* PERFORM FRM_ALV_EVENT_FC02. " 调用FC02按钮事件子例程
* WHEN OTHERS.
* ENDCASE.
* RS_SELFIELD-REFRESH = 'X'. " 刷新ALV屏幕
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_ALV_EVENT_IC1
*&---------------------------------------------------------------------*
*& 定义ALV双击事件子例程
*&---------------------------------------------------------------------*
FORM FRM_ALV_EVENT_IC1 .
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_ALV_EVENT_PRINT
*&---------------------------------------------------------------------*
*& 定义SF打印事件子例程
*&---------------------------------------------------------------------*
FORM FRM_ALV_EVENT_PRINT .
PERFORM FRM_DEAL_DATA. " 数据预处理
PERFORM FRM_DATA_PRINT. " SmartForms数据打印
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_ALV_EVENT_BDC
*&---------------------------------------------------------------------*
*& 定义BDC批导事件子例程
*&---------------------------------------------------------------------*
FORM FRM_ALV_EVENT_BDC .
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_ALV_EVENT_BAPI
*&---------------------------------------------------------------------*
*& 定义BAPI批导事件子例程
*&---------------------------------------------------------------------*
FORM FRM_ALV_EVENT_BAPI .
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_ALV_EVENT_FC01
*&---------------------------------------------------------------------*
*& 定义FC01按钮事件子例程
*&---------------------------------------------------------------------*
FORM FRM_ALV_EVENT_FC01 .
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_ALV_EVENT_FC02
*&---------------------------------------------------------------------*
*& 定义FC02按钮事件子例程
*&---------------------------------------------------------------------*
FORM FRM_ALV_EVENT_FC02 .
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_DEAL_DATA
*&---------------------------------------------------------------------*
*& SF打印数据预处理
*&---------------------------------------------------------------------*
FORM FRM_DEAL_DATA .
* 清空勾选内表数据
* CLEAR GT_CHECKED.
* 将勾选的数据放到临时内表中
* LOOP AT GT_OUTPUT ASSIGNING <FS_OPT> WHERE CHECKBOX = 'X' .
* 如果被勾选,则将数据添加到勾选内表中"
* MOVE-CORRESPONDING <FS_OPT> TO GS_CHECKED.
* APPEND GS_CHECKED TO GT_CHECKED.
* 如果返回状态值为0
* IF SY-SUBRC = 0.
* 提交事务
* COMMIT WORK.
* 如果返回状态不为0
* ELSE.
* 回滚事务
* ROLLBACK WORK.
* ENDIF.
* 清空结构
* CLEAR GS_CHECKED.
* ENDLOOP.
* 当有勾选的数据时才调用打印
* IF GT_CHECKED IS NOT INITIAL.
* 遍历勾选内表 获取打印抬头数据
* CLEAR GT_HEAD.
* LOOP AT GT_CHECKED INTO GS_CHECKED .
* 根据条件获取抬头
* READ TABLE GT_HEAD WITH KEY FIELD = GS_CHECKED-FIELD INTO GS_HEAD.
* 抬头表为空,肯定获取不到数据,所以状态不为0
* IF SY-SUBRC <> 0.
* 将需要的数据加入到抬头内表
* MOVE-CORRESPONDING GS_CHECKED TO GS_HEAD.
* APPEND GS_HEAD TO GT_HEAD.
* ENDIF.
* 清空勾选结构
* CLEAR GS_CHECKED.
* ENDLOOP.
* ELSE.
* 没有勾选数据
* MESSAGE '请勾选数据!' TYPE 'E' .
* ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_DATA_PRINT
*&---------------------------------------------------------------------*
*& SmartForms数据打印
*&---------------------------------------------------------------------*
FORM FRM_DATA_PRINT .
* 定义smartforms参数,实现假脱机
* CONTROL-PREVIEW = '' .
* CONTROL-NO_DIALOG = '' .
* CONTROL-NO_OPEN = 'X' .
* CONTROL-NO_CLOSE = 'X' .
* OUTPUT_OPTIONS-TDIMMED = 'X' .
* OUTPUT_OPTIONS-TDARMOD = 1 .
* GS_OUTPUT_OPTIONS-TDIEXIT = 'X' .
* 开启假脱机"
* PERFORM FRM_OPEN_SSF.
* 开启循环打印
* 遍历抬头表 根据指定条件从勾选内表中获取行项目数据
* LOOP AT GT_HEAD INTO GS_HEAD .
* 清空行项目表
* CLEAR GT_DATA.
* 遍历勾选内表,根据抬头表的条件
* LOOP AT GT_CHECKED INTO GS_CHECKED WHERE FIELD = GS_HEAD-FIELD.
* 将需要的行项目数据放入数据表内
* MOVE-CORRESPONDING GS_CHECKED TO GS_DATA.
* APPEND GS_DATA TO GT_DATA.
* 清空勾选结构
* CLEAR GS_CHECKED.
* ENDLOOP.
* 调用SmartForms、生成函数接口"
* CALL FUNCTION 'SSF_FUNCTION_MODULE_NAME'
* EXPORTING
* FORMNAME = SSF_NAME
* ARIANT = ' '
* DIRECT_CALL = ' '
* IMPORTING
* FM_NAME = L_FM_NAME
* EXCEPTIONS
* NO_FORM = 1
* NO_FUNCTION_MODULE = 2
* OTHERS = 3 .
* 调用SMARTFORMS生成接口
* CALL FUNCTION L_FM_NAME
* EXPORTING
* 传递需要打印的数据
* TAB_HEAD = GS_HEAD
* 设置假脱机参数
* OUTPUT_OPTIONS = OUTPUT_OPTIONS
* CONTROL_PARAMETERS = CONTROL
* TABLES
* 传入打印数据参数"
* ITAB1 = GT_DATA.
* IF SY-SUBRC = 0.
* ENDIF.
* CLEAR GS_HEAD.
* ENDLOOP.
* 打印结束,关闭假脱机"
* PERFORM FRM_CLOSE_SSF.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_OPEN_SSF
*&---------------------------------------------------------------------*
*& 定义假脱机开始参数设置
*&---------------------------------------------------------------------*
FORM FRM_OPEN_SSF .
CALL FUNCTION 'SSF_OPEN'
EXPORTING
USER_SETTINGS = 'X'
CONTROL_PARAMETERS = CONTROL
OUTPUT_OPTIONS = OUTPUT_OPTIONS
EXCEPTIONS
FORMATTING_ERROR = 1
INTERNAL_ERROR = 2
SEND_ERROR = 3
USER_CANCELED = 4
OTHERS = 5.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_CLOSE_SSF
*&---------------------------------------------------------------------*
*& 定义假脱机结束参数设置
*&---------------------------------------------------------------------*
FORM FRM_CLOSE_SSF .
CALL FUNCTION 'SSF_CLOSE'
IMPORTING
JOB_OUTPUT_INFO = JOB_OUTPUT_INFO
EXCEPTIONS
FORMATTING_ERROR = 1
INTERNAL_ERROR = 2
SEND_ERROR = 3
OTHERS = 4.
ENDFORM.
标签:GS,&---------------------------------------------------------------------,FR 来源: https://www.cnblogs.com/zyhcs/p/16193272.html