Skip to content

Commit

Permalink
Merge pull request #484 from jmrsnt/master
Browse files Browse the repository at this point in the history
Fix reading Excel file that has cells with formulas that return string
  • Loading branch information
Jolanrensen authored Oct 26, 2023
2 parents c8ae4f2 + c7f159b commit 10fc58c
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,9 @@ private fun repairNameIfRequired(
}
}

private fun Cell?.cellValue(sheetName: String): Any? =
when (this?.cellType) {
private fun Cell?.cellValue(sheetName: String): Any? {
if (this == null) return null
fun getValueFromType(type: CellType?): Any? = when (type) {
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
Expand All @@ -290,12 +291,14 @@ private fun Cell?.cellValue(sheetName: String): Any? =
}

CellType.STRING -> stringCellValue
CellType.FORMULA -> numericCellValue
CellType.FORMULA -> getValueFromType(cachedFormulaResultType)
CellType.BLANK -> stringCellValue
CellType.BOOLEAN -> booleanCellValue
CellType.ERROR -> errorCellValue
null -> null
}
return getValueFromType(cellType)
}

public fun <T> DataFrame<T>.writeExcel(
path: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,10 @@ class XlsxTest {
df.columnNames() shouldBe listOf("Sepal.Length", "Sepal.Width", "C",
"Petal.Length", "Petal.Width", "Species", "Other.Width", "Species1", "I", "Other.Width1", "Species2")
}

@Test
fun `read xlsx file that has cells with formulas that return numbers and strings`() {
val df = DataFrame.readExcel(testResource("formula_cell.xlsx"))
df.columnNames() shouldBe listOf("Number", "Greater than 5", "Multiplied by 10", "Divided by 5")
}
}
Binary file not shown.

0 comments on commit 10fc58c

Please sign in to comment.