Groovy 将 poi.jar 用于数据接收器脚本时出现编译错误

Groovy compilation error when using poi.jar for a data sink script

我是编码新手,下面是我在 Soap UI 中使用的 groovy 脚本,用于将响应数据转储到 excel sheet。我在第 55 行收到错误,'}' 用于关闭 if 条件(我认为这不是问题所在)。早些时候我只使用 jexl 并且它工作正常但是后来我切换到 poi 以便能够在相同的 sheet 中附加数据并且它抛出错误。我已经在 bin/ext.

中复制了所有相关的 jar

import java.io.*;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.xssf.usermodel.*;
import org.apache.poi.*;
import jxl.*;

def grUtils = new com.eviware.soapui.support.GroovyUtils(context)
def ReqHolder = grUtils.getXmlHolder("Search#Response")
def HotelCount = ReqHolder["count(//*:Property)"]
def tCNo = context.expand( '${DSS#TCNo.}' )

if (tCNo =='1')
{
    FileInputStream fsIP= new FileInputStream(new File("C:/Users/129858/Desktop/Soap/Simplify_HTE/SearchSink.xls"));    
    HSSFWorkbook copy = new HSSFWorkbook(fsIP);
    HSSFSheet worksheet = copy.getSheetAt(0);

    CL = 0;
    rows = HotelCount.toInteger();

    Cell cell = null;
    cell = worksheet.getRow(0).getCell(0);
    cell.setCellValue("TCNo.");
    cell = worksheet.getRow(0).getCell(1);
    cell.setCellValue("HotelName");
    cell = worksheet.getRow(0).getCell(2);
    cell.setCellValue("HotelCode");
    cell = worksheet.getRow(0).getCell(3);
    cell.setCellValue("BrandCode");

    for( tc_row in 1..rows){
        Cell box = null;

        cell = worksheet.getRow(tc_row).getCell(CL);
        String s0 = tCNo;
        cell.setCellValue(s1);

        cell = worksheet.getRow(tc_row).getCell(CL+1);
        String s1 = ReqHolder.getNodeValue("//*:Property[$tc_row]/@HotelName");
        cell.setCellValue(s1);

        cell = worksheet.getRow(tc_row).getCell(CL+2);
        String s2 = ReqHolder.getNodeValue("//*:Property[$tc_row]/@HotelCode");
        cell.setCellValue(s2);

        cell = worksheet.getRow(tc_row).getCell(CL+3);
        String s3 = ReqHolder.getNodeValue("//*:Property[$tc_row]/@BrandCode");
        cell.setCellValue(s3);

    }
    fsIP.close();
    FileOutputStream fsOP =new FileOutputStream(new File("C:/Users/129858/Desktop/Soap/Simplify_HTE/SearchSink.xls");
    copy.write(fsOP);
    fsOP.close();
}
else{

    FileInputStream  file = new File("C:/Users/129858/Desktop/Soap/Simplify_HTE/SearchSink.xls");
    HSSFWorkbook wb = new HSSFWorkbook(file);
    HSSFsheet ws = wb.getSheetAt(0):

    datarows = ws.getRows();
    col = 0;
    log.info datarows
    exrows = HotelCount.toInteger() + datarows + 1;
    log.info exrows

    for( tc_ro in datarows+1..exrows)
    {
        Cell box = null;

        cell = worksheet.getRow(tc_ro).getCell(col);
        String s0 = tCNo;
        cell.setCellValue(s1);

        cell = worksheet.getRow(tc_ro).getCell(col+1);
        String s1 = ReqHolder.getNodeValue("//*:Property[$tc_row]/@HotelName");
        cell.setCellValue(s1);

        cell = worksheet.getRow(tc_ro).getCell(col+2);
        String s2 = ReqHolder.getNodeValue("//*:Property[$tc_row]/@HotelCode");
        cell.setCellValue(s2);

        cell = worksheet.getRow(tc_ro).getCell(col+3);
        String s3 = ReqHolder.getNodeValue("//*:Property[$tc_row]/@BrandCode");
        cell.setCellValue(s3);

}
    fsIP.close();
    FileOutputStream fsOP =new FileOutputStream(new File("C:/Users/129858/Desktop/Soap/Simplify_HTE/SearchSink.xls");
    copy.write(fsOP);
    fsOP.close();
}

第 53 行缺少一个右括号:

FileOutputStream fsOP =new FileOutputStream(new File("C:/Users/129858/Desktop/Soap/Simplify_HTE/SearchSink.xls");

应该是:

FileOutputStream fsOP =new FileOutputStream(new File("C:/Users/129858/Desktop/Soap/Simplify_HTE/SearchSink.xls"));

同样的事情发生在第 91 行。

考虑使用具有代码验证和完成功能的编辑器。您可以从广泛的免费使用 IDE 中进行选择,例如 IntelliJ IDEA、Eclipse、Netbeans 等等。 IDE例如,A 会很快告诉您缺少这个右括号,这样您会节省很多时间。

感谢 Stepniak,为我指明了正确的方向。它有太多的错误和一些非常糟糕的编码。现在可以正常使用了。

import java.io.*;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;
import org.apache.poi.*;
import jxl.*;

def grUtils = new com.eviware.soapui.support.GroovyUtils(context)
def ReqHolder = grUtils.getXmlHolder("Search#Response")
def HotelCount = ReqHolder["count(//*:Property)"]
def tCNo = context.expand( '${DSS#TCNo.}' )

if (tCNo =='1')
{

HSSFWorkbook copy = new HSSFWorkbook();
HSSFSheet worksheet = copy.createSheet("SearchSink");

CL = 0;
rows = HotelCount.toInteger();

Row head = worksheet.createRow(0);
Cell cell0 = head.createCell(0);
cell0.setCellValue("TCNo.");
Cell cell1 = head.createCell(1);
cell1.setCellValue("HotelName");
Cell cell2 = head.createCell(2);
cell2.setCellValue("HotelCode");
Cell cell3 = head.createCell(3);
cell3.setCellValue("BrandCode");

for( tc_row in 1..rows){
    Row data = worksheet.createRow(tc_row);

    Cell box0 = data.createCell(CL);
    String s0 = tCNo;
    box0.setCellValue(s0);

    Cell box1 = data.createCell(CL+1);
    String s1 = ReqHolder.getNodeValue("//*:Property[$tc_row]/@HotelName");
    box1.setCellValue(s1);

    Cell box2 = data.createCell(CL+2);
    String s2 = ReqHolder.getNodeValue("//*:Property[$tc_row]/@HotelCode");
    box2.setCellValue(s2);

    Cell box3 = data.createCell(CL+3);
    String s3 = ReqHolder.getNodeValue("//*:Property[$tc_row]/@BrandCode");
    box3.setCellValue(s3);

}
FileOutputStream fsOP =new FileOutputStream(new 
File("C:/Users/129858/Desktop/Soap/Simplify_HTE/SearchSink.xls"));
copy.write(fsOP);
fsOP.close();
}
else{

FileInputStream  file = new FileInputStream(new 
File("C:/Users/129858/Desktop/Soap/Simplify_HTE/SearchSink.xls"));
HSSFWorkbook wb = new HSSFWorkbook(file);
HSSFSheet ws = wb.getSheetAt(0);

int datarows = ws.getLastRowNum();
col = 0;
log.info datarows
exrows = HotelCount.toInteger() + datarows;
log.info exrows

for( tc_ro in datarows+1..exrows)
{
    Row data = ws.createRow(tc_ro);
    Cell box0 = data.createCell(col);
    String s0 = tCNo;
    box0.setCellValue(s0);

    Cell box1 = data.createCell(col+1);
    String s1 = ReqHolder.getNodeValue("//*:Property[$tc_ro-$datarows]/@HotelName");
    box1.setCellValue(s1);

    Cell box2 = data.createCell(col+2);
    String s2 = ReqHolder.getNodeValue("//*:Property[$tc_ro-$datarows]/@HotelCode");
    box2.setCellValue(s2);

    Cell box3 = data.createCell(col+3);
    String s3 = ReqHolder.getNodeValue("//*:Property[$tc_ro-$datarows]/@BrandCode");
    box3.setCellValue(s3);

}
file.close();
FileOutputStream fsOP =new FileOutputStream(new 
File("C:/Users/129858/Desktop/Soap/Simplify_HTE/SearchSink.xls"));
wb.write(fsOP);
fsOP.close();
}