背景
ABAP实现动态的表达式计算,可以使用函数EVAL_FORMULA实现,参考代码在下面;同时也可以考虑用类CL_JAVA_SCRIPT调用js功能实现。
执行效果
参考代码
代码语言:javascript复制*&---------------------------------------------------------------------*
*& 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