04-04-2007 9:26 AM
Hi,
Does anyone knows how to have some line cells in a diferent color in an ALV grid ?
I need if some cells have certain value to output them with red background.
If someone knows how to do it, i'll also thank information about the code of the red color. Please note that i'm using F.M. REUSE_ALV_GRID_DISPLAY.
Thanks in advance,
Paulo Sousa
04-04-2007 9:38 AM
04-04-2007 9:28 AM
go through this
<a href="http://www.sap-img.com/abap/line-color-in-alv-example.htm">http://www.sap-img.com/abap/line-color-in-alv-example.htm</a>
reagrds
shiba dutta
04-04-2007 9:32 AM
04-04-2007 9:36 AM
Paulo,
Here is some colors and fonting:
REPORT ZR_EXCEL_WITH_COLORS.
Export to Excel data defs - below
INCLUDE OLE2INCL.
handles for OLE objects
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
DATA H TYPE I.
constants: xlCenter type i value '-4108',
xlBottom type i value '-4107',
xlLeft type i value '-4131',
xlRight type i value '-4152'.
constants: xlMinimized type i value '-4140'.
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'.
start-of-selection.
Perform Start_Excel using 'c:\my_file.xls'.
Perform Build_Chain_Line.
Perform Release_Excel.
FORM Start_Excel using p_CliFile.
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_Chain_Line.
data: Chain_Name(40).
data: Year_Line(30).
data: p_row_cnt type i.
p_row_cnt = 1.
Chain_Name = 'My Chain Here'.
Select range object onto cell A1.
CALL METHOD OF hExcel 'RANGE' = hRange EXPORTING #1 = 'A1:A1'.
*Add the chain name to A2.
PERFORM Fill_The_Cell USING p_row_cnt 1 1 Chain_Name.
Set background color to tan.
call method of hRange 'Interior' = hInterior.
set property of hInterior 'ColorIndex' = 40. "tan
*Add the year to A3.
Year_Line = '2007'.
p_row_cnt = p_row_cnt + 1.
PERFORM Fill_The_Cell USING p_row_cnt 1 1 Year_Line.
Select range object onto cell A2.
CALL METHOD OF hExcel 'RANGE' = hRange EXPORTING #1 = 'A2:A2'.
Set background color to blue.
call method of hRange 'Interior' = hInterior.
set property of hInterior 'ColorIndex' = 20. "blue
Center the chain name.
set property of hRange 'HorizontalAlignment' = xlCenter.
set property of hRange 'VerticalAlignment' = xlCenter.
Change font to 14 point.
call method of hRange 'Font' = hFont.
set property of hFont 'Size' = 14.
Endform.
FORM Release_Excel .
CALL METHOD OF hExcel 'RANGE' = hRange EXPORTING #1 = 'A6'.
call method of hRange 'Select'.
Set Excel VISIBLE property to TRUE.
set property of hExcel 'WindowState' = xlMinimized.
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.
*&----
*& Form ERR_HDL
*&----
outputs OLE error if any
*----
--> p1 text
<-- p2 text
*----
FORM ERR_HDL.
IF SY-SUBRC <> 0.
message i000(zz) with 'OLE Automation error: ' SY-SUBRC.
exit.
ENDIF.
ENDFORM. " ERR_HDL
Don't forget to reward if useful...
04-04-2007 9:38 AM
04-04-2007 9:58 AM
Hi Paulo .
I had tried this usually with ABAP Objects , but the same approch works in the case of FM also.
All you need to do is in the IT which contains your data for ALV add another feild of type lvc_t_scol and once the data is populated , loop on the table and assign colors to each feild.
Once this is done assign this feild name to the feild coltab_fieldname of the layout.
Here is a sample code which doesn the same.
tables : mard.
type-pools : slis.
*--> Type Decleration
types : begin of g_ty_mard ,
matnr type matnr ,
werks type werks_d ,
labst type labst ,
row_c(4) type c,
end of g_ty_mard.
types : begin of g_ty_mard1 ,
matnr type matnr ,
werks type werks_d ,
labst type labst ,
row_c(4) type c,
row_d type lvc_t_scol ,
end of g_ty_mard1.
*--> Decleration for ALV
data : g_t_cat type slis_t_fieldcat_alv , " F Catalog
g_wa_cat type slis_fieldcat_alv , " WA F Catalog
g_t_cat_d type slis_t_fieldcat_alv , " F Catalog
g_wa_cat_d type slis_fieldcat_alv , " WA F Catalog
g_wa_layo type slis_layout_alv , " WA Layout
g_t_event type slis_t_event , " Event
g_wa_event type slis_alv_event . " WA Event
*--> Internal Table Decleration
data : g_t_mard type table of g_ty_mard .
data : g_t_mard1 type table of g_ty_mard1 .
*--. Work Area
data : g_wa_mard type g_ty_mard.
data : g_wa_mard1 type g_ty_mard1.
*--> Variable Decleration
data : g_v_repid type sy-repid.
*--> Selection Screen
select-options : s_matnr for mard-matnr ,
s_werks for mard-werks.
start-of-selection.
*--> Get the stock of material for each plant.
select matnr werks SUM( labst )
into table g_t_mard
from mard
where matnr in s_matnr and
werks in s_werks
group by matnr werks .
data : v_test1(3).
perform build_catalog.
*--> Assign the color for the row
loop at g_t_mard into g_wa_mard.
if g_wa_mard-labst > 100.
g_wa_mard-row_c = 'C500'.
modify g_t_mard from g_wa_mard index sy-tabix transporting row_c .
endif.
endloop.
*--> Specify the feild name in the layout.
g_wa_layo-info_fieldname = 'ROW_C'.
g_wa_layo-coltab_fieldname = 'ROW_D'.
g_v_repid = sy-repid.
call function 'REUSE_ALV_GRID_DISPLAY'
exporting
* I_INTERFACE_CHECK = ' '
* I_BYPASSING_BUFFER =
* I_BUFFER_ACTIVE = ' '
i_callback_program = g_v_repid
* I_CALLBACK_PF_STATUS_SET = ' '
* i_callback_user_command = 'EVENT_HANDLE'
* I_CALLBACK_TOP_OF_PAGE = ' '
* I_CALLBACK_HTML_TOP_OF_PAGE = ' '
* I_CALLBACK_HTML_END_OF_LIST = ' '
* I_STRUCTURE_NAME =
* I_BACKGROUND_ID = ' '
* I_GRID_TITLE =
* I_GRID_SETTINGS =
is_layout = g_wa_layo
it_fieldcat = g_t_cat
* IT_EXCLUDING =
* IT_SPECIAL_GROUPS =
* IT_SORT =
* IT_FILTER =
* IS_SEL_HIDE =
* I_DEFAULT = 'X'
* I_SAVE = ' '
* IS_VARIANT =
* it_events = g_t_event
* IT_EVENT_EXIT =
* IS_PRINT =
* IS_REPREP_ID =
* I_SCREEN_START_COLUMN = 0
* I_SCREEN_START_LINE = 0
* I_SCREEN_END_COLUMN = 0
* I_SCREEN_END_LINE = 0
* IT_ALV_GRAPHICS =
* IT_ADD_FIELDCAT =
* IT_HYPERLINK =
* I_HTML_HEIGHT_TOP =
* I_HTML_HEIGHT_END =
* IT_EXCEPT_QINFO =
* IMPORTING
* E_EXIT_CAUSED_BY_CALLER =
* ES_EXIT_CAUSED_BY_USER =
tables
t_outtab = g_t_mard1
* EXCEPTIONS
* PROGRAM_ERROR = 1
* OTHERS = 2
.
if sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
endif.
*&---------------------------------------------------------------------*
*& Form build_catalog
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
form build_catalog.
refresh g_t_cat.
clear g_wa_cat.
g_wa_cat-fieldname = 'MATNR'.
g_wa_cat-tabname = 'G_T_MARD'.
g_wa_cat-outputlen = '20'.
append g_wa_cat to g_t_cat.
clear g_wa_cat.
g_wa_cat-fieldname = 'WERKS'.
g_wa_cat-emphasize = 'C400'. " Set the color to the column
g_wa_cat-tabname = 'G_T_MARD'.
append g_wa_cat to g_t_cat.
clear g_wa_cat.
g_wa_cat-fieldname = 'LABST'.
g_wa_cat-tabname = 'G_T_MARD'.
append g_wa_cat to g_t_cat.
*" Assign Color to cell
data : v_temp type lvc_s_scol ,
v_tmp2 type lvc_t_scol ,
v_tmp type lvc_s_colo.
loop at g_t_mard into g_wa_mard.
MOVE-CORRESPONDING G_WA_MARD TO G_WA_MARD1.
if g_wa_mard1-labst > 0.
refresh v_tmp2.
CLEAR : v_tmp ,
v_tEmp.
v_tmp-col = '3'.
v_temp-color = v_tmp.
v_temp-fname = 'LABST'.
append v_temp to v_tmp2.
g_wa_mard1-row_d = v_tmp2.
endif.
APPEND G_WA_MARD1 TO G_T_MARD1.
CLEAR G_WA_MARD1.
endloop.
Regards
Arun