Skip to content

Error: Cannot get a NUMERIC value from a STRING cell #483

Closed
@jmrsnt

Description

@jmrsnt

There is an error when reading an xlsx file that has cells that have formulas that return string. The file formula_cell.xlsx was used as an example.

import org.jetbrains.kotlinx.dataframe.DataFrame
import org.jetbrains.kotlinx.dataframe.io.*
import org.jetbrains.kotlinx.dataframe.api.*

class MyApp {
    fun run() {
        val path = this.javaClass.getResource("formula_cell.xlsx")!!
        val df = DataFrame.readExcel(path)

        df.print()
    }
}

fun main(args: Array<String>) {
    MyApp().run()
}

This produces the error below:


Exception in thread "main" java.lang.IllegalStateException: Cannot get a NUMERIC value from a STRING cell
	at org.apache.poi.xssf.usermodel.XSSFCell.typeMismatch(XSSFCell.java:946)
	at org.apache.poi.xssf.usermodel.XSSFCell.getNumericCellValue(XSSFCell.java:260)
	at org.jetbrains.kotlinx.dataframe.io.XlsxKt.cellValue(xlsx.kt:293)
	at org.jetbrains.kotlinx.dataframe.io.XlsxKt.readExcel(xlsx.kt:242)
	at org.jetbrains.kotlinx.dataframe.io.XlsxKt.readExcel(xlsx.kt:182)
	at org.jetbrains.kotlinx.dataframe.io.XlsxKt.readExcel(xlsx.kt:101)
	at org.jetbrains.kotlinx.dataframe.io.XlsxKt.readExcel$default(xlsx.kt:91)
	at MyApp.run(Main.kt:8)
	at MainKt.main(Main.kt:15)

This occurs because when reading the cell value, if it is a formula, the value is being returned as stringCellValue, at xlsx.kt:293

private fun Cell?.cellValue(sheetName: String): Any? =
when (this?.cellType) {
CellType._NONE -> error("Cell $address of sheet $sheetName has a CellType that should only be used internally. This is a bug, please report https://github.com/Kotlin/dataframe/issues")
CellType.NUMERIC -> {
val number = numericCellValue
when {
DateUtil.isCellDateFormatted(this) -> DateUtil.getLocalDateTime(number).toKotlinLocalDateTime()
else -> number
}
}
CellType.STRING -> stringCellValue
CellType.FORMULA -> numericCellValue
CellType.BLANK -> stringCellValue
CellType.BOOLEAN -> booleanCellValue
CellType.ERROR -> errorCellValue
null -> null
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    good first issueGood issues to pick-up for newcomers

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions