blob: adbf7e542698d6b1740f6327a44c51214833838e [file] [log] [blame]
/*
* Copyright 2019 WeBank
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.webank.wedatasphere.spark
import org.apache.poi.ss.usermodel.Row.MissingCellPolicy
import org.apache.poi.ss.usermodel.{Cell, Row}
package object excel {
implicit class RichRow(val row: Row) extends AnyVal {
def eachCellIterator(startColumn: Int, endColumn: Int): Iterator[Option[Cell]] = new Iterator[Option[Cell]] {
private val lastCellInclusive = row.getLastCellNum - 1
private val endCol = Math.min(endColumn, Math.max(startColumn, lastCellInclusive))
require(startColumn >= 0 && startColumn <= endCol)
private var nextCol = startColumn
override def hasNext: Boolean = nextCol <= endCol && nextCol <= lastCellInclusive
override def next(): Option[Cell] = {
val next =
if (nextCol > endCol) throw new NoSuchElementException(s"column index = $nextCol")
else Option(row.getCell(nextCol, MissingCellPolicy.RETURN_NULL_AND_BLANK))
nextCol += 1
next
}
}
}
}