Determine if a Spreadsheet Cell contains Numeric or Text data

While working with some text data from a spreadsheet, I needed to read different kinds of SKU data. In some cases it would be a number such as 1313, while in others it might be a mix of text and numbers such as OU812.

Reding the value is normally done with .getNumericCellValue() or .getNumericCellValue(), but what happens when you don't know what the next value would be?

To overcome that, it's possible to the use .getCellType() and adjust the read based on type. Using some previous code, reading values from a spreadsheet and populating a Global List would look like the following:

for (loop = 1; loop <=rowCount; loop++) {
    //Assign spreadsheet columns to variables
    String cellType=sheet.getRow(loop).getCell(0).getCellType()
    //Determine if the cell contains Text or Numeric data
    if (cellType=="1"){
        String cellTextValue=sheet.getRow(loop).getCell(0).getStringCellValue()
        GlobalVariable.inventorySKU[loop]=cellTextValue
    }else{
        long cellIntValue=sheet.getRow(loop).getCell(0).getNumericCellValue()
        GlobalVariable.inventorySKU[loop]=cellIntValue
    }
}

In this case, 1 is String and 0 is Integer or Numeric. Based on the length of the SKU, I used long to store the value.

Additionally, in order to use the Integer value within a form, it needs to be converted to a String using:
String.valueOf(GlobalVariable.inventorySKU[loop])) such as:

WebUI.setText(findTestObject('Object/input-SKU'), String.valueOf(GlobalVariable.inventorySKU[loop]))

Apache POI – Cells

Other articles of interest:

Leave a Reply

Your email address will not be published. Required fields are marked *

Recent Comments