尝试从groovy脚本中获取值时出现excel表问题

时间:2017-12-08 12:56:03

标签: excel groovy soapui

如果我将 getCell(0,0)放在String reqTagName = sheet1.getCell(0,0).getContents()中以获得以下代码,那么它只执行第一个值来自片。但是,如果我为同一个字符串reqTagName = sheet1.getCell(0,0).getContents()放置 getCell(0,Row)并将excel表中的值从数字更改为字符串然后它按预期工作。但是当我在工作表中将数字作为请求值传递时,它不起作用。我已经尝试将数据类型从String更改为任何其他仍无法正常工作....

我已粘贴以下代码:

import com.eviware.soapui.support.XmlHolder
import java.io.File
import java.io.IOException
import jxl.*
import jxl.read.biff.BiffException
import jxl.write.*
import jxl.write.Label
log.info("Testing Started")
def reqOperationName = "getInsuranceDetails_1_FTC_005"
def inputDataFileName = "D:/SOAP UI Pro/MPI.xls"
def inputDataSheetName = "MPI"
Workbook workbook = Workbook.getWorkbook(new File(inputDataFileName))
Sheet  sheet1 = workbook.getSheet(inputDataSheetName)
def responsesCount = 0

String[] Responses = null
def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
String xmlResponse = reqOperationName+"#Request"
def reqholder = groovyUtils.getXmlHolder(xmlResponse)
try{
    rowcount = sheet1.getRows()
    colcount = sheet1.getColumns()
     Responses = new String[rowcount-1]
    responsesCount = rowcount-1
    for(Row in 1..rowcount-1){

    String reqTagName = sheet1.getCell(0,Row).getContents()
        log.info reqTagName
        def TagCount = reqholder["count(//*:"+reqTagName+")"]
        if(TagCount!=0){
            String reqTagValue = sheet1.getCell(0,Row).getContents()
            reqholder.setNodeValue("//*:"+reqTagName, reqTagValue)
            reqholder.updateProperty()                              
        }

    //test the request
    testRunner.runTestStepByName(reqOperationName)
    reqholder = groovyUtils.getXmlHolder(reqOperationName+"#Response")
    Responses[Row-1] = reqholder.getPrettyXml().toString()
    log.info Responses[Row-1]

    }
}
catch (Exception e) {log.info(e)}
finally{
    workbook.close()
}
Workbook existingWorkbook = Workbook.getWorkbook(new File(inputDataFileName));
WritableWorkbook workbookCopy = Workbook.createWorkbook(new File(inputDataFileName), existingWorkbook);

try
{
    WritableSheet sheetToEdit = workbookCopy.getSheet("MPI");
    WritableCell cell;
    for (int i =1;i<Responses.size();i++)
    {
    def resholder = groovyUtils.getXmlHolder(Responses[i])

    resTagValue1= resholder.getNodeValue("//*:productID")
    Label l = new Label(2, i, resTagValue1.toString());
    cell = (WritableCell) l;
    sheetToEdit.addCell(cell);

    resTagValue2= resholder.getNodeValue("//*:accountNumber")
    Label m = new Label(3, i, resTagValue2.toString());
    cell = (WritableCell) m;
    sheetToEdit.addCell(cell);

    resTagValue3= resholder.getNodeValue("//*:insuranceCategory")
    Label n = new Label(4, i, resTagValue3.toString());
    cell = (WritableCell) n;
    sheetToEdit.addCell(cell);

    resTagValue4= resholder.getNodeValue("//*:imei")
    Label o = new Label(5, i, resTagValue4.toString());
    cell = (WritableCell) o;
    sheetToEdit.addCell(cell);

    resTagValue5= resholder.getNodeValue("//*:handsetMake")
    Label p = new Label(6, i, resTagValue5.toString());
    cell = (WritableCell) p;
    sheetToEdit.addCell(cell);

    resTagValue6= resholder.getNodeValue("//*:handsetModel")
    Label q = new Label(7, i, resTagValue6.toString());
    cell = (WritableCell) q;
    sheetToEdit.addCell(cell);

    resTagValue7= resholder.getNodeValue("//*:insurancePolicyName")
    Label r = new Label(8, i, resTagValue7.toString());
    cell = (WritableCell) r;
    sheetToEdit.addCell(cell);

    resTagValue8= resholder.getNodeValue("//*:insuranceStartTimestamp")
    Label s = new Label(9, i, resTagValue8.toString());
    cell = (WritableCell) s;
    sheetToEdit.addCell(cell);

    resTagValue9= resholder.getNodeValue("//*:insuranceEndTimestamp")
    Label t = new Label(10, i, resTagValue9.toString());
    cell = (WritableCell) t;
    sheetToEdit.addCell(cell);
    }
}
catch (Exception e) {log.info(e)}
finally{
     workbookCopy.write();
 workbookCopy.close();
 existingWorkbook.close();
}
log.info("Testing Over")

我知道错误是在excel表上。如果代码有任何变化或使用excel表格进行某些实现吗?

任何帮助都将受到高度赞赏。

谢谢,

实施更改后编辑的代码

