Showing posts with label Simple Reports. Show all posts
Showing posts with label Simple Reports. Show all posts

Monday, 8 October 2018

FM to fill Blank Space

FUNCTION zfm_table_fill.
*"----------------------------------------------------------------------
*"*"Local Interface:
*"  IMPORTING
*"     REFERENCE(IV_CONS) TYPE  CHAR10 DEFAULT 'NA'
*"  CHANGING
*"     REFERENCE(CT_TABLE) TYPE  STANDARD TABLE
*"----------------------------------------------------------------------
*^ Description : Fills the blank spaces of the internal table with a constant value
*^--------------------------------------------------------------------^*

  DATA: lo_struct_descr TYPE REF TO cl_abap_structdescr,
        lt_where        TYPE TABLE OF string.

  CHECK ct_table[] IS NOT INITIAL.
  ASSIGN ct_table[ 1 ] TO FIELD-SYMBOL(<fs_table>).
  CHECK sy-subrc EQ 0.

  lo_struct_descr ?= cl_abap_structdescr=>describe_by_data( <fs_table> ).

  "Column wise updating with a constant value
  APPEND INITIAL LINE TO lt_where ASSIGNING FIELD-SYMBOL(<fs_where>).

  LOOP AT lo_struct_descr->components ASSIGNING FIELD-SYMBOL(<fs_cmp>).
    ASSIGN ct_table[ (<fs_cmp>-name) = space ] TO <fs_table>.
    IF sy-subrc EQ 0.
      TRY .
          ASSIGN COMPONENT <fs_cmp>-name OF STRUCTURE <fs_table> TO FIELD-SYMBOL(<fs_table_field>).
          <fs_table_field> = iv_cons.
          <fs_where> = |{ <fs_cmp>-name } EQ space|.
          MODIFY ct_table FROM <fs_table> TRANSPORTING (<fs_cmp>-name) WHERE (lt_where).
        CATCH cx_sy_conversion_no_number.
      ENDTRY.
    ENDIF.
  ENDLOOP.
ENDFUNCTION.

Monday, 17 April 2017

Excel output with Coloured Cell

The following program has been copied from here. Only a small change has been made for the cell colour so that the background remains transparent and only the heading have a cell colour. The followings are the valid colour indexes:



*&---------------------------------------------------------------------*
*& Report  ZEXCEL
*&
*&---------------------------------------------------------------------*
*&
*& EXCEL OUTPUT WITH CELL COLOURING
*&---------------------------------------------------------------------*

REPORT zexcel.

INCLUDE ole2incl.
FIELD-SYMBOLS: <val> TYPE any.
DATA: row_cnt TYPE i.
TYPES: BEGIN OF t_excel,
  material_info(20),
  sugg_price(20),
  cost(20),
  comments(100),
END OF t_excel.
DATA: r_excel TYPE t_excel.
DATA: i_excel TYPE TABLE OF t_excel WITH HEADER LINE.
CONSTANTS:
  xlcenter TYPE i VALUE '-4108',
  xlbottom TYPE i VALUE '-4107',
  xlleft TYPE i VALUE '-4131',
  xlright TYPE i VALUE '-4152'.
CONSTANTS:
  xlcontinuous TYPE i VALUE '1',
  xlinsidevertical TYPE i VALUE '11',
  xlthin TYPE i VALUE '2',
  xllandscape TYPE i VALUE '2',
  xlportrait TYPE i VALUE '1',
  xlletter TYPE i VALUE '1',
  xllegal TYPE i VALUE '5',
  xlthick TYPE i VALUE '4',
  xlnone TYPE i VALUE '-4142',
  xlautomatic TYPE i VALUE '-4105'.
DATA:
  hexcel TYPE ole2_object, " Excel object
  hworkbooks TYPE ole2_object, " list of workbooks
  hworkbook TYPE ole2_object, " workbook
  hsheet TYPE ole2_object, " worksheet object
  hrange TYPE ole2_object, " range object
  hrange2 TYPE ole2_object, " range object
  hborders TYPE ole2_object, " Border object
  hinterior TYPE ole2_object, " interior object - for coloring
  hcolumn TYPE ole2_object, "column
  hcell TYPE ole2_object, " cell
  hfont TYPE ole2_object, " font
  hselected TYPE ole2_object, " range object
  hpicture TYPE ole2_object, "picture object
  hlogo TYPE ole2_object. "Logo object


SELECTION-SCREEN BEGIN OF BLOCK b1.
SELECTION-SCREEN SKIP 1.
PARAMETER: wraptext AS CHECKBOX.
SELECTION-SCREEN END OF BLOCK b1.


row_cnt = 1.
PERFORM build_dummy_vals.
PERFORM start_excel.
PERFORM build_header_line USING row_cnt.
PERFORM pass_records USING row_cnt.
PERFORM release_excel.
*&---------------------------------------------------------------------*
*&      Form  Build_Dummy_Vals
*&---------------------------------------------------------------------*
FORM build_dummy_vals .
  DATA: matnum(5) TYPE n.
  DATA: baseprice(3) TYPE n.
  DO 5 TIMES.
    matnum = matnum + 50.
    CLEAR r_excel.
    CONCATENATE 'Material ' matnum INTO r_excel-material_info
    SEPARATED BY space.
    r_excel-sugg_price = baseprice * matnum.
    r_excel-cost = ( baseprice * matnum ) / 2.
    CONCATENATE 'Comments for Material ' matnum INTO r_excel-comments
    SEPARATED BY space.
    APPEND r_excel TO i_excel.
  ENDDO.
ENDFORM. " Build_Dummy_Vals
*&---------------------------------------------------------------------*
*&      Form  Start_Excel
*&---------------------------------------------------------------------*
FORM start_excel.
  CREATE OBJECT hexcel 'EXCEL.APPLICATION'.
  PERFORM err_hdl.

*get list of workbooks, initially empty
  CALL METHOD OF
      hexcel
      'Workbooks' = hworkbooks.
  PERFORM err_hdl.

*add a new workbook
  CALL METHOD OF
      hworkbooks
      'Add'      = hworkbook.
  PERFORM err_hdl.

*Get Worksheet object.
  GET PROPERTY OF hworkbook 'ActiveSheet' = hsheet.
ENDFORM.                    "Start_Excel
*&---------------------------------------------------------------------*
*&      Form  Build_Header_Line
*&---------------------------------------------------------------------*
FORM build_header_line USING p_row_cnt.
  DATA: l_range(30).
  DATA: row_start(10).

  PERFORM fill_the_cell USING p_row_cnt 1 1 'Material'.
  PERFORM fill_the_cell USING p_row_cnt 2 1 'Suggested Price'.
  PERFORM fill_the_cell USING p_row_cnt 3 1 'Cost'.
  PERFORM fill_the_cell USING p_row_cnt 4 1 'Comments'.
  PERFORM format_column USING 1 15 xlcenter ' ' xlcenter 0.
  PERFORM format_column USING 2 10 xlcenter ' ' xlcenter 1.
  PERFORM format_column USING 3 35 xlcenter ' ' xlcenter 0.
  IF wraptext = 'X'.
    PERFORM format_column USING 4 35 xlleft ' ' xlcenter 1.
  ELSE.
    PERFORM format_column USING 4 100 xlleft ' ' xlcenter 0.
  ENDIF.

*Build the range object.
  row_start = p_row_cnt.
  CONCATENATE 'A' row_start ':D' row_start INTO l_range.
  CONDENSE l_range NO-GAPS.

*Set row color to yellow.
  CALL METHOD OF
      hexcel
      'RANGE' = hrange
    EXPORTING
      #1      = l_range.
  CALL METHOD OF
      hrange
      'Interior' = hinterior.
  SET PROPERTY OF hinterior 'ColorIndex' = 15. "6. "yellow
  p_row_cnt = p_row_cnt + 1.
