https://bz.apache.org/bugzilla/show_bug.cgi?id=65916

--- Comment #10 from moimoi.chk <moimoi....@gmail.com> ---
↑The comment up is a mistake.
The correct answer is that this problem occurs when "the iterator deletes even
one cell".

=================
OK case
Remove empty row
=================

fun clearAll(sheet: Sheet) {
    val itr = sheet.rowIterator()

    while (itr.hasNext()) {
        val row = itr.next()
        if (row.rowNum >= 4) {
            clearRow(row)
            itr.remove()// remove empty row
        }
    }
}

fun clearRow(row: Row) {
    val itr = row.cellIterator()
    while (itr.hasNext()) {
        itr.next()
        itr.remove()
    }
}


=================
NG case 1
Leave at least one cell in the row
=================
fun clearAll(sheet: Sheet) {
    val itr = sheet.rowIterator()

    while (itr.hasNext()) {
        val row = itr.next()
        if (row.rowNum >= 4) {
            if (clearRow(row)) {
                itr.remove()// remove empty row
            }
        }
    }
}

fun clearRow(row: Row): Boolean {
    //  count cell
    var itr = row.cellIterator()
    var cellCount = 0
    while (itr.hasNext()) {
        itr.next()
        cellCount++
    }

    if (cellCount == 0) {
        return true
    }

    //  leave one cell and delete the rest
    val keepTarget = Random().nextInt(cellCount)

    itr = row.cellIterator()
    var i = 0
    while (itr.hasNext()) {
        itr.next()
        if (i++ != keepTarget) {
            itr.remove()
            cellCount--
        }
    }

    assert(cellCount == 1)
    return false
}

=================
NG case 2
Delete only one cell at most
=================
fun clearAll(sheet: Sheet) {
    val itr = sheet.rowIterator()

    while (itr.hasNext()) {
        val row = itr.next()
        if (row.rowNum >= 4) {
            clearRow(row)
        }
    }
}

fun clearRow(row: Row) {
    val itr = row.cellIterator()
    if (itr.hasNext()) {
        itr.next()
        itr.remove()
    }
}

-- 
You are receiving this mail because:
You are the assignee for the bug.
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@poi.apache.org
For additional commands, e-mail: dev-h...@poi.apache.org

Reply via email to