Monday, November 28, 2016

Convert internal table to CSV

report zjso_itab_to_csv.

types gtt_string type standard table of string with empty key.

class lcl_conv definition.
  public section.
    class-methods conv importing it_input type any table returning value(et_csvtype gtt_string.
    class-methods struct_to_csv_row importing is_row type any returning value(rv_csvtype string.
    class-methods struct_shorttext_to_csv_row importing is_row type any returning value(rv_csvtype string.
    class-methods convert_any_to_string importing iv_any type any returning value(rv_stringtype string.
    class-methods esc importing iv_string type string returning value(rv_stringtype string.
endclass.

start-of-selection.
  select from t001w into table @data(gt_t001w).
  cl_demo_output=>display_datalcl_conv=>convgt_t001w ).

class lcl_conv implementation.
  method conv.
    field-symbols <ls_row> type any.

    loop at it_input assigning <ls_row>.
      if sy-tabix 1.
        insert struct_shorttext_to_csv_row<ls_row> into table et_csv.
      endif.
      insert struct_to_csv_row<ls_row> into table et_csv.
    endloop.
  endmethod.


  method struct_to_csv_row.
    data lv_component_count type i.
    data lv_datatype type c.                                "#EC NEEDED
    data lv_i type i.
    data lv_conv_string type string.
    field-symbols <lv_component> type any.

    clear rv_csv.
    describe field is_row type lv_datatype components lv_component_count.
    lv_i 1.
    do lv_component_count times.
      assign component lv_i of structure is_row to <lv_component>.
      lv_conv_string convert_any_to_string<lv_component> ).
      if lv_i 1.
        rv_csv lv_conv_string.
      else.
        rv_csv |{ rv_csv };{ lv_conv_string }|.
      endif.

      add to lv_i.
    enddo.
  endmethod.


  method struct_shorttext_to_csv_row.
    data lo_sdesc type ref to cl_abap_structdescr.
    data lt_ddic_field type ddfields.
    data ls_ddic_field type dfies.
    data lv_tabix type sytabix.
    data lv_shorttext type string.

    clear rv_csv.
    lo_sdesc cast #cl_abap_structdescr=>describe_by_datais_row ).
    lt_ddic_field lo_sdesc->get_ddic_field_list).
    loop at lt_ddic_field into ls_ddic_field.
      lv_tabix sy-tabix.
      lv_shorttext escconv #ls_ddic_field-fieldtext ).
      if lv_tabix 1.
        rv_csv lv_shorttext.
      else.
        rv_csv |{ rv_csv };{ lv_shorttext }|.
      endif.
    endloop.
  endmethod.


  method convert_any_to_string.
    data lv_type type c.
    data lv_d type d.
    data lv_t type t.

    clear rv_string.
    describe field iv_any type lv_type.

    case lv_type.
      when 'D'" Date
        lv_d iv_any.
        rv_string |{ lv_d date user }|.
      when 'T'" time
        lv_t iv_any.
        rv_string |{ lv_t time user }|.
      when others.
        rv_string iv_any.
    endcase.

    rv_string escrv_string ).
  endmethod.


  method esc.
    if iv_string cs ';'.
      rv_string |'{ rv_string }'|.
    else.
      rv_string iv_string.
    endif.
  endmethod.

endclass.

SAP ABAP: Determine Timezone for Plant

    DATA:       lt_tzone TYPE STANDARD TABLE OF tznzone WITH DEFAULT KEY,       l_tzone  TYPE tznzone.     " get time zone for plant   ...