ENDFORM. " Build_Header_Line
*&---------------------------------------------------------------------*
*&      Form  Format_Column
*&---------------------------------------------------------------------*
FORM format_column USING  p_colnum
                          p_colwidth
                          p_colhalign
                          p_colformat
                          p_colvalign
                          p_wraptext.
  CALL METHOD OF
      hexcel
      'COLUMNS' = hcolumn
    EXPORTING
      #1        = p_colnum. "column number
  SET PROPERTY OF hcolumn 'HorizontalAlignment' = p_colhalign.
  SET PROPERTY OF hcolumn 'VerticalAlignment' = p_colvalign.
  SET PROPERTY OF hcolumn 'ColumnWidth' = p_colwidth.
  SET PROPERTY OF hcolumn 'WrapText' = p_wraptext.
ENDFORM. " Format_Column
*&---------------------------------------------------------------------*
*&      Form  Pass_Records
*&---------------------------------------------------------------------*
FORM pass_records USING p_row_cnt.
  DATA: col_cnt TYPE i.
  DATA: l_range(30).
  DATA: row_start(10).
  col_cnt = 1.
*Pass the internal table values to the spreadsheet.
  LOOP AT i_excel INTO r_excel.
    DO 4 TIMES.
      ASSIGN COMPONENT sy-index OF STRUCTURE r_excel TO <val>.
      PERFORM fill_the_cell USING p_row_cnt col_cnt 0 <val>.
      col_cnt = col_cnt + 1. "increment column
    ENDDO.

*Build the range object.
    row_start = p_row_cnt.
    CONCATENATE 'A' row_start ':D' row_start INTO l_range.
    CONDENSE l_range NO-GAPS.

*Set row color to yellow.
    CALL METHOD OF
        hexcel
        'RANGE' = hrange
      EXPORTING
        #1      = l_range.
    CALL METHOD OF
        hrange
        'Interior' = hinterior.
*    SET PROPERTY OF hinterior 'ColorIndex' = 2. "7. "yellow
    p_row_cnt = p_row_cnt + 1. "increment row
    col_cnt = 1. "reset column to A (ie. col 1)
  ENDLOOP.
ENDFORM.                    "Pass_Records
*&---------------------------------------------------------------------*
*&      Form  Release_Excel
*&---------------------------------------------------------------------*
FORM release_excel .
  SET PROPERTY OF hexcel 'Visible' = 1.
  FREE OBJECT hexcel.
  PERFORM err_hdl.
ENDFORM. " Release_Excel
*---------------------------------------------------------------------
*FORM Fill_The_Cell
*---------------------------------------------------------------------
*
*Sets cell at coordinates i,j to value val boldtype bold *
*
*BOLD --> 1 = true, set bold ON 0 = false, set bold OFF
*---------------------------------------------------------------------
FORM fill_the_cell USING i j bold thevalue.
  CALL METHOD OF
      hexcel
      'Cells' = hcell
    EXPORTING
      #1      = i
      #2      = j.
  PERFORM err_hdl.
  SET PROPERTY OF hcell 'Value' = thevalue.
  PERFORM err_hdl.
  GET PROPERTY OF hcell 'Font' = hfont.
  PERFORM err_hdl.
  SET PROPERTY OF hfont 'Bold' = bold.
  PERFORM err_hdl.
ENDFORM.                    "Fill_The_Cell
*&---------------------------------------------------------------------*
*&      Form  ERR_HDL
*&---------------------------------------------------------------------*
FORM err_hdl.
  IF sy-subrc <> 0.
    MESSAGE i000(zz) WITH 'OLE Automation error: ' sy-subrc.
    EXIT.
  ENDIF.
ENDFORM. " ERR_HDL

Using Memory ID To Transfer Value

With the help of MEMORY ID we can transfer the value from one report to another. Following is a simple example to show how this works:

Source System:
*&---------------------------------------------------------------------*
*& Report  ZSOURCE
*&
*&---------------------------------------------------------------------*
*&
*& SOURCE SYSTEM: LISTING THE PLANT AGAINST A PARTICULAR EBELN
*&---------------------------------------------------------------------*

REPORT zsource.
TABLES: ekko, ekpo.

SELECT-OPTIONS: s_ebeln FOR ekko-ebeln.

