SAP ABAP smartforms 创建并实现调用源码(下)

SAP ABAP smartforms 创建并实现调用源码(上)
smartforms 入门详见上一篇博文。

一:报表程序调用 smartforms

示例:报表选中一行,将这行机相关数据通过表单打印出来。实际例子:采购订单表,销售订单。

调用核心代码:

  • 根据SmartForm 名称获得Form的 Function Names
    call function ‘SSF_FUNCTION_MODULE_NAME’
    exporting
    formname = uv_name
    importing
    fm_name = gv_fm_name
    exceptions
    no_form = 1
    no_function_module = 2
    others = 3.

  • 传入参数
    call function gv_fm_name
    exporting
    control_parameters = ls_ssfctrlop
    ls_student = gs_student
    tables
    it_student = it_student
    exceptions
    formatting_error = 1
    internal_error = 2
    send_error = 3
    user_canceled = 4
    others = 5.

二:进阶功能
1.初始化变量
当变量是调用 smartforms 后需要计算或转换的,可以先在全局数据定义一个变量,在初始化页写代码赋值。例:打印的列表计算总和。
SAP ABAP smartforms 创建并实现调用源码(下)_第1张图片
2.在se11中参考过了货币和数字单位字段,也需要在smartforms中的全局定义中做参考。否则报错消息编号601:参考字段&1在表格中未知 的错误。数量选择数据类型 ‘QUAN’ 货币选择数据类型 ‘CURR’。
SAP ABAP smartforms 创建并实现调用源码(下)_第2张图片
3.动态表格行数超过一页打印纸,需要转到下一页打印并且其他部分布局和内容不变的情况,在表格下方右键新建命令。
SAP ABAP smartforms 创建并实现调用源码(下)_第3张图片
PS:一定要设置条件页(仅在页为第一页)和常规属性(转到新页第二页)。

SAP ABAP smartforms 创建并实现调用源码(下)_第4张图片
除了命令所在页,后续还有几页就创建几个命令。设置的所在页和转到下一页依次累加。
SAP ABAP smartforms 创建并实现调用源码(下)_第5张图片
4.当前页码/总页码
SAP ABAP smartforms 创建并实现调用源码(下)_第6张图片
&SFSY-PAGE&/&SFSY-JOBPAGES&
5.根据判断条件决定文本显示。在条件栏输入对应关系,下图的例子表示当这个字段是’张三’的时候就显示,否则不显示。文本的话要用引号。
SAP ABAP smartforms 创建并实现调用源码(下)_第7张图片

三:全部代码

*&---------------------------------------------------------------------*
*& Report ZTEST_EXPORT
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT ZTEST_EXPORT.


*----------------------------------------------------------------------*
* Type-pools/定义类型池
*----------------------------------------------------------------------*
TYPE-POOLS: slis.

*----------------------------------------------------------------------*
* Tables/声明数据库表
*----------------------------------------------------------------------*
TABLES:ZMMTEST.

*----------------------------------------------------------------------*
* Type/自定义类型
*----------------------------------------------------------------------*
TYPES:BEGIN OF ty_alv,
        sel type char1,  "是否被选择
        id   type zmmtest-id,
        name type zmmtest-name,
        age  type zmmtest-age,
        sex  type zmmtest-sex,
        hobby type zmmtest-hobby,
        "include TYPE zmmtest,
        times     type numc4,    "打印次数
  END OF ty_alv.

DATA:gt_alv      TYPE STANDARD TABLE OF ty_alv,
     gs_alv      TYPE ty_alv,
     gt_print    type standard table of ty_alv,
     gs_student  type zmmtest,
     it_student  type TABLE OF zmmtest.
DATA:gv_fm_name TYPE rs38l_fnam.

*-------------------------------------*
* Define the Macros/定义
*----------------------------------------------------------------------*
DEFINE set_fieldcat.
  CLEAR gs_fcat_lvc .
  gs_fcat_lvc-fieldname = &1."内表的字段
  gs_fcat_lvc-outputlen = &2."输出长度
  gs_fcat_lvc-scrtext_l = &3."在ALV里面显示的名字
  gs_fcat_lvc-just      = &4."水平对齐方式,L左对齐,R右对齐。
  gs_fcat_lvc-no_zero   = &5."去除前导零
  APPEND gs_fcat_lvc TO gt_fcat_lvc .
END-OF-DEFINITION.
*----------------------------------------------------------------------*
* 声明ALV对象
*----------------------------------------------------------------------*
DATA:gs_layout_lvc TYPE lvc_s_layo,
     gt_fcat_lvc   TYPE lvc_t_fcat,
     gs_fcat_lvc   TYPE lvc_s_fcat.
*----------------------------------------------------------------------*
* Internal table and work area/定义内表和工作区
*----------------------------------------------------------------------*