import com.eviware.soapui.support.XmlHolder
import java.io.File
import java.io.IOException
import jxl.*
import jxl.read.biff.BiffException
import jxl.write.*
import jxl.write.Label
log.info("Testing Started")
def reqOperationName = "getInsuranceDetails_1_FTC_005"
def inputDataFileName = "D:/SOAP UI Pro/MPI.xls"
def inputDataSheetName = "MPI"
Workbook workbook = Workbook.getWorkbook(new File(inputDataFileName))
Sheet  sheet1 = workbook.getSheet(inputDataSheetName)
def responsesCount = 0


String[] Responses = null
def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
String xmlResponse = reqOperationName+"#Request"
def reqholder = groovyUtils.getXmlHolder(xmlResponse)
try{
    rowcount = sheet1.getRows()
    colcount = sheet1.getColumns()
     Responses = new String[rowcount-1]
    responsesCount = rowcount-1
    for(Row in 1..rowcount-1){

    String reqTagName = sheet1.getCell(0,0).getContents()
    log.info reqTagName
    def TagCount = reqholder["count(//*:"+reqTagName+")"]
    log.info reqholder["//*:"+reqTagName]
    if(TagCount!=0){
        String reqTagValue = sheet1.getCell(0,Row).getContents()
        reqholder.setNodeValue("//*:"+reqTagName, reqTagValue)
        reqholder.updateProperty()                              
    }

    //test the request
    testRunner.runTestStepByName(reqOperationName)
    reqholder = groovyUtils.getXmlHolder(reqOperationName+"#Response")
    Responses[Row-1] = reqholder.getPrettyXml().toString()
    log.info Responses[Row-1]

    }
}
catch (Exception e) {log.info(e)}
finally{
    workbook.close()
}
Workbook existingWorkbook = Workbook.getWorkbook(new File(inputDataFileName));
WritableWorkbook workbookCopy = Workbook.createWorkbook(new File(inputDataFileName), existingWorkbook);

try
{
    WritableSheet sheetToEdit = workbookCopy.getSheet("MPI");
    WritableCell cell;
    for (int i =1;i<Responses.size();i++)
    {
    def resholder = groovyUtils.getXmlHolder(Responses[i])

    resTagValue1= resholder.getNodeValue("//*:productID")
    Label l = new Label(2, i, resTagValue1.toString());
    cell = (WritableCell) l;
    sheetToEdit.addCell(cell);

    resTagValue2= resholder.getNodeValue("//*:accountNumber")
    Label m = new Label(3, i, resTagValue2.toString());
    cell = (WritableCell) m;
    sheetToEdit.addCell(cell);

    resTagValue3= resholder.getNodeValue("//*:insuranceCategory")
    Label n = new Label(4, i, resTagValue3.toString());
    cell = (WritableCell) n;
    sheetToEdit.addCell(cell);

    resTagValue4= resholder.getNodeValue("//*:imei")
    Label o = new Label(5, i, resTagValue4.toString());
    cell = (WritableCell) o;
    sheetToEdit.addCell(cell);

    resTagValue5= resholder.getNodeValue("//*:handsetMake")
    Label p = new Label(6, i, resTagValue5.toString());
    cell = (WritableCell) p;
    sheetToEdit.addCell(cell);

    resTagValue6= resholder.getNodeValue("//*:handsetModel")
    Label q = new Label(7, i, resTagValue6.toString());
    cell = (WritableCell) q;
    sheetToEdit.addCell(cell);

    resTagValue7= resholder.getNodeValue("//*:insurancePolicyName")
    Label r = new Label(8, i, resTagValue7.toString());
    cell = (WritableCell) r;
    sheetToEdit.addCell(cell);

    resTagValue8= resholder.getNodeValue("//*:insuranceStartTimestamp")
    Label s = new Label(9, i, resTagValue8.toString());
    cell = (WritableCell) s;
    sheetToEdit.addCell(cell);

    resTagValue9= resholder.getNodeValue("//*:insuranceEndTimestamp")
    Label t = new Label(10, i, resTagValue9.toString());
    cell = (WritableCell) t;
    sheetToEdit.addCell(cell);
    }
}
catch (Exception e) {log.info(e)}
finally{
     workbookCopy.write();
 workbookCopy.close();
 existingWorkbook.close();
}
log.info("Testing Over")

在输出日志中,我可以看到它正在使用第一个MSISDN,但之后它会占用一些垃圾字段。

每次执行时都会提到日志输出。

Mon Dec 11 16:44:18 IST 2017:INFO:447803000269

