如何从 JCoTable 对象读取特定值

问题描述

我通过 RFC_GET_TABLE_ENTRIES 从 SAP 系统成功获取了表条目。它工作正常,并列出了表格的所有行。

我现在的问题是我不知道如何获取单个值。通常我会使用代码 [x][y] 但这不起作用,因为它不是一个普通的二维数组表而是一个 JCOtable,我不知道它是如何工作的。

代码有点长,但这就是调用本身。

JCoDestination destination = JCoDestinationManager.getDestination("mySAPSystem");
JCoFunction function = destination.getRepository().getFunction("RFC_GET_TABLE_ENTRIES");



if (function==null)
    throw new RuntimeException("Function not found in SAP.");

function.getImportParameterList().setValue( "MAX_ENTRIES",30);
function.getImportParameterList().setValue( "TABLE_NAME","ZTEST_TABLE ");
JCoTable codes = function.getTableParameterList().getTable("ENTRIES");
codes.appendRow();

这是控制台输出

System.out.println("RFC_GET_TABLE_ENTRIES");

for (int i = 0; i < 30; i++) {
    codes.setRow(i);
    System.out.println(codes.getString("WA"));
}

解决方法

getString 实际上也接受索引。如果您想根据 x 和 y 检索值,您可以执行以下操作

codes.setRow(y);
String value = codes.getString(x); // It can also be getFloat,getInt,etc. depending on the data type,// or getValue,which gives you an Object

它的工作方式与 codes[x][y] 类似,好像它是一个数组,但这并不常用。

在其他情况下,您可能希望使用 JCoRecordFieldIterator 遍历行中的每个值。

JCoRecordFieldIterator itr = codes. getRecordFieldIterator();
while(itr.hasNextField()){
    JCoRecordField field = itr.nextRecordField();
    String value = field.getValue(); // Or getString,getFloat,etc.
    // Whatever you want to do with the value
}