*----------------------------------------------------------------------*
* Selection  screen/定义屏幕
*----------------------------------------------------------------------*
SELECTION-SCREEN FUNCTION KEY: 1 .
SELECTION-SCREEN BEGIN OF BLOCK block1 WITH FRAME TITLE TEXT-t01.
    SELECT-OPTIONS: s_id FOR zmmtest-id,
                    s_name FOR zmmtest-name,
                    s_age FOR zmmtest-age,
                    s_sex FOR zmmtest-sex,
                    s_hobby FOR zmmtest-hobby.
SELECTION-SCREEN END OF BLOCK block1.
*----------------------------------------------------------------------*
* Initialization/初始事件
*----------------------------------------------------------------------*
INITIALIZATION.

*----------------------------------------------------------------------*
* At selection-screen/屏幕事件
*----------------------------------------------------------------------*
AT SELECTION-SCREEN.

*----------------------------------------------------------------------*
* AT SELECTION-SCREEN OUTPUT
*----------------------------------------------------------------------*
AT SELECTION-SCREEN OUTPUT.
*----------------------------------------------------------------------*
* Start-of-selection/开始选择事件
*----------------------------------------------------------------------*
START-OF-SELECTION.
*取数据
  PERFORM frm_get_data.
*ALV展示
  PERFORM frm_display_alv.
*----------------------------------------------------------------------*
* End-of-selection/结束选择事件
*----------------------------------------------------------------------*
END-OF-SELECTION.
*&---------------------------------------------------------------------*
*& Form FRM_GET_DATA
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
FORM frm_get_data .
  SELECT *
    INTO CORRESPONDING FIELDS OF TABLE gt_alv
    from zmmtest
    where id in s_id
       AND name in s_name
       AND age  in s_age
       AND sex  in s_sex
       AND hobby in s_hobby.

endform.

*&---------------------------------------------------------------------*
*& Form FRM_DISPLAY_ALV
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
FORM frm_display_alv .
  set_fieldcat:'ID'  '' '学号' '' '',
               'NAME' '' '学生姓名' '' '',
               'AGE' '' '年龄' '' '',
               'SEX' '' '性别' '' '',
               'HOBBY' '' '兴趣爱好' '' ''.
  CLEAR:gs_layout_lvc.
  gs_layout_lvc-zebra     = 'X'.
  gs_layout_lvc-cwidth_opt   = 'X'.
  gs_layout_lvc-box_fname   = 'SEL'.   "选择框
  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
    EXPORTING
      i_callback_program = sy-repid
      i_callback_pf_status_set = 'FRM_PF_STATUS'
      i_callback_user_command  = 'FRM_USER_COMMAND'
      is_layout_lvc      = gs_layout_lvc
      it_fieldcat_lvc    = gt_fcat_lvc
      i_save             = 'A'
    TABLES
      t_outtab           = gt_alv
    EXCEPTIONS
      program_error      = 1
      OTHERS             = 2.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
    WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*&      Form  FRM_PF_STATUS
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM frm_pf_status USING pt_extab TYPE slis_t_extab .
  SET PF-STATUS 'STATUS' .

ENDFORM.
*&---------------------------------------------------------------------*
*&      Form  FRM_USER_COMMAND
*&---------------------------------------------------------------------*
*       自定义的用户命令处理
*----------------------------------------------------------------------*
*      -->PV_UCOMM      用户命令类型
*      -->PS_SELFIELD  用户命令信息
*----------------------------------------------------------------------*
form frm_user_command using pv_ucomm type sy-ucomm
      ps_selfield type slis_selfield.
  data: lr_grid type ref to cl_gui_alv_grid.
*  ps_selfield-refresh    = 'X'.
*  ps_selfield-col_stable = 'X'.
*  ps_selfield-row_stable = 'X'.
  data : lv_stable type lvc_s_stbl.
  lv_stable-row = '1'.
  lv_stable-col = '1'.

  call function 'GET_GLOBALS_FROM_SLVC_FULLSCR'
    importing
      e_grid = lr_grid.
  call method lr_grid->check_changed_data.

  case pv_ucomm.
    when '&PRINT'.
      perform frm_print_data.
    when 'ALL'.
      perform frm_select_all.
    when 'SAL'.
      perform frm_deselect_all.
    when '&IC1'.
      read table gt_alv index ps_selfield-tabindex into gs_alv.  "获取单击行
      if ps_selfield-fieldname = 'SEL' and gs_alv-sel = ''.
        gs_alv-sel = 'X'.
        modify gt_alv from gs_alv transporting sel where id = gs_alv-id.
      elseif ps_selfield-fieldname = 'SEL' and gs_alv-sel = 'X'.
        gs_alv-sel = ''.
        modify gt_alv from gs_alv transporting sel where id = gs_alv-id.
      endif.
  endcase.
*刷新
  call method lr_grid->refresh_table_display
    exporting
      is_stable = lv_stable.
endform.

