ABAP CL_DEMO_OUTPUT类的运用

本文详细介绍了ABAP中的CL_DEMO_OUTPUT类,该类提供了一系列简化数据输出的方法,适用于示例程序,无需传统列表即可生成输出。文章覆盖了如何通过静态和实例方法使用CL_DEMO_OUTPUT,并提供了丰富的代码示例,展示如何创建不同格式的输出,如HTML、XML、JSON等。

文章来自:https://blogs.sap.com/2016/05/10/cldemooutput-part-1-of-2-usage/

Methods of CL_DEMO_OUTPUT

 

The methods of class CL_DEMO_OUTPUT create simple outputs of data in example programs without the need of classical lists. The class can be used via static methods and instance methods. The following methods create output in an output stream:

类CL_DEMO_OUTPUT 在示例程序中创造了很多简单的数据输出的方法而不需要经典的list。这个类可以通过静态或实例化的方式使用。下面这些方法在输出流中创建输出。

 

  • Methods BEGIN_SECTION, NEXT_SECTION, and END_SECTION create headers and open or close header levels.
  • 方法BEGIN_SECTION,NEXT_SECTION和END_SECTION创建标题和开始或结束标题级别
  • Methods WRITE_DATA, WRITE_TEXT, WRITE_XML, WRITE_JSON, and WRITE_HTML write different kinds of output into the output stream.
  • 方法WRITE_DATA,WRITE_TEXT,WRITE_XML,WRITE_JSON和WRITE_HTML显示不同类型的输出。
     
    • With method WRITE_DATA you can write elementary data objects (no reference variables), structures with elementary components, and internal tables of such line types.
    • 使用WRITE_DATA可以输出基本类型对象(不是引用类型),结构的组件和内表的行类型。
       
    • The other methods create formated outputs of texts, XML, JSON, or HTML data.
    • 其它方法创建格式化的输出数据如xml ,json,HTML。
       
  • Method WRITE is generic. It handles ABAP data as well as texts (in non proportional format).

 

  • Methods DISPLAY_... (available  as static methods only) work as WRITE_..., but close the current output stream and open a new one. If a SAP GUI is available, the output is displayed in a window.
     
  • Method LINE creates a horzontal line.
     
  • Method DISPLAY closes the current output stream and opens a new one. If a SAP GUI is available, the output is displayed in a window. Optionally you can also pass data to DISPLAY as you can do forWRITE.
     
  • Method GET works like DISPLAY but does not display the data. Instead the formated output data are returned in a text string and can be handled further.

 

The standard output format is HTML. Optionally you can choose a simple text format with tabulators and line breaks. You choose the format with method SET_MODE for the static methods or using the input parameter MODE of the factory methodNEW for the instance methods.

 

The class CL_DEMO_OUTPUT is available in release 7.03/7.31 since SP07 and in higher releases. It has a class documentation.

 

Code Examples

 

The most simple and common type of usage might look as follows:

SELECT *

       FROM scarr

       INTO TABLE @DATA(carriers).


cl_demo_output=>display( carriers ).

 

The output is:

A program using more than one static method of CL_DEMO_OUTPUT might look as follows:

SELECT *

       FROM scarr

       INTO TABLE @DATA(carriers).

CALL TRANSFORMATION id SOURCE carriers = carriers

                       RESULT XML DATA(xml).


cl_demo_output=>begin_section( `Some Text` ).

cl_demo_output=>write_text( |blah blah blah \n| &&

                            |blah blah blah| ).

cl_demo_output=>next_section( `Some Data` ).

cl_demo_output=>begin_section( `Elementary Object` ).

cl_demo_output=>write_data( carriers[ 1 ]-carrid ).

cl_demo_output=>next_section( `Internal Table` ).

cl_demo_output=>write_data( carriers ).

cl_demo_output=>end_section( ).

cl_demo_output=>next_section( `XML` ).

cl_demo_output=>write_xml( xml ).

cl_demo_output=>display( ).

 

Since this looks very ugly, it is better to use the instance methods instead of the static methods if you call more than 3 to 4 methods of the class within a program:

SELECT *

       FROM scarr

       INTO TABLE @DATA(carriers).

CALL TRANSFORMATION id SOURCE carriers = carriers

                       RESULT XML DATA(xml).


cl_demo_output=>new(

  )->begin_section( `Some Text`

  )->write_text( |blah blah blah \n| &&

                 |blah blah blah|

  )->next_section( `Some Data`

  )->begin_section( `Elementary Object`

  )->write_data( carriers[ 1 ]-carrid

  )->next_section( `Internal Table`

  )->write_data( carriers

  )->end_section(

  )->next_section( `XML`

  )->write_xml( xml

  )->display( ).

 

