Changeset 7772
- Timestamp:
- 03/10/08 23:51:21 (5 years ago)
- Location:
- lang/scala/sandbox/src/jp/ryugate/apache
- Files:
-
- 2 modified
-
POI.scala (modified) (9 diffs)
-
POISpecification.scala (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
-
lang/scala/sandbox/src/jp/ryugate/apache/POI.scala
r7415 r7772 6 6 */ 7 7 object POI { 8 import java.io.FileInputStream 8 9 9 import java.io.InputStream 10 10 … … 24 24 * Book 25 25 */ 26 class Book(hssf_book:HSSFWorkbook) { 27 def this() = this(new HSSFWorkbook()) 28 def this(is:InputStream) = this(new HSSFWorkbook(is)) 29 def this(ist:FileInputStream) = this(new HSSFWorkbook(ist)) 30 def this(filename:String) = this(new FileInputStream(filename)) 31 32 def sheet(index:int ) = new Sheet(hssf_book.getSheetAt(index-1)) 26 class MyBook(hssf_book:HSSFWorkbook) { 27 def sheet(index:int ) = hssf_book.getSheetAt(index-1) 33 28 def sheet(name:String) = hssf_book.getSheet(name) match { 34 29 case null => throw new NoSuchElementException(name + "(Sheet not found)") 35 case v => new Sheet(v)30 case v => v 36 31 } 37 32 38 def withSheet[V](index:int )(f: Sheet => V) = f(sheet(index))39 def withSheet[V](name:String)(f: Sheet => V) = f(sheet(name))33 def withSheet[V](index:int )(f:HSSFSheet => V) = f(sheet(index)) 34 def withSheet[V](name:String)(f:HSSFSheet => V) = f(sheet(name)) 40 35 41 def sheets(idxs:int* ) = makeSomeList(sheet, idxs:_*)42 def sheets(range:Range) = makeSomeList(sheet, range:_*)36 def getSheets(idxs:int* ) = makeSomeList(sheet, idxs:_*) 37 def getSheets(range:Range) = makeSomeList(sheet, range:_*) 43 38 44 def eachSheets[V](idxs:int* )(f: Sheet => V) = sheets(idxs:_* ).foreach{f(_)}45 def eachSheets[V](range:Range)(f: Sheet => V) = sheets(range:_*).foreach{f(_)}39 def eachSheets[V](idxs:int* )(f:HSSFSheet => V) = getSheets(idxs:_* ).foreach{f(_)} 40 def eachSheets[V](range:Range)(f:HSSFSheet => V) = getSheets(range:_*).foreach{f(_)} 46 41 47 def eachSheetsWithIndex[V](idxs:int* )(f:( Sheet,int) => V) = eachWithIndex(sheets(idxs:_*), f)48 def eachSheetsWithIndex[V](range:Range)(f:( Sheet,int) => V) = eachWithIndex(sheets(range:_*), f)42 def eachSheetsWithIndex[V](idxs:int* )(f:(HSSFSheet,int) => V) = eachWithIndex(getSheets(idxs:_*), f) 43 def eachSheetsWithIndex[V](range:Range)(f:(HSSFSheet,int) => V) = eachWithIndex(getSheets(range:_*), f) 49 44 50 def each[V](f:Sheet => V) = eachSheets(1 to hssf_book.getNumberOfSheets)(f) 51 52 //-------------------------------------------- 53 def createSheet() = new Sheet(hssf_book.createSheet) 54 def createSheet(name:String) = new Sheet(hssf_book.createSheet(name)) 45 def each[V](f:HSSFSheet => V) = eachSheets(1 to hssf_book.getNumberOfSheets)(f) 55 46 } 47 implicit def HSSFWorkbookToExtBook(book:HSSFWorkbook) = new MyBook(book) 56 48 57 49 /** 58 50 * Sheet 59 51 */ 60 class Sheet(hssf_sheet:HSSFSheet) {61 def row(ri:int) = new Row(ri, hssf_sheet.getRow(ri-1))52 class MySheet(hssf_sheet:HSSFSheet) { 53 def row(ri:int) = hssf_sheet.getRow(ri-1) 62 54 def col(ci:int) = new Col(this, ci) 63 55 … … 65 57 def cell(ri:int,ci:int) = row(ri).apply(ci) 66 58 67 def withRow[V](ri:int)(f: Row => V) = f(row(ri))59 def withRow[V](ri:int)(f:HSSFRow => V) = f(row(ri)) 68 60 def withCol[V](ci:int)(f:Col => V) = f(col(ci)) 69 61 … … 73 65 def cols(range:Range ) = makeSomeList(col, range:_*) 74 66 75 def eachRows[V](rowIdxs:int*)(f: Row => V) = rows(rowIdxs:_*).foreach{f(_)}67 def eachRows[V](rowIdxs:int*)(f:HSSFRow => V) = rows(rowIdxs:_*).foreach{f(_)} 76 68 def eachCols[V](colIdxs:int*)(f:Col => V) = cols(colIdxs:_*).foreach{f(_)} 77 def eachRows[V](range:Range )(f: Row => V) = rows(range ).foreach{f(_)}69 def eachRows[V](range:Range )(f:HSSFRow => V) = rows(range ).foreach{f(_)} 78 70 def eachCols[V](range:Range )(f:Col => V) = cols(range ).foreach{f(_)} 79 71 80 def eachRowsWithIndex[V](idxs:int* )(f:( Row,int) => V) = eachWithIndex(rows(idxs:_*), f)72 def eachRowsWithIndex[V](idxs:int* )(f:(HSSFRow,int) => V) = eachWithIndex(rows(idxs:_*), f) 81 73 def eachColsWithIndex[V](idxs:int* )(f:(Col,int) => V) = eachWithIndex(cols(idxs:_*), f) 82 def eachRowsWithIndex[V](range:Range)(f:( Row,int) => V) = eachWithIndex(rows(range:_*), f)74 def eachRowsWithIndex[V](range:Range)(f:(HSSFRow,int) => V) = eachWithIndex(rows(range:_*), f) 83 75 def eachColsWithIndex[V](range:Range)(f:(Col,int) => V) = eachWithIndex(cols(range:_*), f) 84 76 85 def each[V](f: Row => V) = eachRows(0 to getLastRowNum)(f)77 def each[V](f:HSSFRow => V) = eachRows(0 to getLastRowNum)(f) 86 78 87 79 //-------------------------------------------- 88 def createRow(rownum:int) = new Row(rownum, hssf_sheet.createRow(rownum-1))80 def createRow(rownum:int) = hssf_sheet.createRow(rownum-1) 89 81 90 82 def getLastRowNum = hssf_sheet.getLastRowNum 91 83 } 92 84 implicit def HSSFSheetToExtSheet(sheet:HSSFSheet) = new MySheet(sheet) 85 93 86 /** 94 87 * Row 95 88 */ 96 class Row(ri:int,hssf_row:HSSFRow) {89 class MyRow(hssf_row:HSSFRow) { 97 90 def get(cellnum:int) = hssf_row match { 98 91 case null => None 99 92 case _ => hssf_row.getCell((cellnum-1).toShort) match { 100 93 case null => None 101 case v => new Some( new Cell(v))94 case v => new Some(v) 102 95 } 103 96 } 104 97 105 98 def apply(cellnum:int) = get(cellnum) match { 106 case None => throw new NoSuchElementException( ri +"," +cellnum + " (Cell not found)")99 case None => throw new NoSuchElementException(cellnum + " (Cell not found)") 107 100 case Some(v) => v 108 101 } … … 115 108 def cells(range:Range) = makeSomeList(apply, range:_*) 116 109 117 def eachCells[V](idxs:int* )(f: Cell => V) = cells(idxs:_* ).foreach{f(_)}118 def eachCells[V](range:Range)(f: Cell => V) = cells(range:_*).foreach{f(_)}110 def eachCells[V](idxs:int* )(f:HSSFCell => V) = cells(idxs:_* ).foreach{f(_)} 111 def eachCells[V](range:Range)(f:HSSFCell => V) = cells(range:_*).foreach{f(_)} 119 112 120 def eachCellsWithIndex[V](idxs:int* )(f:( Cell,int) => V) = eachWithIndex(cells(idxs:_*), f)121 def eachCellsWithIndex[V](range:Range)(f:( Cell,int) => V) = eachWithIndex(cells(range:_*), f)113 def eachCellsWithIndex[V](idxs:int* )(f:(HSSFCell,int) => V) = eachWithIndex(cells(idxs:_*), f) 114 def eachCellsWithIndex[V](range:Range)(f:(HSSFCell,int) => V) = eachWithIndex(cells(range:_*), f) 122 115 123 def each[V](f: Cell => V) = eachCells(0 to hssf_row.getLastCellNum)(f)116 def each[V](f:HSSFCell => V) = eachCells(0 to hssf_row.getLastCellNum)(f) 124 117 125 118 //-------------------------------------------- 126 def createCell(cellnum:int) = new Cell(hssf_row.createCell(cellnum.toShort))119 def createCell(cellnum:int) = hssf_row.createCell(cellnum.toShort) 127 120 128 121 def setCell(cellnum:int, value:String, encoding:Short):HSSFCell = { … … 135 128 def setCell(cellnum:int, value:String):HSSFCell = setCell(cellnum, value, ENCODING_UNCHANGED) 136 129 } 137 130 implicit def HSSFRowToExtRow(row:HSSFRow) = new MyRow(row) 131 138 132 /** 139 133 * Col 140 134 */ 141 class Col(sheet: Sheet, colIdx:int) {135 class Col(sheet:MySheet, colIdx:int) { 142 136 def get(ri:int) = sheet.row(ri).get(colIdx) 143 137 … … 151 145 def cells(range:Range) = makeSomeList(apply, range:_*) 152 146 153 def eachCells[V](idxs:int* )(f: Cell => V) = cells(idxs:_* ).foreach{f(_)}154 def eachCells[V](range:Range)(f: Cell => V) = cells(range:_*).foreach{f(_)}147 def eachCells[V](idxs:int* )(f:HSSFCell => V) = cells(idxs:_* ).foreach{f(_)} 148 def eachCells[V](range:Range)(f:HSSFCell => V) = cells(range:_*).foreach{f(_)} 155 149 156 def eachCellsWithIndex[V](idxs:int* )(f:( Cell,int) => V) = eachWithIndex(cells(idxs:_*), f)157 def eachCellsWithIndex[V](range:Range)(f:( Cell,int) => V) = eachWithIndex(cells(range:_*), f)150 def eachCellsWithIndex[V](idxs:int* )(f:(HSSFCell,int) => V) = eachWithIndex(cells(idxs:_*), f) 151 def eachCellsWithIndex[V](range:Range)(f:(HSSFCell,int) => V) = eachWithIndex(cells(range:_*), f) 158 152 159 def each[V](f: Cell => V) = eachCells(0 to sheet.getLastRowNum)(f)153 def each[V](f:HSSFCell => V) = eachCells(0 to sheet.getLastRowNum)(f) 160 154 } 161 155 … … 163 157 * Cell 164 158 */ 165 class Cell(hssf_cell:HSSFCell) {159 class MyCell(hssf_cell:HSSFCell) { 166 160 // def get = apply match { 167 161 // case null => None … … 180 174 def value = apply 181 175 } 176 implicit def HSSFCellToExtCell(cell:HSSFCell) = new MyCell(cell) 182 177 } -
lang/scala/sandbox/src/jp/ryugate/apache/POISpecification.scala
r7415 r7772 6 6 object POISpecification extends Specification { 7 7 import jp.ryugate.apache.POI._ 8 8 import org.apache.poi.hssf.usermodel.HSSFWorkbook 9 import org.apache.poi.hssf.usermodel.HSSFSheet 10 import org.apache.poi.hssf.usermodel.HSSFRow 11 import org.apache.poi.hssf.usermodel.HSSFCell 12 import org.apache.poi.hssf.usermodel.HSSFCell._ 13 9 14 import java.lang._ 10 15 import java.io.FileInputStream 16 11 17 "POI module" should { 12 val book = new Book("testdata/test.xls")18 val book = new HSSFWorkbook(new FileInputStream("testdata/test.xls")) 13 19 val sheet1 = book.sheet(1) 14 20 … … 24 30 25 31 "get meny sheets" in { 26 book. sheets(1 to 10) must throwException(new IndexOutOfBoundsException)27 book. sheets(1,10) must throwException(new IndexOutOfBoundsException)32 book.getSheets(1 to 10) must throwException(new IndexOutOfBoundsException) 33 book.getSheets(1,10) must throwException(new IndexOutOfBoundsException) 28 34 } 29 35 … … 44 50 sheet1.cell(100,100) must throwException(new NoSuchElementException) 45 51 46 sheet1.row(100) must notBeNull52 sheet1.row(100) must beNull 47 53 sheet1.col(100) must notBeNull 48 54 … … 57 63 } 58 64 59 "new Book" in {60 val bk = new Book61 bk must notBeNull62 }63 64 65 "create and get Sheet" in { 65 val bk = new Book66 val bk = new HSSFWorkbook 66 67 bk.createSheet("test") 67 68 bk.sheet(1) must notBeNull
![(please configure the [header_logo] section in trac.ini)](/share/chrome/site/your_project_logo.png)