*&---------------------------------------------------------------------*
*& Form FRM_SELECT_ALL
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
form frm_select_all .
  loop at gt_alv into gs_alv.
    gs_alv-sel = 'X'.
    modify gt_alv from gs_alv index sy-tabix transporting sel.
  endloop.
endform.
*&---------------------------------------------------------------------*
*& Form FRM_DESELECT_ALL
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
form frm_deselect_all .
  loop at gt_alv into gs_alv.
    gs_alv-sel = ''.
    modify  gt_alv from gs_alv index sy-tabix transporting sel .
  endloop.
endform.

*&---------------------------------------------------------------------*
*& Form FRM_PRINT_DATA
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
form frm_print_data .
  refresh gt_print.
  loop at gt_alv into gs_alv where sel = 'X'.
    append gs_alv to gt_print.
  endloop.
  if sy-langu  = '1'.
    if gt_print is initial.
      message '请选择需打印的凭证' type 'E' display like 'S'.
    endif.
  else.
    if gt_print is initial.
      message 'Please select the certificate to print' type 'E' display like 'S'.
    endif.
  endif.

*获取数据
"perform frm_select_data.
  if sy-langu = '1'.
    perform frm_open_print_window using 'ZMMFTEST'.
  else.
    perform frm_open_print_window using 'ZMMFTEST_EN'.
  endif.
  perform frm_get_print_data.
  perform frm_close_print_window.
endform.
*&---------------------------------------------------------------------*
*& Form FRM_SELECT_DATA
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
form frm_select_data .
"根据实际查询需要
endform.

*&---------------------------------------------------------------------*
*& Form FRM_OPEN_PRINT_WINDOW
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
form frm_open_print_window using uv_name type tdsfname..
  data:ls_ssfcompop type ssfcompop,
       ls_ssfctrlop type ssfctrlop.
  ls_ssfcompop-tdimmed = 'X'.
  ls_ssfcompop-tddelete = 'X'.
  ls_ssfcompop-tdiexit = 'X'.

  ls_ssfctrlop-no_open = 'X'.
  ls_ssfctrlop-no_close = 'X'.
  ls_ssfctrlop-langu = sy-langu.
* 根据SmartForm 名称获得Form的 Function Names
  call function 'SSF_FUNCTION_MODULE_NAME'
    exporting
      formname           = uv_name
    importing
      fm_name            = gv_fm_name
    exceptions
      no_form            = 1
      no_function_module = 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.
*****打开打印窗口
  call function 'SSF_OPEN'
    exporting
      user_settings      = ''
      output_options     = ls_ssfcompop
      control_parameters = ls_ssfctrlop
    exceptions
      formatting_error   = 1
      internal_error     = 2
      send_error         = 3
      user_canceled      = 4
      others             = 5.
  if sy-subrc <> 0.
    message id sy-msgid type sy-msgty number sy-msgno
    with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  endif.
endform.
*&---------------------------------------------------------------------*
*& Form FRM_CLOSE_PRINT_WINDOW
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
form frm_close_print_window .
  data: ls_ssfcrescl type ssfcrescl.

*****关闭打印窗口
  call function 'SSF_CLOSE'
    importing
      job_output_info  = ls_ssfcrescl
    exceptions
      formatting_error = 1
      internal_error   = 2
      send_error       = 3
      others           = 4.
  if sy-subrc <> 0.
    message id sy-msgid type sy-msgty number sy-msgno
    with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  endif.
*统计打印次数
  if ls_ssfcrescl-outputdone = 'X'.
    loop at gt_print into gs_alv.

*更改ALV数据
      gs_alv-times = gs_alv-times + 1.
      modify gt_alv from gs_alv transporting times where id = gs_alv-id.
      if sy-langu = '1'.
        message '打印成功' type 'S'.
      else.
        message 'Print success' type 'S'.
      endif.
    endloop.
  endif.
endform.
*&---------------------------------------------------------------------*
*& Form FRM_GET_PRINT_DATA
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
form frm_get_print_data .
  loop at gt_print into gs_alv.
*打印
     gs_student = CORRESPONDING #( gs_alv ).

     it_student =  CORRESPONDING #( gt_alv ).

    perform frm_print.
  endloop.

endform.

*&---------------------------------------------------------------------*
*& Form FRM_PRINT
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
form frm_print.
  data:ls_ssfctrlop type ssfctrlop.
  data lv_page type i.
  ls_ssfctrlop-no_open = 'X'.
  ls_ssfctrlop-no_close = 'X'.
****打印
  call function gv_fm_name
    exporting
      control_parameters = ls_ssfctrlop
      ls_student          = gs_student
    tables
      it_student            = it_student
    exceptions
      formatting_error   = 1
      internal_error     = 2
      send_error         = 3
      user_canceled      = 4
      others             = 5.
  if sy-subrc <> 0.
    message id sy-msgid type sy-msgty number sy-msgno
    with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  endif.
endform.

你可能感兴趣的:(ABAP,ABAP,SAP,SMARTFORMS)