matinal:SAP ABAP 程序里如何使用计算公式,函数EVAL_FORMULA可以解决

背景

ABAP实现动态的表达式计算,可以使用函数EVAL_FORMULA实现,参考代码在下面;同时也可以考虑用类CL_JAVA_SCRIPT调用js功能实现。

执行效果

 参考代码

*&---------------------------------------------------------------------*
*& Report ZMATINAL
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*& Report ZMATINAL
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
report zmatinal.



data: dataf type f,
      datap type lbkum.

parameters: formula(40) default 'A*B*C*D',
            a_val       type lbkum default 3,
            b_val       type lbkum default 1,
            c_val       type lbkum default 2,
            d_val       type lbkum default 4.

start-of-selection.

  call function 'CHECK_FORMULA'
    exporting
      formula           = formula
      program           = sy-repid
      routine           = 'SUB_CHECK_FORMULA'
*     UNIT_OF_MEASURE   = ' '
* IMPORTING
*     FUNCNAME          =
*     MESSAGE           =
*     POS               =
*     SUBRC             =
    exceptions
      error_in_formula  = 1
      missing_parameter = 2
      others            = 3.
  if sy-subrc <> 0.
    message id sy-msgid type sy-msgty number sy-msgno
          with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  endif.

  call function 'EVAL_FORMULA'
    exporting
      formula                 = formula
      program                 = sy-repid
      routine                 = 'SUB_GET_VALUE'
    importing
      value                   = dataf
    exceptions
      division_by_zero        = 1
      exp_error               = 2
      formula_table_not_valid = 3
      invalid_expression      = 4
      invalid_value           = 5
      log_error               = 6
      parameter_error         = 7
      sqrt_error              = 8
      units_not_valid         = 9
      missing_parameter       = 10
      others                  = 11.

  if sy-subrc <> 0.
    message id sy-msgid type sy-msgty number sy-msgno
          with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  else.

    move dataf to datap.
    write:/ formula, datap.
  endif.

form sub_check_formula using value(name)
                       CHANGING value(subrc).
subrc = 0.

ENDFORM.


form sub_get_value using value(name)
                  changing value(value)
                           value(subrc).
  subrc = 0.
  value = 0.  
  case name.
    when 'A'.
      value = a_val.
    when 'B'.
      value = b_val.
    when 'C'.
      value = c_val.
    when 'D'.
      value = d_val.
  endcase.
endform.                    "VAR_GET

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值