需求:SAP期初上线的时候,业务顾问经常会遇到批量创建角色和分配角色权限的情况
岗位需求:一般是业务顾问定义权限,BASIS进行后期运维,今天讲两个批导功能,方便期初上线
此程序功能:给具体用户分配多角色
代码如下
*&---------------------------------------------------------------------*
*& Report Y_YZD_TEST11
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*REPORT Y_YZD_TEST11.TABLES: sscrfields.TYPES: BEGIN OF ty_list,uname TYPE usnam,pname TYPE agr_name,msg(1024),END OF ty_list.DATA: gt_list TYPE STANDARD TABLE OF ty_list,gt_usrs TYPE STANDARD TABLE OF ty_list.
* OLE使用变量
DATA: gv_excel TYPE ole2_object,gv_application TYPE ole2_object,gv_wbook TYPE ole2_object,gv_sheet TYPE ole2_object,go_error TYPE REF TO i_oi_error,go_document TYPE REF TO i_oi_document_proxy,go_container TYPE REF TO cl_gui_custom_container,go_control TYPE REF TO i_oi_container_control,go_spreadsheet TYPE REF TO i_oi_spreadsheet.*----------------------------------------------------------------------*
* CONSTANTS
*----------------------------------------------------------------------*
CONSTANTS: cns_left TYPE i VALUE 1,cns_top TYPE i VALUE 2,cns_col TYPE i VALUE 2.*----------------------------------------------------------------------*
* SELECTION-SCREEN
*----------------------------------------------------------------------*
* 选择屏幕
SELECTION-SCREEN BEGIN OF BLOCK bk1 WITH FRAME TITLE TEXT-t01.
PARAMETERS: p_file TYPE string OBLIGATORY.
SELECTION-SCREEN FUNCTION KEY 1.
PARAMETERS:p_rec TYPE i OBLIGATORY.
SELECTION-SCREEN END OF BLOCK bk1.*----------------------------------------------------------------------*
* INITIALIZATION
*----------------------------------------------------------------------*
INITIALIZATION.
* 初始化对象PERFORM frm_init_prog.*----------------------------------------------------------------------*
* AT SELECTION-SCREEN
*----------------------------------------------------------------------*
AT SELECTION-SCREEN.
* 运行前的相关检查PERFORM frm_check_ref.*----------------------------------------------------------------------*
* AT SELECTION-SCREEN
*----------------------------------------------------------------------*
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
* 得到文件路径PERFORM frm_get_file_path.*&---------------------------------------------------------------------*
* START-OF-SELECTION
*&---------------------------------------------------------------------*
START-OF-SELECTION.
* 处理导入数据PERFORM frm_upload_sheets .*&---------------------------------------------------------------------*
*& Form FRM_INIT_PROG
*&---------------------------------------------------------------------*
* text:初始化OLE对象
*----------------------------------------------------------------------*
FORM frm_init_prog .CALL METHOD c_oi_container_control_creator=>get_container_controlIMPORTINGcontrol = go_controlerror = go_error.IF go_error->has_failed = abap_true.CALL METHOD go_error->raise_messageEXPORTINGtype = 'E'.ENDIF.CREATE OBJECT go_containerEXPORTINGcontainer_name = 'CONT'EXCEPTIONScntl_error = 1cntl_system_error = 2create_error = 3lifetime_error = 4lifetime_dynpro_dynpro_link = 5OTHERS = 6.IF sy-subrc <> 0.MESSAGE 'Error while creating container' TYPE 'E'.ENDIF.CALL METHOD go_control->init_controlEXPORTINGinplace_enabled = abap_truer3_application_name = 'EXCEL CONTAINER'parent = go_containerIMPORTINGerror = go_errorEXCEPTIONSjavabeannotsupported = 1OTHERS = 2.IF go_error->has_failed = abap_true.CALL METHOD go_error->raise_messageEXPORTINGtype = 'E'.ENDIF.CALL METHOD go_control->get_document_proxyEXPORTINGdocument_type = soi_doctype_excel_sheetIMPORTINGdocument_proxy = go_documenterror = go_error.IF go_error->has_failed = abap_true.CALL METHOD go_error->raise_messageEXPORTINGtype = 'E'.ENDIF.ENDFORM. " FRM_INIT_PROG*&---------------------------------------------------------------------*
*& Form FRM_CHECK_REF
*&---------------------------------------------------------------------*
* text:检查文件是否存在
*----------------------------------------------------------------------*
FORM frm_check_ref .DATA lv_ret TYPE abap_bool.* 将路径全部转换为大写TRANSLATE p_file TO UPPER CASE.IF p_file IS NOT INITIAL.
* 检查文件是否存在CALL METHOD cl_gui_frontend_services=>file_existEXPORTINGfile = p_fileRECEIVINGresult = lv_retEXCEPTIONScntl_error = 1error_no_gui = 2wrong_parameter = 3not_supported_by_gui = 4OTHERS = 5.IF sy-subrc <> 0.MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgnoWITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.ENDIF.IF lv_ret NE abap_true.MESSAGE TEXT-t02 TYPE 'E'.ENDIF.ENDIF.ENDFORM. " FRM_CHECK_INPUT*&---------------------------------------------------------------------*
*& Form FRM_GET_FILE_PATH
*&---------------------------------------------------------------------*
* text:取得文件路径
*----------------------------------------------------------------------*
FORM frm_get_file_path .DATA: lt_filetab TYPE filetable.DATA: lv_rc TYPE i.* 取得文件路径CALL METHOD cl_gui_frontend_services=>file_open_dialogEXPORTINGwindow_title = '导入文件'default_extension = '*.xls'file_filter = cl_gui_frontend_services=>filetype_excelCHANGINGfile_table = lt_filetabrc = lv_rcEXCEPTIONSfile_open_dialog_failed = 1cntl_error = 2error_no_gui = 3not_supported_by_gui = 4OTHERS = 5.IF sy-subrc <> 0.MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgnoWITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.ENDIF.IF lt_filetab IS NOT INITIAL.READ TABLE lt_filetab INTO p_file INDEX 1.ENDIF.ENDFORM. " FRM_GET_FILE_PATH*&---------------------------------------------------------------------*
*& Form FRM_UPLOAD_SHEETS
*&---------------------------------------------------------------------*
* text:上传Excel内容
*----------------------------------------------------------------------*
FORM frm_upload_sheets .DATA: lwa_list TYPE ty_list,lv_flag,lv_document_url TYPE c LENGTH 256,lv_sheet_name TYPE soi_field_name,lt_data TYPE soi_generic_table,lt_ranges TYPE soi_range_list,lt_range TYPE soi_dimension_table,ls_range TYPE soi_dimension_item,lv_str TYPE string.lv_sheet_name = 'Assets'.CALL FUNCTION 'SAPGUI_PROGRESS_INDICATOR'EXPORTING
* percentage = progresstext = TEXT-t03." open documentCONCATENATE 'FILE://' p_file INTO lv_document_url.CALL METHOD go_document->open_documentEXPORTINGdocument_title = TEXT-t05document_url = lv_document_urlopen_inplace = abap_trueIMPORTINGerror = go_error.IF go_error->has_failed = abap_true.CALL METHOD go_error->raise_messageEXPORTINGtype = 'I'.LEAVE LIST-PROCESSING.ENDIF." get sheet interfaceCALL METHOD go_document->get_spreadsheet_interfaceEXPORTINGno_flush = ' 'IMPORTINGerror = go_errorsheet_interface = go_spreadsheet.IF go_error->has_failed = abap_true.CALL METHOD go_error->raise_messageEXPORTINGtype = 'I'.LEAVE LIST-PROCESSING.ENDIF.ls_range-row = cns_top.ls_range-column = cns_left.ls_range-rows = p_rec.ls_range-columns = cns_col.APPEND ls_range TO lt_range." get sheet contentCALL METHOD go_spreadsheet->select_sheetEXPORTINGname = lv_sheet_name "lstr_sheets-sheet_nameIMPORTINGerror = go_error.IF go_error->has_failed = abap_true.CALL METHOD go_error->raise_messageEXPORTINGtype = 'I'.LEAVE LIST-PROCESSING.ENDIF.CALL METHOD go_spreadsheet->set_selectionEXPORTINGtop = cns_topleft = cns_leftrows = p_reccolumns = cns_col.CALL METHOD go_spreadsheet->insert_rangeEXPORTINGname = TEXT-t04rows = p_reccolumns = cns_colno_flush = ''IMPORTINGerror = go_error.IF go_error->has_failed = abap_true.CALL METHOD go_error->raise_messageEXPORTINGtype = 'I'.LEAVE LIST-PROCESSING.ENDIF.REFRESH lt_data.CALL METHOD go_spreadsheet->get_ranges_dataEXPORTINGrangesdef = lt_rangeIMPORTINGcontents = lt_dataerror = go_errorCHANGINGranges = lt_ranges.DELETE lt_data WHERE value IS INITIAL OR value = space.LOOP AT lt_data ASSIGNING FIELD-SYMBOL(<lfs_data>)." build records by selected combinationPERFORM frm_build_records_by_comb USING <lfs_data> lwa_list.AT END OF row.CLEAR lv_flag.TRANSLATE lwa_list-uname TO UPPER CASE.TRANSLATE lwa_list-pname TO UPPER CASE.* 检查用户是否存在SELECT SINGLE bname INTO @DATA(lv_bname)FROM usr01 WHERE bname = @lwa_list-uname.IF sy-subrc EQ 0.ELSE.lwa_list-msg = TEXT-t05.lv_flag = abap_true.ENDIF.* 检查角色是否存在SELECT SINGLE agr_name INTO @DATA(lv_agr)FROM agr_define WHERE agr_name = @lwa_list-pname.IF sy-subrc EQ 0.ELSE.lwa_list-msg = lwa_list-msg && '|' && TEXT-t08.lv_flag = abap_true.ENDIF.IF lv_flag IS INITIAL.APPEND lwa_list TO gt_usrs.ELSE.SHIFT lwa_list-msg LEFT DELETING LEADING '|'.APPEND lwa_list TO gt_list.ENDIF.CLEAR:lwa_list.ENDAT.ENDLOOP.* 出错的用户,直接从gt_usrs里面移到gt_listSORT gt_list BY uname.LOOP AT gt_usrs INTO lwa_list.READ TABLE gt_list ASSIGNING FIELD-SYMBOL(<lfs_list>)WITH KEY uname = lwa_list-uname BINARY SEARCH.CHECK sy-subrc EQ 0.APPEND lwa_list TO gt_list.SORT gt_list BY uname.DELETE gt_usrs.ENDLOOP.IF gt_usrs IS NOT INITIAL.
* 执行修改PERFORM frm_exec_change.ENDIF.* 如果出现检查或上载错误,则将错误的下载下来SORT gt_list BY uname pname.IF gt_list IS NOT INITIAL.lwa_list-uname = 'SAP用户'.lwa_list-pname = '角色'.lwa_list-msg = '错误消息'.INSERT lwa_list INTO gt_list INDEX 1.CALL FUNCTION 'GUI_DOWNLOAD'EXPORTING
* BIN_FILESIZE =filename = 'C:\code\role_error.xls'filetype = 'DAT'
* APPEND = ' 'write_field_separator = 'X'
* HEADER = '00'
* TRUNC_TRAILING_BLANKS = ' '
* WRITE_LF = 'X'
* COL_SELECT = ' '
* COL_SELECT_MASK = ' '
* DAT_MODE = ' '
* CONFIRM_OVERWRITE = ' '
* NO_AUTH_CHECK = ' 'codepage = '4103'
* IGNORE_CERR = ABAP_TRUEreplacement = '#'write_bom = 'X'
* TRUNC_TRAILING_BLANKS_EOL = 'X'
* WK1_N_FORMAT = ' '
* WK1_N_SIZE = ' '
* WK1_T_FORMAT = ' '
* WK1_T_SIZE = ' '
* WRITE_LF_AFTER_LAST_LINE = ABAP_TRUE
* SHOW_TRANSFER_STATUS = ABAP_TRUE
* VIRUS_SCAN_PROFILE = '/SCET/GUI_DOWNLOAD'TABLESdata_tab = gt_list
* FIELDNAMES =EXCEPTIONSfile_write_error = 1no_batch = 2gui_refuse_filetransfer = 3invalid_type = 4no_authority = 5unknown_error = 6header_not_allowed = 7separator_not_allowed = 8filesize_not_allowed = 9header_too_long = 10dp_error_create = 11dp_error_send = 12dp_error_write = 13unknown_dp_error = 14access_denied = 15dp_out_of_memory = 16disk_full = 17dp_timeout = 18file_not_found = 19dataprovider_exception = 20control_flush_error = 21OTHERS = 22.WRITE: TEXT-t07.ELSE.MESSAGE TEXT-t06 TYPE 'S'.ENDIF.ENDFORM. " FRM_UPLOAD_SHEETS*&---------------------------------------------------------------------*
*& Form FRM_BUILD_RECORDS_BY_COMB
*&---------------------------------------------------------------------*
* text:将列值赋值到上传内表
*----------------------------------------------------------------------*
* -->U_DATA:上传的EXCEL值
* -->U_UPLDATA:传入内表的行
*----------------------------------------------------------------------*
FORM frm_build_records_by_comb USING u_data TYPE soi_generic_itemu_list TYPE ty_list.CASE u_data-column.WHEN '1'.u_list-uname = u_data-value.WHEN '2'.u_list-pname = u_data-value.WHEN OTHERS.ENDCASE.ENDFORM.*&---------------------------------------------------------------------*
*& Form FRM_EXEC_CHANGE
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_LV_FLAG text
* -->P_LWA_LIST text
*----------------------------------------------------------------------*
FORM frm_exec_change.DATA:lt_agr TYPE TABLE OF bapiagr,ls_agr TYPE bapiagr,lt_ret TYPE TABLE OF bapiret2,lv_name TYPE bapibname-bapibname,lt_list TYPE TABLE OF ty_list,ls_list TYPE ty_list,lv_str TYPE string.SORT gt_usrs BY uname.LOOP AT gt_usrs ASSIGNING FIELD-SYMBOL(<lfs_list>).ls_agr-agr_name = <lfs_list>-pname.ls_agr-from_dat = sy-datum.ls_agr-to_dat = '99991231'.* 取角色的描述SELECT text UP TO 1 ROWSINTO @ls_agr-agr_textFROM agr_textsWHERE agr_name = @<lfs_list>-pname.ENDSELECT.APPEND ls_agr TO lt_agr.APPEND <lfs_list> TO lt_list.AT END OF uname.lv_name = <lfs_list>-uname.CALL FUNCTION 'ISAI_USER_ROLES_MAINTAIN'EXPORTINGusername = lv_nameTABLESactgroups_new = lt_agrreturn = lt_ret.LOOP AT lt_ret INTO DATA(lwa_return).IF lwa_return-type = 'E' ORlwa_return-type = 'A' ORlwa_return-type = 'X'.DATA(lv_flg) = abap_true.EXIT.ENDIF.ENDLOOP.IF lv_flg IS INITIAL.
* 成功CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'EXPORTINGwait = abap_true.ELSE.
* 失败CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.CLEAR lv_str.LOOP AT lt_ret INTO lwa_returnWHERE type = 'E' OR type = 'A' OR type = 'X'.lv_str = lv_str && '|' && lwa_return-message.ENDLOOP.SHIFT lv_str LEFT DELETING LEADING '|'.CLEAR ls_list.ls_list-msg = lv_str.MODIFY lt_list FROM ls_list TRANSPORTING msgWHERE uname NE ''.APPEND LINES OF lt_list TO gt_list.ENDIF.CLEAR:lt_agr,lt_ret,lt_list,lv_name.ENDAT.ENDLOOP.ENDFORM.