Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix reading Excel file that has cells with formulas that return string #484

Merged
merged 3 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.