Читайте контент Excel после получения данных из определенной ячейки - PullRequest
0 голосов
/ 06 февраля 2020

У меня есть существующие коды, которые работают, чтобы получить конкретную ячейку из листа Excel. Можно ли прочитать определенное содержимое в ячейке?

Excel Reader:

public String ReadCellData(int vRow, int vColumn)
	{
		String value=null;          //variable for storing the cell value
		Workbook wb=null;           //initialize Workbook null
		try
		{
			//reading data from a file in the form of bytes
      FileInputStream fis=new FileInputStream(RunConfiguration.getProjectDir() + "/Data Files/testmatrix.xlsx");
			//constructs an XSSFWorkbook object, by buffering the whole stream into the memory
			wb=new XSSFWorkbook(fis);
		}
		catch(FileNotFoundException e)
		{
			e.printStackTrace();
		}
		catch(IOException e1)
		{
			e1.printStackTrace();
		}
		Sheet sheet=wb.getSheetAt(0);   //getting the XSSFSheet object at given index
		Row row=sheet.getRow(vRow); //returns the logical row
		Cell cell=row.getCell(vColumn); //getting the cell representing the given column
		//value=cell.getStringCellValue();    //getting cell value
		//return value;               //returns the cell value
	}
}

коды для получения и печати данных

//read excel cell C11, ignore first row column A1. (int vRow, int vColumn)
def exceldata = CustomKeywords.'test.readexcel.ReadCellData'(11, 2)

String jsondata = JsonOutput.prettyPrint(exceldata.toString())

println(jsondata)

WebUI.delay(1)

//call the object and post the above data as API HTTP request body
def post = ((findTestObject('Object Repository/Web Service Request/test-service/Post')) as RequestObject)

post.setBodyContent(new HttpTextBodyContent(jsondata))

WebUI.delay(2)

//POST and verification
def response = WS.sendRequestAndVerify(post)

println(response.statusCode)

assert response.getStatusCode() == 201

Данные Excel в ячейке C11

Я хочу получить значение ключа testId.

{
  "testId": "test123",
  "created": "2020-02-06T17:02:39.257Z",
}

1 Ответ

0 голосов
/ 06 февраля 2020

Вам нужно будет разобрать ответ:

def parsedResponse = new JsonSlurper().parseText(response.getResponseText())
println parsedResponse.get("testId")

(не забудьте import groovy.json.JsonSlurper)

...