Skip to content

Commit

Permalink
セルの指定方法を配列のように変更
Browse files Browse the repository at this point in the history
  • Loading branch information
webarata3 committed Nov 8, 2015
1 parent 63d3c46 commit 2261e84
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 51 deletions.
33 changes: 21 additions & 12 deletions src/main/kotlin/link/arata/kexcelapi/KExcel.kt
Expand Up @@ -44,24 +44,24 @@ public class KExcel {
}
}

public operator fun Sheet.invoke(n: Int): Row {
public operator fun Sheet.get(n: Int): Row {
return getRow(n) ?: createRow(n)
}

public operator fun Row.invoke(n: Int): Cell {
public operator fun Row.get(n: Int): Cell {
return getCell(n) ?: createCell(n, Cell.CELL_TYPE_BLANK)
}

public operator fun Sheet.invoke(x: Int, y: Int): Cell {
var row = this(y)
return row(x)
public operator fun Sheet.get(x: Int, y: Int): Cell {
var row = this[y]
return row[x]
}

private val ORIGIN = 'A'.toInt()
private val RADIX = 26

// https://github.com/nobeans/gexcelapi/blob/master/src/main/groovy/org/jggug/kobo/gexcelapi/GExcel.groovy
public operator fun Sheet.invoke(cellLabel: String): Cell {
public operator fun Sheet.get(cellLabel: String): Cell {
val p1 = Pattern.compile("([a-zA-Z]+)([0-9]+)");
val matcher = p1.matcher(cellLabel)
matcher.find()
Expand All @@ -73,10 +73,10 @@ public operator fun Sheet.invoke(cellLabel: String): Cell {
num += delta * Math.pow(RADIX.toDouble(), i.toDouble()).toInt()
}
num -= 1
return this(num, matcher.group(2).toInt() - 1)
return this[num, matcher.group(2).toInt() - 1]
}

public fun Cell.getString(): String {
public fun Cell.toStr(): String {
when (cellType) {
Cell.CELL_TYPE_STRING -> return stringCellValue
Cell.CELL_TYPE_NUMERIC -> return numericCellValue.toString()
Expand All @@ -95,7 +95,7 @@ public fun Cell.getString(): String {
}
}

public fun Cell.getInt(): Int {
public fun Cell.toInt(): Int {
fun stringToInt(value: String): Int {
try {
// toIntだと44.5のような文字列を44に変換できないため、一度Dobuleに変換している
Expand All @@ -120,7 +120,7 @@ public fun Cell.getInt(): Int {
}
}

public fun Cell.getDouble(): Double {
public fun Cell.toDouble(): Double {
fun stringToDouble(value: String): Double {
try {
return value.toDouble()
Expand All @@ -144,7 +144,7 @@ public fun Cell.getDouble(): Double {
}
}

public fun Cell.getDate(): Date {
public fun Cell.toDate(): Date {
when (cellType) {
Cell.CELL_TYPE_NUMERIC -> return dateCellValue
Cell.CELL_TYPE_FORMULA -> {
Expand All @@ -165,7 +165,16 @@ private fun getFormulaCellValue(cell: Cell): CellValue {
return evaluator.evaluate(cell)
}

public infix fun Cell.value(value: Any) {
public operator fun Sheet.set(cellLabel: String, value: Any) {
this[cellLabel].setValue(value)
}


public operator fun Sheet.set(x: Int, y: Int, value: Any) {
this[x, y].setValue(value)
}

public fun Cell.setValue(value: Any) {
when (value) {
is String -> setCellValue(value)
is Int -> setCellValue(value.toDouble())
Expand Down
118 changes: 79 additions & 39 deletions src/test/kotlin/link/arata/kexcelapi/KExcelTest.kt
Expand Up @@ -41,22 +41,22 @@ class KExcelTest() {
KExcel.open("$BASE_DIR/book1.xlsx").use { workbook ->
val sheet = workbook.getSheetAt(0)

assertThat(sheet("A1").getString(), IS(""))
assertThat(sheet("B2").getString(), IS(""))
assertThat(sheet("C3").getString(), IS(""))
assertThat(sheet["A1"].toString(), IS(""))
assertThat(sheet["B2"].toString(), IS(""))
assertThat(sheet["C3"].toString(), IS(""))

assertThat(sheet("A6").getInt(), IS(1))
assertThat(sheet("B6").getInt(), IS(2))
assertThat(sheet("C6").getDouble(), IS(3.0))
assertThat(sheet["A6"].toInt(), IS(1))
assertThat(sheet["B6"].toInt(), IS(2))
assertThat(sheet["C6"].toDouble(), IS(3.0))

assertThat(sheet("A7").getDouble(), IS(1.5))
assertThat(sheet("B7").getDouble(), IS(2.5))
assertThat(sheet("C7").getInt(), IS(3))
assertThat(sheet["A7"].toDouble(), IS(1.5))
assertThat(sheet["B7"].toDouble(), IS(2.5))
assertThat(sheet["C7"].toInt(), IS(3))

val sdf = SimpleDateFormat("yyyy/MM/dd HH:mm")
assertThat(sdf.format(sheet("A8").getDate()), IS("2015/01/03 08:15"))
assertThat(sdf.format(sheet("B8").getDate()), IS("1899/12/31 11:27"))
assertThat(sdf.format(sheet("C8").getDate()), IS("2015/10/01 00:00"))
assertThat(sdf.format(sheet["A8"].toDate()), IS("2015/01/03 08:15"))
assertThat(sdf.format(sheet["B8"].toDate()), IS("1899/12/31 11:27"))
assertThat(sdf.format(sheet["C8"].toDate()), IS("2015/10/01 00:00"))
}
}

Expand All @@ -65,22 +65,22 @@ class KExcelTest() {
KExcel.open("$BASE_DIR/book1.xlsx").use { workbook ->
val sheet = workbook.getSheetAt(0)

assertThat(sheet(0, 0).getString(), IS(""))
assertThat(sheet(1, 1).getString(), IS(""))
assertThat(sheet(2, 2).getString(), IS(""))
assertThat(sheet[0, 0].toString(), IS(""))
assertThat(sheet[1, 1].toString(), IS(""))
assertThat(sheet[2, 2].toString(), IS(""))

assertThat(sheet(0, 5).getInt(), IS(1))
assertThat(sheet(1, 5).getInt(), IS(2))
assertThat(sheet(2, 5).getDouble(), IS(3.0))
assertThat(sheet[0, 5].toInt(), IS(1))
assertThat(sheet[1, 5].toInt(), IS(2))
assertThat(sheet[2, 5].toDouble(), IS(3.0))

assertThat(sheet(0, 6).getDouble(), IS(1.5))
assertThat(sheet(1, 6).getDouble(), IS(2.5))
assertThat(sheet(2, 6).getInt(), IS(3))
assertThat(sheet[0, 6].toDouble(), IS(1.5))
assertThat(sheet[1, 6].toDouble(), IS(2.5))
assertThat(sheet[2, 6].toInt(), IS(3))

val sdf = SimpleDateFormat("yyyy/MM/dd HH:mm")
assertThat(sdf.format(sheet(0, 7).getDate()), IS("2015/01/03 08:15"))
assertThat(sdf.format(sheet(1, 7).getDate()), IS("1899/12/31 11:27"))
assertThat(sdf.format(sheet(2, 7).getDate()), IS("2015/10/01 00:00"))
assertThat(sdf.format(sheet[0, 7].toDate()), IS("2015/01/03 08:15"))
assertThat(sdf.format(sheet[1, 7].toDate()), IS("1899/12/31 11:27"))
assertThat(sdf.format(sheet[2, 7].toDate()), IS("2015/10/01 00:00"))
}
}

Expand All @@ -89,10 +89,10 @@ class KExcelTest() {
KExcel.open("$BASE_DIR/book1.xlsx").use { workbook ->
val sheet = workbook.getSheetAt(1)

assertThat(sheet("A1").getString(), IS("a"))
assertThat(sheet("B1").getString(), IS("3.0"))
assertThat(sheet("C1").getString(), IS("true"))
assertThat(sheet("D1").getString(), IS(""))
assertThat(sheet["A1"].toStr(), IS("a"))
assertThat(sheet["B1"].toStr(), IS("3.0"))
assertThat(sheet["C1"].toStr(), IS("true"))
assertThat(sheet["D1"].toStr(), IS(""))
}
}

Expand All @@ -101,8 +101,8 @@ class KExcelTest() {
KExcel.open("$BASE_DIR/book1.xlsx").use { workbook ->
val sheet = workbook.getSheetAt(1)

assertThat(sheet("A2").getInt(), IS(33))
assertThat(sheet("B2").getInt(), IS(5))
assertThat(sheet["A2"].toInt(), IS(33))
assertThat(sheet["B2"].toInt(), IS(5))
}
}

Expand All @@ -111,8 +111,8 @@ class KExcelTest() {
KExcel.open("$BASE_DIR/book1.xlsx").use { workbook ->
val sheet = workbook.getSheetAt(1)

assertThat(sheet("A3").getInt(), IS(44))
assertThat(sheet("B3").getInt(), IS(33))
assertThat(sheet["A3"].toInt(), IS(44))
assertThat(sheet["B3"].toInt(), IS(33))
}
}

Expand All @@ -121,8 +121,8 @@ class KExcelTest() {
KExcel.open("$BASE_DIR/book1.xlsx").use { workbook ->
val sheet = workbook.getSheetAt(1)

assertThat(sheet("A3").getDouble(), IS(closeTo(44.5, 44.5)))
assertThat(sheet("B3").getDouble(), IS(closeTo(33.29, 33.31)))
assertThat(sheet["A3"].toDouble(), IS(closeTo(44.5, 44.5)))
assertThat(sheet["B3"].toDouble(), IS(closeTo(33.29, 33.31)))
}
}

Expand All @@ -132,7 +132,7 @@ class KExcelTest() {
val sheet = workbook.getSheetAt(1)

val sdf = SimpleDateFormat("yyyy/MM/dd")
assertThat(sdf.format(sheet("B4").getDate()), IS("2015/05/01"))
assertThat(sdf.format(sheet["B4"].toDate()), IS("2015/05/01"))
}
}

Expand All @@ -141,17 +141,57 @@ class KExcelTest() {
KExcel.open("$BASE_DIR/book1.xlsx").use { workbook ->
val sheet = workbook.getSheetAt(0)

sheet("A1") value 100
sheet("A2") value "あいうえお"
sheet["A1"].setValue(100)
sheet["A2"].setValue("あいうえお")

KExcel.write(workbook, "$BASE_DIR/book2.xlsx")
}

KExcel.open("$BASE_DIR/book2.xlsx").use { workbook ->
val sheet = workbook.getSheetAt(0)

assertThat(sheet("A1").getInt(), IS(100))
assertThat(sheet("A2").getString(), IS("あいうえお"))
assertThat(sheet["A1"].toInt(), IS(100))
assertThat(sheet["A2"].toString(), IS("あいうえお"))
}
Files.delete(Paths.get("$BASE_DIR/book2.xlsx"))
}

@Test
fun シートのラベルからの書き込みテスト() {
KExcel.open("$BASE_DIR/book1.xlsx").use { workbook ->
val sheet = workbook.getSheetAt(0)

sheet["A1"] = 100
sheet["A2"] = "あいうえお"

KExcel.write(workbook, "$BASE_DIR/book2.xlsx")
}

KExcel.open("$BASE_DIR/book2.xlsx").use { workbook ->
val sheet = workbook.getSheetAt(0)

assertThat(sheet["A1"].toInt(), IS(100))
assertThat(sheet["A2"].toString(), IS("あいうえお"))
}
Files.delete(Paths.get("$BASE_DIR/book2.xlsx"))
}

@Test
fun シートのインデックスからの書き込みテスト() {
KExcel.open("$BASE_DIR/book1.xlsx").use { workbook ->
val sheet = workbook.getSheetAt(0)

sheet[0, 0] = 100
sheet[0, 1] = "あいうえお"

KExcel.write(workbook, "$BASE_DIR/book2.xlsx")
}

KExcel.open("$BASE_DIR/book2.xlsx").use { workbook ->
val sheet = workbook.getSheetAt(0)

assertThat(sheet["A1"].toInt(), IS(100))
assertThat(sheet["A2"].toString(), IS("あいうえお"))
}
Files.delete(Paths.get("$BASE_DIR/book2.xlsx"))
}
Expand Down
Binary file modified src/test/resources/link/arata/kexcelapi/book1.xlsx
Binary file not shown.

0 comments on commit 2261e84

Please sign in to comment.