START-OF-SELECTION.
  DATA: lt_ekko TYPE TABLE OF ekko,
        lt_res TYPE TABLE OF ekpo,
        ls_res TYPE ekpo
        .
  CLEAR: ls_res.
  REFRESH: lt_ekko, lt_res.
  FREE MEMORY ID 'TEMP'.

  SUBMIT zdest WITH s_ebeln IN s_ebeln AND RETURN.

  IMPORT lt_ekpo TO lt_res FROM MEMORY ID 'TEMP'.

  LOOP AT lt_res INTO ls_res.
    AT NEW ebeln.
      FORMAT INTENSIFIED ON.
      WRITE: / 'Plant: ',ls_res-ebeln.
      FORMAT INTENSIFIED OFF.
    ENDAT.

    WRITE: / ls_res-werks.
  ENDLOOP.

Destination System:
*&---------------------------------------------------------------------*
*& Report  ZDEST
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT zdest.
TABLES: ekko, ekpo.

SELECTION-SCREEN: BEGIN OF BLOCK b1.
SELECT-OPTIONS: s_ebeln FOR ekko-ebeln.
SELECTION-SCREEN: END OF BLOCK b1.

START-OF-SELECTION.

  DATA: lt_ekpo TYPE TABLE OF ekpo.

  SELECT *
  FROM ekpo
  INTO TABLE lt_ekpo
  WHERE ebeln IN s_ebeln.

  EXPORT lt_ekpo TO MEMORY ID 'TEMP'.

Thursday, 13 April 2017

Background Job Processing

Following is a simple example to show how to perform background job processing:

*&---------------------------------------------------------------------*
*& Report  ZTEST
*&
*&---------------------------------------------------------------------*
*& Background Job Processing
*&---------------------------------------------------------------------*

REPORT ztest.

SELECTION-SCREEN: BEGIN OF BLOCK b1 WITH FRAME.
PARAMETERS: p_for RADIOBUTTON GROUP gr1 DEFAULT 'X' USER-COMMAND f1,
            p_back RADIOBUTTON GROUP gr1.
PARAMETERS: p_name TYPE char10.
SELECTION-SCREEN: END OF BLOCK b1.

START-OF-SELECTION.
  IF p_for = 'X'.
    PERFORM disp USING p_name.
  ELSE.
    PERFORM batch_job USING p_name.
  ENDIF.

*&---------------------------------------------------------------------*
*&      Form  batch_job
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM batch_job USING p_name TYPE char10.
  DATA: lv_jobcount TYPE tbtcjob-jobcount,
        lv_jobname  TYPE tbtcjob-jobname VALUE 'ZTEST',
        lv_text     TYPE char200.

  DATA: lv_timestamp TYPE timestampl ##needed,
        lv_time      TYPE char50.

  CONSTANTS: lc_jobclass TYPE tbtcjob-jobclass VALUE 'A',
             lc_sep2     TYPE char3            VALUE  '_'.


  GET TIME STAMP FIELD lv_timestamp.
  lv_time = sy-uzeit.

  CONCATENATE lv_jobname lc_sep2 sy-datum lc_sep2 lv_time
         INTO lv_jobname .


* JOB OPEN
  CALL FUNCTION 'JOB_OPEN'
    EXPORTING
      jobname          = lv_jobname
      jobclass         = lc_jobclass
    IMPORTING
      jobcount         = lv_jobcount
    EXCEPTIONS
      cant_create_job  = 1
      invalid_job_data = 2
      jobname_missing  = 3
      OTHERS           = 4.
  IF sy-subrc <> 0.
    MESSAGE 'Error' TYPE 'S' DISPLAY LIKE 'E'.
    RETURN.
  ELSE.
*    CALLING THE SAME PROGRAM BY PASSING THE INPUT PARAMTERS
    SUBMIT ztest
    WITH p_name EQ p_name
    VIA JOB lv_jobname
    NUMBER lv_jobcount
    AND RETURN.

    IF sy-subrc = 0.
