需求:SAP期初上线的时候,业务顾问经常会遇到批量创建角色和分配角色权限的情况
岗位需求:一般是业务顾问定义权限,BASIS进行后期运维,今天讲两个批导功能,方便期初上线
此程序功能:给具体用户分配多角色
代码如下
bash
*&---------------------------------------------------------------------*
*& 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_control
IMPORTING
control = go_control
error = go_error.
IF go_error->has_failed = abap_true.
CALL METHOD go_error->raise_message
EXPORTING
type = 'E'.
ENDIF.
CREATE OBJECT go_container
EXPORTING
container_name = 'CONT'
EXCEPTIONS
cntl_error = 1
cntl_system_error = 2
create_error = 3
lifetime_error = 4
lifetime_dynpro_dynpro_link = 5
OTHERS = 6.
IF sy-subrc <> 0.
MESSAGE 'Error while creating container' TYPE 'E'.
ENDIF.
CALL METHOD go_control->init_control
EXPORTING
inplace_enabled = abap_true
r3_application_name = 'EXCEL CONTAINER'
parent = go_container
IMPORTING
error = go_error
EXCEPTIONS
javabeannotsupported = 1
OTHERS = 2.
IF go_error->has_failed = abap_true.
CALL METHOD go_error->raise_message
EXPORTING
type = 'E'.
ENDIF.
CALL METHOD go_control->get_document_proxy
EXPORTING
document_type = soi_doctype_excel_sheet
IMPORTING
document_proxy = go_document
error = go_error.
IF go_error->has_failed = abap_true.
CALL METHOD go_error->raise_message
EXPORTING
type = '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_exist
EXPORTING
file = p_file
RECEIVING
result = lv_ret
EXCEPTIONS
cntl_error = 1
error_no_gui = 2
wrong_parameter = 3
not_supported_by_gui = 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.
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_dialog
EXPORTING
window_title = '导入文件'
default_extension = '*.xls'
file_filter = cl_gui_frontend_services=>filetype_excel
CHANGING
file_table = lt_filetab
rc = lv_rc
EXCEPTIONS
file_open_dialog_failed = 1
cntl_error = 2
error_no_gui = 3
not_supported_by_gui = 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.
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 = progress
text = TEXT-t03.
" open document
CONCATENATE 'FILE://' p_file INTO lv_document_url.
CALL METHOD go_document->open_document
EXPORTING
document_title = TEXT-t05
document_url = lv_document_url
open_inplace = abap_true
IMPORTING
error = go_error.
IF go_error->has_failed = abap_true.
CALL METHOD go_error->raise_message
EXPORTING
type = 'I'.
LEAVE LIST-PROCESSING.
ENDIF.
" get sheet interface
CALL METHOD go_document->get_spreadsheet_interface
EXPORTING
no_flush = ' '
IMPORTING
error = go_error
sheet_interface = go_spreadsheet.
IF go_error->has_failed = abap_true.
CALL METHOD go_error->raise_message
EXPORTING
type = '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 content
CALL METHOD go_spreadsheet->select_sheet
EXPORTING
name = lv_sheet_name "lstr_sheets-sheet_name
IMPORTING
error = go_error.
IF go_error->has_failed = abap_true.
CALL METHOD go_error->raise_message
EXPORTING
type = 'I'.
LEAVE LIST-PROCESSING.
ENDIF.
CALL METHOD go_spreadsheet->set_selection
EXPORTING
top = cns_top
left = cns_left
rows = p_rec
columns = cns_col.
CALL METHOD go_spreadsheet->insert_range
EXPORTING
name = TEXT-t04
rows = p_rec
columns = cns_col
no_flush = ''
IMPORTING
error = go_error.
IF go_error->has_failed = abap_true.
CALL METHOD go_error->raise_message
EXPORTING
type = 'I'.
LEAVE LIST-PROCESSING.
ENDIF.
REFRESH lt_data.
CALL METHOD go_spreadsheet->get_ranges_data
EXPORTING
rangesdef = lt_range
IMPORTING
contents = lt_data
error = go_error
CHANGING
ranges = 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 combination
PERFORM 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_list
SORT 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_TRUE
replacement = '#'
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'
TABLES
data_tab = gt_list
* FIELDNAMES =
EXCEPTIONS
file_write_error = 1
no_batch = 2
gui_refuse_filetransfer = 3
invalid_type = 4
no_authority = 5
unknown_error = 6
header_not_allowed = 7
separator_not_allowed = 8
filesize_not_allowed = 9
header_too_long = 10
dp_error_create = 11
dp_error_send = 12
dp_error_write = 13
unknown_dp_error = 14
access_denied = 15
dp_out_of_memory = 16
disk_full = 17
dp_timeout = 18
file_not_found = 19
dataprovider_exception = 20
control_flush_error = 21
OTHERS = 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_item
u_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 ROWS
INTO @ls_agr-agr_text
FROM agr_texts
WHERE 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'
EXPORTING
username = lv_name
TABLES
actgroups_new = lt_agr
return = lt_ret.
LOOP AT lt_ret INTO DATA(lwa_return).
IF lwa_return-type = 'E' OR
lwa_return-type = 'A' OR
lwa_return-type = 'X'.
DATA(lv_flg) = abap_true.
EXIT.
ENDIF.
ENDLOOP.
IF lv_flg IS INITIAL.
* 成功
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
wait = abap_true.
ELSE.
* 失败
CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
CLEAR lv_str.
LOOP AT lt_ret INTO lwa_return
WHERE 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 msg
WHERE uname NE ''.
APPEND LINES OF lt_list TO gt_list.
ENDIF.
CLEAR:lt_agr,lt_ret,lt_list,lv_name.
ENDAT.
ENDLOOP.
ENDFORM.