Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added method to find rows that match the condition #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
61 changes: 61 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,23 @@ 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)
Copy link
Owner

Choose a reason for hiding this comment

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

要求が「何行目以降で1行まるまる空である空行が欲しい」であればまだ何とか納得できますが、「何行目以降でこの列が空である空行が欲しい」に対して「対象がなかったので最後尾の行を返します」は、ちょっと違和感があります。

最後尾に行が欲しければ、L85のように

def newRow = sheet.createRow(sheet.lastRowNum + 1)

で簡単に手に入りますし、特に専用APIを用意してあげる必要もない感じがします。
上のコードが何度も頻出するとして、せいぜい

def newRow = sheet.newLastRow()

としてあげるくらい(これもあまり必要性を感じてないですが)。

}

return targetRow
}
findByCellValue { label, cellValue ->
def condition = createCondition(label, cellValue)
delegate.find(condition)
}
findAllByCellValue { label, cellValue ->
def condition = createCondition(label, cellValue)
delegate.findAll(condition)
}
}
}

Expand Down Expand Up @@ -156,6 +173,50 @@ class GExcel {
}
}

private static createCondition(String label, String cellValue) {
int cellIndex = CLU.columnIndex(label)
def condition = { Row row ->
if (row.rowNum < CLU.rowIndex(label)) {
return false
}

Cell cell = row.getCell(cellIndex)
if (cell == null) {
return false
}
if (cell.value == null) {
return false
}
if (!cell.isStringType()) {
return false
}
cell.value.contains(cellValue)
Copy link
Owner

Choose a reason for hiding this comment

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

実用的な意味としては理解できますが、文字列の場合だけ暗黙で部分一致、というのもちょっと違和感がありますね。

}
return condition
}

private static createCondition(String label, Integer cellValue) {
int cellIndex = CLU.columnIndex(label)
def condition = { Row row ->
if (row.rowNum < CLU.rowIndex(label)) {
return false
}

Cell cell = row.getCell(cellIndex)
if (cell == null) {
return false
}
if (cell.value == null) {
return false
}
if (!cell.isNumericType()) {
return false
}
cell.value == cellValue
}
return condition
}

private static getRowFromSheetByLabel(sheet, label) {
int rowIndex = CLU.rowIndex(label)
def row = sheet.getRow(rowIndex)
Expand Down
89 changes: 89 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 sheetForFindRowTest

void setUp() {
book = GExcel.open(sampleFile)
sheet = book[0]
sheetForFindRowTest = 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
sheetForFindRowTest = null
outputFile.delete()
}

void testOpen() {
Expand Down Expand Up @@ -294,5 +301,87 @@ 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 = sheetForFindRowTest.findEmptyRow('A2');
assert emptyRow != null
assert emptyRow.rowNum == 10 // rowNum start 0
assert emptyRow.getCell(0)?.value == null
Copy link
Owner

Choose a reason for hiding this comment

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

もしかして、ここで渡しているA2というラベルの使われ方として、

  • "2"行目以降の
  • "A"列が空の行を探す

という2つの意味を持たせていますか?

}

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

void testAddRow() {
def emptyRow = sheetForFindRowTest.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"
}

void testFindByCellValue() {
def resultRow = sheetForFindRowTest.findByCellValue('F4', 'Ken');
assert resultRow != null
assert resultRow.label == "7" // label equals line number on excel
assert resultRow.F_.value == "Ken"
assert resultRow.getCell(5) == sheetForFindRowTest.F7
}

void testFindByCellValuePrefixMatch() {
def resultRow = sheetForFindRowTest.findByCellValue('C4', 'Add');
assert resultRow != null
assert resultRow.label == "4" // label equals line number on excel
assert resultRow.C_.value == "Add Feature"
assert resultRow.getCell(2) == sheetForFindRowTest.C4
}

void testFindByCellValueSuffixMatch() {
def resultRow = sheetForFindRowTest.findByCellValue('H6', 'ed');
assert resultRow != null
assert resultRow.label == "6" // label equals line number on excel
assert resultRow.H_.value == "Closed"
assert resultRow.getCell(7) == sheetForFindRowTest.H6
}

void testFindByCellValueNumberMatch() {
def resultRow = sheetForFindRowTest.findByCellValue('I4', 20);
assert resultRow != null
assert resultRow.label == "5" // label equals line number on excel
assert resultRow.I_.value == 20
assert resultRow.getCell(8) == sheetForFindRowTest.I5
}

void testFindAllByCellValue() {
def resultRows = sheetForFindRowTest.findAllByCellValue('D4', 'B');
assert resultRows[0].label == "5"
assert resultRows[0].D_.value == "B"
assert resultRows[1].label == "6"
assert resultRows[1].D_.value == "B"
}

void testFindAllByCellValuePrefixMatch() {
def resultRows = sheetForFindRowTest.findAllByCellValue('C4', 'Spec');
assert resultRows[0].label == "5"
assert resultRows[0].C_.value == "Spec Change"
assert resultRows[1].label == "7"
assert resultRows[1].C_.value == "Spec Change"
}

void testFindAllByCellValueNumberMatch() {
def resultRows = sheetForFindRowTest.findAllByCellValue('I4', 10);
assert resultRows[0].label == "7"
assert resultRows[0].I_.value == 10
assert resultRows[1].label == "8"
assert resultRows[1].I_.value == 10
}
}

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