* JOB CLOSE
      CALL FUNCTION 'JOB_CLOSE'
        EXPORTING
          jobcount             = lv_jobcount
          jobname              = lv_jobname
          strtimmed            = 'X'
        EXCEPTIONS
          cant_start_immediate = 1
          invalid_startdate    = 2
          jobname_missing      = 3
          job_close_failed     = 4
          job_nosteps          = 5
          job_notex            = 6
          lock_failed          = 7
          invalid_target       = 8
          OTHERS               = 9.
      IF sy-subrc = 0.
        MESSAGE 'Success' TYPE 'S'.
      ELSE.
        MESSAGE 'Error' TYPE 'S' DISPLAY LIKE 'E'.
        RETURN.
      ENDIF.
    ENDIF.
  ENDIF.
ENDFORM.                    "batch_job

*&---------------------------------------------------------------------*
*&      Form  DISP
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM disp USING p_name TYPE char10.
  WRITE: 'Hello', p_name.
ENDFORM.                    " DISP

NOTE: You can view the background job in SM37. Click on execute and then you can view the spool.

Wednesday, 29 March 2017

Insert and Update of a Table

Scenario: Provide two radio button - Insert and Update in the selection screen. On selecting the insert button, Data will be inserted in the table and on selecting the update the data will be updated.

Expected Output:
When Insert is selected the New Employee ID should come automatically & it should be non editable

When Update is selected, the employee ID that needs to be updated will be displayed

On entering the Employee ID, the records has to be displayed in the selection screen for editing

Solution:
*&---------------------------------------------------------------------*
*&
*& INSERT AND UPDATE IN TABLE ZEMP
*&---------------------------------------------------------------------*

REPORT zdemo.
TABLES: zemp.

DATA: gs_zemp TYPE zemp,
      gv_last_id TYPE zdt_id.

SELECTION-SCREEN: BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.  "RADIO BUTTON FOR - INSERT OR UPDATE
PARAMETERS: rb_in RADIOBUTTON GROUP rb1 USER-COMMAND c1 DEFAULT 'X' MODIF ID b1,
            rb_up RADIOBUTTON GROUP rb1 MODIF ID b1.
SELECTION-SCREEN: END OF BLOCK b1.

SELECTION-SCREEN: BEGIN OF BLOCK b2 WITH FRAME TITLE text-002. "INSERT RECORD BLOCK
PARAMETERS: p_id    TYPE zdt_id MODIF ID b2i,
            p_name  TYPE zemp_n MODIF ID b2,
            p_des   TYPE zemp_d MODIF ID b2,
            p_yr    TYPE zemp_y MODIF ID b2,
            p_ph    TYPE zemp_p MODIF ID b2.
SELECTION-SCREEN: END OF BLOCK b2.

SELECTION-SCREEN: BEGIN OF BLOCK b3 WITH FRAME TITLE text-003. "UPDATE BLOCK: EMP ID
PARAMETERS: p_u_id TYPE zdt_id MODIF ID b3.
SELECTION-SCREEN: END OF BLOCK b3.

SELECTION-SCREEN: BEGIN OF BLOCK b4 WITH FRAME TITLE text-004. "UPDATE BLOCK
PARAMETERS: p_u_name  TYPE zemp_n MODIF ID b4,
            p_u_des   TYPE zemp_d MODIF ID b4,
            p_u_yr    TYPE zemp_y MODIF ID b4,
            p_u_ph    TYPE zemp_p MODIF ID b4.
SELECTION-SCREEN: END OF BLOCK b4.

INITIALIZATION.
  SELECT MAX( emp_id ) FROM zemp INTO gv_last_id .  "GETTING THE LAST EMPID FOR GENERATING THE NEW EMPID
  IF sy-subrc EQ 0.
    p_id = gv_last_id + 1.
  ELSE.
*    IMPLEMENT SUITABLE HANDLER
  ENDIF.

