diff --git a/src/main/groovy/org/jggug/kobo/gexcelapi/GExcel.groovy b/src/main/groovy/org/jggug/kobo/gexcelapi/GExcel.groovy index ca14922..4ece271 100644 --- a/src/main/groovy/org/jggug/kobo/gexcelapi/GExcel.groovy +++ b/src/main/groovy/org/jggug/kobo/gexcelapi/GExcel.groovy @@ -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 + } } } diff --git a/src/test/groovy/org/jggug/kobo/gexcelapi/GExcelTest.groovy b/src/test/groovy/org/jggug/kobo/gexcelapi/GExcelTest.groovy index a04b2aa..aa62d88 100644 --- a/src/test/groovy/org/jggug/kobo/gexcelapi/GExcelTest.groovy +++ b/src/test/groovy/org/jggug/kobo/gexcelapi/GExcelTest.groovy @@ -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. @@ -34,6 +39,8 @@ class GExcelTest extends GroovyTestCase { void tearDown() { book = null sheet = null + sheet4 = null + outputFile.delete() } void testOpen() { @@ -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" + } } diff --git a/src/test/resources/sample.xls b/src/test/resources/sample.xls index 80cd780..9d1b52a 100644 Binary files a/src/test/resources/sample.xls and b/src/test/resources/sample.xls differ