Skip to content
Closed
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
9 changes: 9 additions & 0 deletions src/main/groovy/org/jggug/kobo/gexcelapi/GExcel.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,15 @@ class GExcel {
}
return null
}
findEmptyRow { label ->
Row targetRow = delegate.find { Row row ->
row.rowNum >= CLU.rowIndex(label) && row.getCell(CLU.columnIndex(label))?.value == null }
if (!targetRow) {
return delegate.createRow(delegate.lastRowNum+1)
}

return targetRow
}
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

テストをみても期待動作というか仕様がちょっと理解できてないのですが、「指定したセルが空である最初の行を探す」メソッドなんでしょうか?

あと、探索的メソッドの仕様を記述するテストとして、「編集」視点でのGExcelEditTestである理由がちょっとわかりませんでした。

}
}

Expand Down
33 changes: 33 additions & 0 deletions src/test/groovy/org/jggug/kobo/gexcelapi/GExcelTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,17 @@ package org.jggug.kobo.gexcelapi
class GExcelTest extends GroovyTestCase {

def sampleFile = "build/resources/test/sample.xls"
def outputPath = "output.xls"
def outputFile
def book
def sheet
def sheet4

void setUp() {
book = GExcel.open(sampleFile)
sheet = book[0]
sheet4 = book[3]
outputFile = new File(outputPath)

// dateCellValue is affected by TimeZone.
// So it should be explicitly set as GMT in order to avoid causing a failure dependent on an environment.
Expand All @@ -34,6 +39,8 @@ class GExcelTest extends GroovyTestCase {
void tearDown() {
book = null
sheet = null
sheet4 = null
outputFile.delete()
}

void testOpen() {
Expand Down Expand Up @@ -294,5 +301,31 @@ class GExcelTest extends GroovyTestCase {
assert sheet.getEnclosingMergedRegion(sheet.A4).isFirstCell(sheet.A5) == false
assert sheet.getEnclosingMergedRegion(sheet.A4).isFirstCell(sheet.B5) == false
}

void testFindEmptyRowFormulaValueColumn() {
def emptyRow = sheet4.findEmptyRow('A2');
assert emptyRow != null
assert emptyRow.rowNum == 10 // rowNum start 0
assert emptyRow.getCell(0)?.value == null
}

void testFindEmptyRowValueColumn() {
def emptyRow = sheet4.findEmptyRow('B3');
assert emptyRow != null
assert emptyRow.rowNum == 8 // rowNum start 0
assert emptyRow.getCell(1)?.value == null
}

void testAddRow() {
def emptyRow = sheet4.findEmptyRow('A2');
emptyRow.createCell(0).value = "100"
emptyRow.createCell(1).value = "test"

outputFile.withOutputStream { book.write(it) }
def targetBook = GExcel.open(outputFile)
def targetSheet = targetBook[3]
assert targetSheet.A11.value == "100"
assert targetSheet.B11.value == "test"
}
}

Binary file modified src/test/resources/sample.xls
Binary file not shown.