Both give the same output:

 

You might ask yourself two things:

 

  • How can static methods and instance methods have the same name? 

    The instance methods are interface methods. Method NEW returns a reference variable of typeIF_DEMO_OUTPUT. This interface is implemented by CL_DEMO_OUTPUT. The interface methods have the same names as the static methods of the class.  
            
  • Why can you chain these methods? 

    For this convenience, each instance method returns the self reference me.

 

If you want a more simplistic output, you can switch to text mode:

SELECT *

       FROM scarr

       INTO TABLE @DATA(carriers).


cl_demo_output=>new( 'TEXT'

  )->display( carriers ).

 

If you want to deal with the resulting formatted data yourself, you use GET instead of DISPLAY:

 

SELECT *

       FROM scarr

       INTO TABLE @DATA(carriers).

 

DATA(html) = cl_demo_output=>get( carriers ).

 

cl_abap_browser=>show_html( html_string = html ).


This produces the same output as the first example above.

 

You can also examine and run the following programs to get a complete overview of all possiblities:

 

  • DEMO_USAGE_OUTPUT_STATIC
  • DEMO_USAGE_OUTPUT_INSTANCE

 

Examples of Usage

 

An example how CL_DEMO_OUTPUT can be used by a framework is provided by the ABAP Keyword Documentation in ADT (aka ABAP in Eclipse). If an example of the ABAP Example Library uses CL_DEMO_OUTPUT, the documentation framework allows you to execute the example and displays the output. This is done by getting the HTML output fromCL_DEMO_OUTPUT  and merging it into the (non SAP GUI) documentation display.

 

Another example is quite remarkable. CL_DEMO_OUTPUT made it to the stage in SAP Teched 2013!

 

Here a snapshot from Dr. Vishal Sikka's keynote:

 

(B.t.w., see  AMDP, Comparison of SQLScript with Open SQL for a closer look at the performance results of that example; The bad ABAP results above come from nested SELECT loops …).

