Skip to content

Commit 10fc58c

Browse files
authored
Merge pull request #484 from jmrsnt/master
Fix reading Excel file that has cells with formulas that return string
2 parents c8ae4f2 + c7f159b commit 10fc58c

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

dataframe-excel/src/main/kotlin/org/jetbrains/kotlinx/dataframe/io/xlsx.kt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -278,8 +278,9 @@ private fun repairNameIfRequired(
278278
}
279279
}
280280

281-
private fun Cell?.cellValue(sheetName: String): Any? =
282-
when (this?.cellType) {
281+
private fun Cell?.cellValue(sheetName: String): Any? {
282+
if (this == null) return null
283+
fun getValueFromType(type: CellType?): Any? = when (type) {
283284
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")
284285
CellType.NUMERIC -> {
285286
val number = numericCellValue
@@ -290,12 +291,14 @@ private fun Cell?.cellValue(sheetName: String): Any? =
290291
}
291292

292293
CellType.STRING -> stringCellValue
293-
CellType.FORMULA -> numericCellValue
294+
CellType.FORMULA -> getValueFromType(cachedFormulaResultType)
294295
CellType.BLANK -> stringCellValue
295296
CellType.BOOLEAN -> booleanCellValue
296297
CellType.ERROR -> errorCellValue
297298
null -> null
298299
}
300+
return getValueFromType(cellType)
301+
}
299302

300303
public fun <T> DataFrame<T>.writeExcel(
301304
path: String,

dataframe-excel/src/test/kotlin/org/jetbrains/kotlinx/dataframe/io/XlsxTest.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,4 +121,10 @@ class XlsxTest {
121121
df.columnNames() shouldBe listOf("Sepal.Length", "Sepal.Width", "C",
122122
"Petal.Length", "Petal.Width", "Species", "Other.Width", "Species1", "I", "Other.Width1", "Species2")
123123
}
124+
125+
@Test
126+
fun `read xlsx file that has cells with formulas that return numbers and strings`() {
127+
val df = DataFrame.readExcel(testResource("formula_cell.xlsx"))
128+
df.columnNames() shouldBe listOf("Number", "Greater than 5", "Multiplied by 10", "Divided by 5")
129+
}
124130
}
Binary file not shown.

0 commit comments

Comments
 (0)