Changeset 6890
- Timestamp:
- 02/19/08 01:17:36 (5 years ago)
- Location:
- lang/scala/sandbox/src/jp/ryugate/apache
- Files:
-
- 2 modified
-
POI.scala (modified) (7 diffs)
-
POISpecification.scala (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
lang/scala/sandbox/src/jp/ryugate/apache/POI.scala
r6747 r6890 2 2 3 3 /** 4 * POI wrapper 4 * POI wrapper 5 * 2008/2/18 5 6 */ 6 7 object POI { … … 14 15 import org.apache.poi.hssf.usermodel.HSSFCell._ 15 16 16 private def gets[T](f:int => T, n:int*)17 private def makeSomeList[T](f:int => T, n:int*) 17 18 = n.foldLeft(List[T]()){(acc, i) => f(i) :: acc}.reverse 18 private def eachWithIndex[T](f:(int*) => List[T], f2:(T,int) => Unit, arg:int*) 19 = f(arg:_*).zipWithIndex.foreach{case (row,idx) => f2(row,idx)} 20 19 20 private def eachWithIndex[T](lst:List[T], f2:(T,int) => Unit) 21 = lst.zipWithIndex.foreach{case (row,idx) => f2(row,idx)} 22 21 23 /** 22 24 * Book 23 25 */ 24 class Book( book:HSSFWorkbook) {26 class Book(hssf_book:HSSFWorkbook) { 25 27 def this() = this(new HSSFWorkbook()) 26 28 def this(is:InputStream) = this(new HSSFWorkbook(is)) 27 29 def this(ist:FileInputStream) = this(new HSSFWorkbook(ist)) 28 30 def this(filename:String) = this(new FileInputStream(filename)) 29 30 def getSheet(index:int ) = new Sheet(book.getSheetAt(index))31 def getSheet(name:String) = new Sheet(book.getSheet(name))32 31 33 def sheet(index:int ) = getSheet(index)34 def sheet(name:String) = getSheet(name)35 36 def sheets(idxs:int* ) = gets(sheet, idxs:_*)37 def sheets(range:Range) = gets(sheet, range:_*)32 def sheet(index:int ) = new Sheet(hssf_book.getSheetAt(index-1)) 33 def sheet(name:String) = hssf_book.getSheet(name) match { 34 case null => throw new NoSuchElementException(name + "(Sheet not found)") 35 case v => new Sheet(v) 36 } 38 37 39 def sheetWith(index:int )(f:Sheet => Unit):Unit = f(getSheet(index)) 40 def sheetWith(name:String)(f:Sheet => Unit):Unit = f(getSheet(name)) 38 def withSheet(index:int )(f:Sheet => Unit):Unit = f(sheet(index)) 39 def withSheet(name:String)(f:Sheet => Unit):Unit = f(sheet(name)) 40 41 def sheets(idxs:int* ) = makeSomeList(sheet, idxs:_*) 42 def sheets(range:Range) = makeSomeList(sheet, range:_*) 41 43 42 44 def eachSheets(idxs:int* )(f:Sheet => Unit):Unit = sheets(idxs:_* ).foreach{f(_)} 43 45 def eachSheets(range:Range)(f:Sheet => Unit):Unit = sheets(range:_*).foreach{f(_)} 46 47 def eachSheetsWithIndex(idxs:int* )(f:(Sheet,int) => Unit) = eachWithIndex(sheets(idxs:_*), f) 48 def eachSheetsWithIndex(range:Range)(f:(Sheet,int) => Unit) = eachWithIndex(sheets(range:_*), f) 44 49 45 def eachSheetsWithIndex(idxs:int* )(f:(Sheet,int) => Unit) = eachWithIndex(sheets, f, idxs:_*) 46 def eachSheetsWithIndex(range:Range)(f:(Sheet,int) => Unit) = eachWithIndex(sheets, f, range:_*) 47 48 def each(f:Sheet => Unit):Unit = eachSheets(0 to book.getNumberOfSheets-1)(f) 50 def each(f:Sheet => Unit):Unit = eachSheets(1 to hssf_book.getNumberOfSheets)(f) 49 51 50 52 //-------------------------------------------- 51 def createSheet() = new Sheet( book.createSheet)52 def createSheet(name:String) = new Sheet( book.createSheet(name))53 def createSheet() = new Sheet(hssf_book.createSheet) 54 def createSheet(name:String) = new Sheet(hssf_book.createSheet(name)) 53 55 } 54 56 … … 56 58 * Sheet 57 59 */ 58 class Sheet( sheet:HSSFSheet) {59 def getRow(rownum:int) = new Row(sheet.getRow(rownum))60 def getCol(colnum:int) = new Col(sheet, colnum)60 class Sheet(hssf_sheet:HSSFSheet) { 61 def row(ri:int) = new Row(ri, hssf_sheet.getRow(ri-1)) 62 def col(ci:int) = new Col(this, ci) 61 63 62 def row(rownum:int) = getRow(rownum)63 def c ol(colnum:int) = getCol(colnum)64 def getCell(ri:int,ci:int) = row(ri).get(ci) 65 def cell(ri:int,ci:int) = row(ri).apply(ci) 64 66 65 def getCell(row:int,col:int) = getRow(row).getCell(col)66 def cell(row:int,col:int) = getCell(row,col)67 def withRow(ri:int)(f:Row => Unit):Unit = f(row(ri)) 68 def withCol(ci:int)(f:Col => Unit):Unit = f(col(ci)) 67 69 68 def rows(rowIdxs:int*) = gets(getRow, rowIdxs:_*) 69 def cols(colIdxs:int*) = gets(getCol, colIdxs:_*) 70 def rows(range:Range ) = gets(getRow, range:_*) 71 def cols(range:Range ) = gets(getCol, range:_*) 72 73 def rowWith(num:int)(f:Row => Unit):Unit = f(getRow(num)) 74 def colWith(num:int)(f:Col => Unit):Unit = f(getCol(num)) 70 def rows(rowIdxs:int*) = makeSomeList(row, rowIdxs:_*) 71 def cols(colIdxs:int*) = makeSomeList(col, colIdxs:_*) 72 def rows(range:Range ) = makeSomeList(row, range:_*) 73 def cols(range:Range ) = makeSomeList(col, range:_*) 75 74 76 75 def eachRows(rowIdxs:int*)(f:Row => Unit) = rows(rowIdxs:_*).foreach{f(_)} … … 79 78 def eachCols(range:Range )(f:Col => Unit) = cols(range ).foreach{f(_)} 80 79 81 def eachRowsWithIndex(idxs:int* )(f:(Row,int) => Unit) = eachWithIndex(rows , f, idxs:_*)82 def eachColsWithIndex(idxs:int* )(f:(Col,int) => Unit) = eachWithIndex(cols , f, idxs:_*)83 def eachRowsWithIndex(range:Range)(f:(Row,int) => Unit) = eachWithIndex(rows , f, range:_*)84 def eachColsWithIndex(range:Range)(f:(Col,int) => Unit) = eachWithIndex(cols , f, range:_*)80 def eachRowsWithIndex(idxs:int* )(f:(Row,int) => Unit) = eachWithIndex(rows(idxs:_*), f) 81 def eachColsWithIndex(idxs:int* )(f:(Col,int) => Unit) = eachWithIndex(cols(idxs:_*), f) 82 def eachRowsWithIndex(range:Range)(f:(Row,int) => Unit) = eachWithIndex(rows(range:_*), f) 83 def eachColsWithIndex(range:Range)(f:(Col,int) => Unit) = eachWithIndex(cols(range:_*), f) 85 84 86 def each(f:Row => Unit):Unit = eachRows(0 to sheet.getLastRowNum)(f)85 def each(f:Row => Unit):Unit = eachRows(0 to getLastRowNum)(f) 87 86 88 87 //-------------------------------------------- 89 def createRow(rownum:int) = new Row(sheet.createRow(rownum)) 88 def createRow(rownum:int) = new Row(rownum, hssf_sheet.createRow(rownum-1)) 89 90 def getLastRowNum = hssf_sheet.getLastRowNum 90 91 } 91 92 … … 93 94 * Row 94 95 */ 95 class Row(row:HSSFRow) { 96 def getCell(cellnum:int) = new Cell(row.getCell(cellnum.toShort)) 97 def cell(cellnum:int) = getCell(cellnum) 98 99 def cells(idxs:int* ) = gets(getCell, idxs:_*) 100 def cells(range:Range) = gets(getCell, range:_*) 96 class Row(ri:int, hssf_row:HSSFRow) { 97 def get(cellnum:int) = hssf_row match { 98 case null => None 99 case _ => hssf_row.getCell((cellnum-1).toShort) match { 100 case null => None 101 case v => new Some(new Cell(v)) 102 } 103 } 104 105 def apply(cellnum:int) = get(cellnum) match { 106 case None => throw new NoSuchElementException(ri +"," + cellnum + " (Cell not found)") 107 case Some(v) => v 108 } 109 def col(cellnum:int) = apply(cellnum) 110 def cell(cellnum:int) = apply(cellnum) 111 112 def getCells(idxs:int* ) = makeSomeList(get, idxs:_*) 113 def getCells(range:Range) = makeSomeList(get, range:_*) 114 def cells(idxs:int* ) = makeSomeList(apply, idxs:_*) 115 def cells(range:Range) = makeSomeList(apply, range:_*) 101 116 102 117 def eachCells(idxs:int* )(f:Cell => Unit) = cells(idxs:_* ).foreach{f(_)} 103 118 def eachCells(range:Range)(f:Cell => Unit) = cells(range:_*).foreach{f(_)} 104 119 105 def eachCellsWithIndex(idxs:int* )(f:(Cell,int) => Unit) = eachWithIndex(cells , f, idxs:_*)106 def eachCellsWithIndex(range:Range)(f:(Cell,int) => Unit) = eachWithIndex(cells , f, range:_*)120 def eachCellsWithIndex(idxs:int* )(f:(Cell,int) => Unit) = eachWithIndex(cells(idxs:_*), f) 121 def eachCellsWithIndex(range:Range)(f:(Cell,int) => Unit) = eachWithIndex(cells(range:_*), f) 107 122 108 def each(f:Cell => Unit):Unit = eachCells( 1 to row.getLastCellNum-1)(f)123 def each(f:Cell => Unit):Unit = eachCells(0 to hssf_row.getLastCellNum)(f) 109 124 110 125 //-------------------------------------------- 111 def createCell(cellnum:int) = new Cell( row.createCell(cellnum.toShort))126 def createCell(cellnum:int) = new Cell(hssf_row.createCell(cellnum.toShort)) 112 127 113 128 def setCell(cellnum:int, value:String, encoding:Short):HSSFCell = { 114 val cell = row.createCell(cellnum.toShort)129 val cell = hssf_row.createCell(cellnum.toShort) 115 130 cell.setEncoding(encoding) 116 131 cell.setCellValue(value) … … 124 139 * Col 125 140 */ 126 class Col(sheet:HSSFSheet, col:int) { 127 def getCell(row:int) = new Cell(sheet.getRow(row).getCell(col.toShort)) 128 def cell(row:int) = getCell(row) 141 class Col(sheet:Sheet, colIdx:int) { 142 def get(ri:int) = sheet.row(ri).get(colIdx) 129 143 130 def cells(idxs:int* ) = gets(getCell, idxs:_*) 131 def cells(range:Range) = gets(getCell, range:_*) 144 def apply(ri:int) = sheet.row(ri).apply(colIdx) 145 def row(ri:int) = apply(ri) 146 def cell(ri:int) = apply(ri) 147 148 def getCells(idxs:int* ) = makeSomeList(get, idxs:_*) 149 def getCells(range:Range) = makeSomeList(get, range:_*) 150 def cells(idxs:int* ) = makeSomeList(apply, idxs:_*) 151 def cells(range:Range) = makeSomeList(apply, range:_*) 132 152 133 153 def eachCells(idxs:int* )(f:Cell => Unit) = cells(idxs:_* ).foreach{f(_)} 134 154 def eachCells(range:Range)(f:Cell => Unit) = cells(range:_*).foreach{f(_)} 135 155 136 def eachCellsWithIndex(idxs:int* )(f:(Cell,int) => Unit) = eachWithIndex(cells , f, idxs:_*)137 def eachCellsWithIndex(range:Range)(f:(Cell,int) => Unit) = eachWithIndex(cells , f, range:_*)156 def eachCellsWithIndex(idxs:int* )(f:(Cell,int) => Unit) = eachWithIndex(cells(idxs:_*), f) 157 def eachCellsWithIndex(range:Range)(f:(Cell,int) => Unit) = eachWithIndex(cells(range:_*), f) 138 158 139 def each(f:Cell => Unit):Unit = eachCells( 1 to sheet.getLastRowNum-1)(f)159 def each(f:Cell => Unit):Unit = eachCells(0 to sheet.getLastRowNum)(f) 140 160 } 141 161 … … 143 163 * Cell 144 164 */ 145 class Cell(cell:HSSFCell) { 146 def getNumericCellValue = cell.getNumericCellValue 165 class Cell(hssf_cell:HSSFCell) { 166 // def get = apply match { 167 // case null => None 168 // case v => new Some(v) 169 // } 170 171 def apply = hssf_cell.getCellType match { 172 case CELL_TYPE_STRING => hssf_cell.getRichStringCellValue.getString 173 case CELL_TYPE_NUMERIC => hssf_cell.getNumericCellValue 174 case CELL_TYPE_FORMULA => hssf_cell.getCellFormula 175 case CELL_TYPE_BOOLEAN => hssf_cell.getBooleanCellValue 176 case CELL_TYPE_ERROR => hssf_cell.getErrorCellValue 177 case _ => hssf_cell.toString 178 } 147 179 148 def getCellValue = { 149 cell.getCellType match { 150 case CELL_TYPE_STRING => cell.getRichStringCellValue.getString 151 case CELL_TYPE_NUMERIC => cell.getNumericCellValue 152 case CELL_TYPE_FORMULA => cell.getCellFormula 153 case CELL_TYPE_BOOLEAN => cell.getBooleanCellValue 154 case CELL_TYPE_ERROR => cell.getErrorCellValue 155 case _ => cell.toString 156 } 157 } 158 def value = getCellValue 180 def value = apply 159 181 } 160 182 } -
lang/scala/sandbox/src/jp/ryugate/apache/POISpecification.scala
r6646 r6890 2 2 3 3 import org.specs._ 4 5 import jp.ryugate.apache.POI._ 4 import org.specs.matcher._ 5 import org.specs.matcher.AnyMatchers 6 import org.specs.matcher.PatternMatchers.CaseMatcher 6 7 7 8 object POISpecification extends Specification { 8 "Book module" should { 9 import jp.ryugate.apache.POI._ 10 11 import java.lang._ 12 13 "POI module" should { 14 val book = new Book("test.xls") 15 val sheet1 = book.sheet(1) 16 17 "open Book" in { book must notBeNull } 18 19 "get Sheet" in { 20 sheet1 must notBeNull 21 book.sheet(1) must notBeNull 22 book.sheet("Sheet1") must notBeNull 23 book.sheet(10) must throwException(new IndexOutOfBoundsException) 24 book.sheet("nosheet1") must throwException(new NoSuchElementException) 25 } 26 27 "get meny sheets" in { 28 book.sheets(1 to 10) must throwException(new IndexOutOfBoundsException) 29 book.sheets(1,10) must throwException(new IndexOutOfBoundsException) 30 } 31 32 "sheet and block" in { 33 book.withSheet("Sheet2") {sheet => sheet must notBeNull } 34 book.withSheet(10) {sheet => ; } must throwException(new IndexOutOfBoundsException) 35 36 book.eachSheets(1 to 3) {sheet => sheet must notBeNull} 37 book.eachSheets(1,10) {sheet => ; } must throwException(new IndexOutOfBoundsException) 38 39 book.eachSheetsWithIndex(1 to 3) {(sheet, idx) => sheet must notBeNull} 40 book.eachSheetsWithIndex(1,10) {(sheet, idx) => ; } must throwException(new IndexOutOfBoundsException) 41 42 book.each {sheet => sheet must notBeNull} 43 } 44 45 "read null cell" in { 46 sheet1.cell(100,100) must throwException(new NoSuchElementException) 47 48 sheet1.row(100) must notBeNull 49 sheet1.col(100) must notBeNull 50 51 sheet1.row(100).col(100) must throwException(new NoSuchElementException) 52 sheet1.col(100).row(100) must throwException(new NoSuchElementException) 53 } 54 55 "get cell" in { 56 sheet1.cell(1,1).value must is_==(1.0) 57 sheet1.col(1).row(1).value must is_==(1.0) 58 sheet1.row(1).col(1).value must is_==(1.0) 59 } 9 60 10 61 "new Book" in { … … 16 67 val bk = new Book 17 68 bk.createSheet("test") 18 bk. getSheet(0) must notBeNull19 bk. getSheet("hoge") must notBeNull69 bk.sheet(1) must notBeNull 70 bk.sheet("hoge") must throwException(new NoSuchElementException) 20 71 } 21 72
![(please configure the [header_logo] section in trac.ini)](/share/chrome/site/your_project_logo.png)