Mon Dec 11 16:44:18 IST 2017:INFO:[Ljava.lang.String; @ 8ddf43

Mon Dec 11 16:44:18 IST 2017:INFO:[Ljava.lang.String; @ 7bd8d3

Mon Dec 11 16:44:19 IST 2017:INFO:[Ljava.lang.String; @ 12dda68

2 个答案:

答案 0 :(得分:0)

尝试以下步骤,如果它解决了问题,请告诉我:

第1步:打开Excel WorkBook

步骤2:选择包含数字的列值,如下图所示,然后右键单击以显示选项 View here

步骤3:点击格式化单元格,它将打开一个窗口 view here

步骤4:点击数字,如下图所示 view here

步骤5:转到右侧的十进制位置,然后单击向下箭头,如下图所示 view here

步骤6:单击向下箭头,直到将小数位数设置为0

步骤7:转到图中所示的负数,然后选择第二个选项 view here

步骤8:单击“确定”并关闭窗口。

步骤9:保存Excel工作表并运行脚本。你的问题应该修复

让我知道它是否有效:)

答案 1 :(得分:0)

请尝试下面的代码更改,并告诉我它是否有效:

import com.eviware.soapui.support.XmlHolder
import java.io.File
import java.io.IOException
import jxl.*
import jxl.read.biff.BiffException
import jxl.write.*
import jxl.write.Label
log.info("Testing Started")
def reqOperationName = "getInsuranceDetails_1_FTC_005"
def inputDataFileName = "D:/SOAP UI Pro/MPI.xls"
def inputDataSheetName = "MPI"
Workbook workbook = Workbook.getWorkbook(new File(inputDataFileName))
Sheet  sheet1 = workbook.getSheet(inputDataSheetName)


def myList = new ArrayList<String>();
def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
String xmlResponse = reqOperationName+"#Request"
def reqholder = groovyUtils.getXmlHolder(xmlResponse)
try{
    rowcount = sheet1.getRows()
    colcount = sheet1.getColumns()

    for(Row in 1..rowcount-1){

    String reqTagName = sheet1.getCell(0,0).getContents()

    def TagCount = reqholder["count(//*:"+reqTagName+")"]

    if(TagCount!=0){
        String reqTagValue = sheet1.getCell(0,Row).getContents()
        if(reqTagValue!=null && !reqTagValue.isEmpty() && reqTagValue!="")
                {
                    reqholder = groovyUtils.getXmlHolder(xmlResponse)
                    log.info "extracted value : " + reqTagValue
                reqholder.setNodeValue("//*:"+reqTagName, reqTagValue)
                reqholder.updateProperty()        
                log.info "node value : " + reqholder.getNodeValue("//*:"+reqTagName)
                //test the request
                testRunner.runTestStepByName(reqOperationName)
                reqholder = groovyUtils.getXmlHolder(reqOperationName+"#Response")
                myList.add(reqholder.getPrettyXml().toString())
                log.info myList[Row-1]
                }                      
    }

    }
}
catch (Exception e) {log.info(e)}
finally{
    workbook.close()
}
Workbook existingWorkbook = Workbook.getWorkbook(new File(inputDataFileName));
WritableWorkbook workbookCopy = Workbook.createWorkbook(new File(inputDataFileName), existingWorkbook);

try
{
    WritableSheet sheetToEdit = workbookCopy.getSheet(inputDataSheetName);
    WritableCell cell;
    for (int i =1;i<myList.size();i++)
    {
    def resholder = groovyUtils.getXmlHolder(myList[i])

    resTagValue1= resholder.getNodeValue("//*:productID")
    Label l = new Label(2, i+1, resTagValue1.toString());
    cell = (WritableCell) l;
    sheetToEdit.addCell(cell);

    resTagValue2= resholder.getNodeValue("//*:accountNumber")
    Label m = new Label(3, i+1, resTagValue2.toString());
    cell = (WritableCell) m;
    sheetToEdit.addCell(cell);

    resTagValue3= resholder.getNodeValue("//*:insuranceCategory")
    Label n = new Label(4, i+1, resTagValue3.toString());
    cell = (WritableCell) n;
    sheetToEdit.addCell(cell);

    resTagValue4= resholder.getNodeValue("//*:imei")
    Label o = new Label(5, i+1, resTagValue4.toString());
    cell = (WritableCell) o;
    sheetToEdit.addCell(cell);

    resTagValue5= resholder.getNodeValue("//*:handsetMake")
    Label p = new Label(6, i+1, resTagValue5.toString());
    cell = (WritableCell) p;
    sheetToEdit.addCell(cell);

    resTagValue6= resholder.getNodeValue("//*:handsetModel")
    Label q = new Label(7, i+1, resTagValue6.toString());
    cell = (WritableCell) q;
    sheetToEdit.addCell(cell);

    resTagValue7= resholder.getNodeValue("//*:insurancePolicyName")
    Label r = new Label(8, i+1, resTagValue7.toString());
    cell = (WritableCell) r;
    sheetToEdit.addCell(cell);

    resTagValue8= resholder.getNodeValue("//*:insuranceStartTimestamp")
    Label s = new Label(9, i+1, resTagValue8.toString());
    cell = (WritableCell) s;
    sheetToEdit.addCell(cell);

    resTagValue9= resholder.getNodeValue("//*:insuranceEndTimestamp")
    Label t = new Label(10, i+1, resTagValue9.toString());
    cell = (WritableCell) t;
    sheetToEdit.addCell(cell);
    }
}
catch (Exception e) {log.info(e)}
finally{
     workbookCopy.write();
 workbookCopy.close();
 existingWorkbook.close();
}
log.info("Testing Over")