You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -148,7 +149,7 @@ public fun DataFrame.Companion.readExcel(
148
149
skipRows:Int = 0,
149
150
rowsCount:Int? = null
150
151
): AnyFrame {
151
-
val columnIndexes =if (columns !=null) {
152
+
val columnIndexes:Iterable<Int>=if (columns !=null) {
152
153
columns.split(",").flatMap {
153
154
if (it.contains(":")) {
154
155
val (start, end) = it.split(":").map { CellReference.convertColStringToIndex(it) }
@@ -158,42 +159,58 @@ public fun DataFrame.Companion.readExcel(
158
159
}
159
160
}
160
161
} else {
161
-
sheet.getRow(skipRows).map { it.columnIndex }
162
+
val headerRow = checkNotNull(sheet.getRow(skipRows)) {
163
+
"Row number ${skipRows +1} (1-based index) is not defined on the sheet ${sheet.sheetName}"
164
+
}
165
+
val firstCellNum = headerRow.firstCellNum
166
+
check(firstCellNum != (-1).toShort()) {
167
+
"There are no defined cells on header row number ${skipRows +1} (1-based index). Pass `columns` argument to specify what columns to read or make sure the index is correct"
168
+
}
169
+
headerRow.firstCellNum until headerRow.lastCellNum
162
170
}
163
171
164
-
val headerRow = sheet.getRow(skipRows)
165
-
val valueRows = sheet.drop(1+ skipRows).let { if (rowsCount !=null) it.take(rowsCount) else it }
172
+
val headerRow:Row?= sheet.getRow(skipRows)
173
+
val first = skipRows +1
174
+
val last = rowsCount?.let { first + it -1 } ?: sheet.lastRowNum
175
+
val valueRowsRange = (first..last)
176
+
166
177
val columns = columnIndexes.map { index ->
167
-
val headerCell = headerRow.getCell(index)
178
+
val headerCell = headerRow?.getCell(index)
168
179
val name =if (headerCell?.cellType ==CellType.NUMERIC) {
169
180
headerCell.numericCellValue.toString() // Support numeric-named columns
170
181
} else {
171
182
headerCell?.stringCellValue ?:CellReference.convertNumToColString(index) // Use Excel column names if no data
172
183
}
173
-
val values:List<Any?> = valueRows.map {
174
-
val cell:Cell?= it.getCell(index)
175
-
when (cell?.cellType) {
176
-
CellType._NONE-> error("Cell ${cell.address} of sheet ${sheet.sheetName} has a CellType that should only be used internally. This is a bug, please report https://github.com/Kotlin/dataframe/issues")
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")
0 commit comments