Application Development Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
I meet this requirement from time to time.

Each time I start from scratch. This time I decided to put a note here.

A pretty simple task is to convert a number, say 3 to a column, like C. Or, not less obvious, 1467 to BDK.

Here's the code for int to column name conversion (without additional checks) and back:




* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Static Public Method ZB74H_NEXUS_GSHEETS_READWRITE=>GET_COLUMN_FROM_INT
* +-------------------------------------------------------------------------------------------------+
* | [--->] IV_COLUMN_NUM                  TYPE        INT4
* | [<-()] RV_RESULT                      TYPE        STRING
* +--------------------------------------------------------------------------------------</SIGNATURE>
  METHOD get_column_from_int.
    CHECK iv_column_num > 0.
    DO.
      DATA(lv_modiv_column_num MOD 26.
      DATA(lv_diviv_column_num DIV 26.
      iv_column_num lv_div.
      rv_result sy-abcde+lv_mod(1&& rv_result.
      IF iv_column_num <= 0.
        EXIT.
      ENDIF.
    ENDDO.
  ENDMETHOD.





* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Static Public Method ZB74H_NEXUS_GSHEETS_READWRITE=>GET_INT_FROM_COLUMN
* +-------------------------------------------------------------------------------------------------+
* | [--->] IV_COLUMN                      TYPE        STRING
* | [<-()] RV_RESULT                      TYPE        INT4
* +--------------------------------------------------------------------------------------</SIGNATURE>
  METHOD get_int_from_column.
    DO.
      IF iv_column IS INITIAL.
        RETURN.
      ENDIF.
      FIND iv_column(1IN sy-abcde MATCH OFFSET DATA(lv_offset).
      rv_result rv_result * 26 + lv_offset + 1.
      iv_column iv_column+1.
    ENDDO.
  ENDMETHOD.
12 Comments