<think> 首先,用户的问题是关于ABAP编程的,具体是如何解决语句 `CL_DEMO_OUTPUT=>DISPLAY(GT_DATA)` 未定义的问题。用户提到了一个引用,但根据系统指令,我应该参考之前的上下文,但这里没有提供之前的对话历史。所以,我需要基于当前查询来回答。 关键点: - 用户想解决 `CL_DEMO_OUTPUT=>DISPLAY(GT_DATA)` 未定义的问题。 - 引用[1]提到了一种方法:使用 `cl_salv_bs_runtime_info=>set( display = SPACE metadata = SPACE data = 'X' )` 来避免输出报表但获取数据。这似乎是相关的,但不能直接解决未定义的问题。 系统指令: 1. **行内数学表达式**:必须用 `$...$` 格式。但在这个上下文中,没有数学表达式,所以可能不需要。 2. **独立公式**:必须用 `$$...$$` 并单独成段。同样,可能不需要。 3. **LaTeX语法正确**:确保任何数学格式正确。 4. **使用中文回答**:整个回答必须用中文。 5. **生成相关问题**:在回答结束时,添加相关问题,标记为 `§§相关问题§§`。 6. **引用的段落末尾添加引用标识**:如 `[^1]`。用户提供了引用[1],所以我应该自然地引用它。 7. **用户可见层**:回答结构清晰,逐步解决问题,确保真实可靠。 用户的问题核心:`CL_DEMO_OUTPUT=>DISPLAY(GT_DATA)` 未定义。这意味着在ABAP中,这个或方法可能不存在或没有被正确引用。 可能的解决方案: - `CL_DEMO_OUTPUT` 是SAP ABAP中的一个标准,用于演示输出。如果未定义,可能是环境问题或未激活。 - 常见原因: - `CL_DEMO_OUTPUT` 没有被激活。 - 缺少必要的包含或型组。 - 在错误的系统版本中。 - 解决方案: - 确保已激活:在SE24或SE80中激活。 - 使用替代方法,如引用[1]提到的 `cl_salv_bs_runtime_info` 来输出数据。 - 检查SAP系统版本;`CL_DEMO_OUTPUT` 可能只在较新版本中可用。 - 如果不可用,使用其他输出方法,如 `WRITE` 语句或ALV网格。 引用[1]的建议:它提到使用 `cl_salv_bs_runtime_info=>set(...)` 来设置运行时信息,避免显示报表但获取数据。这可以用来替代 `CL_DEMO_OUTPUT=>DISPLAY`,因为 `CL_DEMO_OUTPUT` 可能用于简单输出,而 `cl_salv_bs_runtime_info` 用于SALV(SAP List Viewer)相关的输出。 结构化回答: - **步骤1:确认问题**:解释未定义的原因。 - **步骤2:解决方案**:提供修复方法。 - **步骤3:替代方案**:如果 `CL_DEMO_OUTPUT` 不可用,使用其他输出方式。 - **自然引用**:在相关部分末尾添加 `[^1]`。 生成相关问题: - 需要在回答结束时添加相关问题,格式如 `§§相关问题§§`。 最终输出结构: - 回答用户问题。 - 添加相关问题列表。 相关问题想法: 1. 如何在ABAP中激活未定义的? 2. CL_DEMO_OUTPUT 的其他用途? 3. SALV 输出方法的最佳实践? 4. ABAP 输出数据的常见方式? 5. 如何处理ABAP中的运行时错误? 确保真实可靠:基于SAP ABAP知识,`CL_DEMO_OUTPUT` 是一个标准;如果未定义,通常需要激活它或使用替代。 完整回答: - 解释问题。 - 给出解决方案:激活或使用替代如 `cl_salv_bs_runtime_info`。 - 引用引用[1]。 - 结束相关问题。 </think> ### 解决 CL_DEMO_OUTPUT=>DISPLAY(GT_DATA) 未定义问题 在ABAP编程中,语句 `CL_DEMO_OUTPUT=>DISPLAY(GT_DATA)` 未定义通常是由于 `CL_DEMO_OUTPUT` 未激活、系统环境不支持或缺少必要依赖导致的。这是一个常见问题,我将一步步引导您解决,确保方案真实可靠(基于SAP ABAP标准文档和实践经验)。 #### 步骤1: 确认原因和基本检查 - **原因分析**: `CL_DEMO_OUTPUT` 是SAP提供的标准,用于简化数据输出(常用于演示)。如果未定义,可能原因包括: - 未被激活或未在系统中注册。 - 您的SAP系统版本较旧(例如,低于S/4HANA 1809),该可能不可用。 - 代码中缺少型声明或包含文件。 - **初步检查**: 1.ABAP编辑器(SE24或SE80)中,输入 `CL_DEMO_OUTPUT` 并检查状态。如果未激活,请激活它。 2. 确保 `GT_DATA` 已正确定义为内部表(例如,`DATA: gt_data TYPE TABLE OF spfli.`)。 3. 验证系统版本:在事务码 `SE38` 中执行 `SY-SYSID` 查看版本;如果版本过低,考虑升级或使用替代方案。 #### 步骤2: 解决方案 - 激活或使用替代方法 如果 `CL_DEMO_OUTPUT` 可用但未激活: - 在事务码 SE24 中: 1. 输入名 `CL_DEMO_OUTPUT`。 2. 点击 "激活" 按钮(如果状态为 "未激活")。 3. 重试您的代码: `CL_DEMO_OUTPUT=>DISPLAY(GT_DATA)`。 如果 `CL_DEMO_OUTPUT` 不可用(如系统版本不支持),使用替代输出方法: - **推荐替代方案**:使用 SALV(SAP List Viewer)或简单输出语句。引用[1]提到了一种方法:通过 `cl_salv_bs_runtime_info` 设置运行时信息来输出数据而不显示报表。您可以将此调整为: ```abap DATA: lo_runtime_info TYPE REF TO cl_salv_bs_runtime_info. " 设置运行时信息,仅获取数据(不显示报表) lo_runtime_info = cl_salv_bs_runtime_info=>get_instance( ). lo_runtime_info->set( display = abap_false metadata = abap_false data = abap_true ). " 调用SALV输出(示例,需替换为您的逻辑) CALL METHOD cl_salv_table=>factory IMPORTING r_salv_table = lo_alv CHANGING t_table = gt_data. " 如果需要显示数据,使用以下语句 lo_alv->display( ). ``` 此代码避免了 `CL_DEMO_OUTPUT`,直接使用SALV输出数据[^1]。确保 `gt_data` 已填充有效数据。 - **简单替代**:如果仅需调试输出,使用 `WRITE` 语句: ```abap LOOP AT gt_data INTO DATA(ls_data). WRITE: / ls_data-field1, ls_data-field2. " 替换为您的字段 ENDLOOP. ``` #### 步骤3: 测试和验证 - 在开发系统(如SE38)中创建测试报告: 1. 复制您的代码,包括数据定义和输出逻辑。 2. 执行报告:如果使用了 `cl_salv_bs_runtime_info`,确保在调用前设置好参数。 3. 常见错误处理:如果仍报错 "未定义",检查 ABAP 字典(SE11)确保所有型正确。 如果问题持续,提供更多上下文(如完整代码或系统版本),我可以进一步优化方案。解决此问题通常涉及激活核心或采用兼容的输出方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值