AT SELECTION-SCREEN OUTPUT.
  IF rb_in EQ 'X' .    "IF INSERT IS SELECTED
    CLEAR: p_u_id.
    LOOP AT SCREEN.
      IF screen-group1 = 'B3' OR screen-group1 = 'B4'.
        screen-active = 0.
      ELSEIF screen-group1 = 'B2I'.
        screen-input = 0.
      ELSE.
        screen-active = 1.
      ENDIF.
      MODIFY SCREEN.
    ENDLOOP.

  ELSEIF rb_up EQ 'X' AND p_u_id IS INITIAL.
    LOOP AT SCREEN.
      IF screen-group1 = 'B2' OR screen-group1 = 'B2I' OR screen-group1 = 'B4'.
        screen-active = 0.
      ELSE.
        screen-active = 1.
      ENDIF.
      MODIFY SCREEN.
    ENDLOOP.

  ELSEIF rb_up EQ 'X' AND p_u_id IS NOT INITIAL.
    SELECT SINGLE *
    FROM zemp
    INTO gs_zemp
    WHERE emp_id = p_u_id.

    IF sy-subrc EQ 0.
      LOOP AT SCREEN.
        IF screen-group1 = 'B2' OR screen-group1 = 'B2I'.
          screen-active = 0.
        ELSE.
          screen-active = 1.
        ENDIF.
        MODIFY SCREEN.
      ENDLOOP.

      p_u_name = gs_zemp-emp_name.
      p_u_des = gs_zemp-designation.
      p_u_yr = gs_zemp-yr_of_exp.
      p_u_ph = gs_zemp-phone_no.
    ELSE.
      MESSAGE 'Invalid ID' TYPE 'S' DISPLAY LIKE 'E'.
      LEAVE LIST-PROCESSING.
    ENDIF.
  ENDIF.



START-OF-SELECTION.

  IF rb_up EQ 'X'.    "UPDATE
    gs_zemp-emp_name = p_u_name.
    gs_zemp-designation = p_u_des.
    gs_zemp-yr_of_exp = p_u_yr.
    gs_zemp-phone_no = p_u_ph.

    UPDATE zemp FROM gs_zemp .
    IF sy-subrc EQ 0.
      MESSAGE 'Record Updated' TYPE 'S'.
    ELSE.
      MESSAGE 'Record Not Updated' TYPE 'E'.
    ENDIF.
  ELSE.                "INSERT
    CLEAR: gs_zemp.
    gs_zemp-mandt       = sy-mandt. "'115'.
    gs_zemp-emp_id      = p_id.
    gs_zemp-emp_name    = p_name.
    gs_zemp-designation = p_des.
    gs_zemp-yr_of_exp   = p_yr.
    gs_zemp-phone_no    = p_ph.

    INSERT zemp FROM gs_zemp.
    IF sy-subrc EQ 0.
      MESSAGE 'Record Inserted' TYPE 'S'.
    ELSE.
      MESSAGE 'Record Not Inserted' TYPE 'E'.
    ENDIF.
    CLEAR: gs_zemp.
  ENDIF.

Thursday, 23 March 2017

Permutation of Numbers - Simple Report

Permutation of Numbers - A simple ABAP Report
INITIALIZATION.
  TYPES: BEGIN OF lty_tab,
      i_field TYPE i,
      c_field TYPE c,
      c_field2 TYPE c,
    END OF lty_tab.

  DATA: it_tab TYPE STANDARD TABLE OF lty_tab WITH HEADER LINE.

START-OF-SELECTION.
  it_tab-i_field = 1.
  it_tab-c_field = 'A'.
  it_tab-c_field2 = 'X'.
  APPEND it_tab.
  it_tab-i_field = 2.
  it_tab-c_field = 'B'.
  it_tab-c_field2 = 'Y'.
  APPEND it_tab.
  it_tab-i_field = 3.
  it_tab-c_field = 'C'.
  it_tab-c_field2 = 'Z'.
  APPEND it_tab.
  PERFORM permutation TABLES it_tab.
*&---------------------------------------------------------------------*
*&      Form  Permutation
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM permutation TABLES f_itab LIKE it_tab[].
  DATA:
        f_line LIKE LINE OF f_itab,
        f_line2 LIKE LINE OF f_itab
        .

  LOOP AT f_itab INTO f_line.
    LOOP AT f_itab INTO f_line2.
      WRITE: / f_line-i_field, f_line2-c_field, f_line2-c_field2.
    ENDLOOP.
  ENDLOOP.
ENDFORM.                    "permutation

Report to find CDS view of Standard Table

A small change has been made to the original program ( SAP YARD Article ) so that it can also display the common CDS used by multiple table...