-
Notifications
You must be signed in to change notification settings - Fork 7
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
frepe2013
wants to merge
1
commit into
nobeans:master
Choose a base branch
from
frepe2013:add-feature-findRow
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
} | ||
|
||
return targetRow | ||
} | ||
findByCellValue { label, cellValue -> | ||
def condition = createCondition(label, cellValue) | ||
delegate.find(condition) | ||
} | ||
findAllByCellValue { label, cellValue -> | ||
def condition = createCondition(label, cellValue) | ||
delegate.findAll(condition) | ||
} | ||
} | ||
} | ||
|
||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
|
@@ -34,6 +39,8 @@ class GExcelTest extends GroovyTestCase { | |
void tearDown() { | ||
book = null | ||
sheet = null | ||
sheetForFindRowTest = null | ||
outputFile.delete() | ||
} | ||
|
||
void testOpen() { | ||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. もしかして、ここで渡している
という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 not shown.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
要求が「何行目以降で1行まるまる空である空行が欲しい」であればまだ何とか納得できますが、「何行目以降でこの列が空である空行が欲しい」に対して「対象がなかったので最後尾の行を返します」は、ちょっと違和感があります。
最後尾に行が欲しければ、L85のように
で簡単に手に入りますし、特に専用APIを用意してあげる必要もない感じがします。
上のコードが何度も頻出するとして、せいぜい
としてあげるくらい(これもあまり必要性を感じてないですが)。