Changeset 7772

Show
Ignore:
Timestamp:
03/10/08 23:51:21 (5 years ago)
Author:
ryugate
Message:

POI: Views化した

Location:
lang/scala/sandbox/src/jp/ryugate/apache
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • lang/scala/sandbox/src/jp/ryugate/apache/POI.scala

    r7415 r7772  
    66*/ 
    77object POI { 
    8   import java.io.FileInputStream 
     8 
    99  import java.io.InputStream 
    1010 
     
    2424  * Book  
    2525  */ 
    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) 
    3328    def sheet(name:String) = hssf_book.getSheet(name) match { 
    3429      case null => throw new NoSuchElementException(name + "(Sheet not found)") 
    35       case v => new Sheet(v) 
     30      case v => v 
    3631    } 
    3732 
    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)) 
    4035 
    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:_*) 
    4338 
    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(_)} 
    4641     
    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) 
    4944 
    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) 
    5546  } 
     47  implicit def HSSFWorkbookToExtBook(book:HSSFWorkbook) = new MyBook(book) 
    5648 
    5749  /** 
    5850  * Sheet  
    5951  */ 
    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) 
    6254    def col(ci:int) = new Col(this, ci) 
    6355 
     
    6557    def cell(ri:int,ci:int) = row(ri).apply(ci) 
    6658 
    67     def withRow[V](ri:int)(f:Row => V) = f(row(ri)) 
     59    def withRow[V](ri:int)(f:HSSFRow => V) = f(row(ri)) 
    6860    def withCol[V](ci:int)(f:Col => V) = f(col(ci)) 
    6961 
     
    7365    def cols(range:Range ) = makeSomeList(col, range:_*) 
    7466 
    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(_)} 
    7668    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(_)} 
    7870    def eachCols[V](range:Range )(f:Col => V) = cols(range     ).foreach{f(_)} 
    7971 
    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) 
    8173    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) 
    8375    def eachColsWithIndex[V](range:Range)(f:(Col,int) => V) = eachWithIndex(cols(range:_*), f) 
    8476 
    85     def each[V](f:Row => V) = eachRows(0 to getLastRowNum)(f) 
     77    def each[V](f:HSSFRow => V) = eachRows(0 to getLastRowNum)(f) 
    8678     
    8779    //-------------------------------------------- 
    88     def createRow(rownum:int) = new Row(rownum, hssf_sheet.createRow(rownum-1)) 
     80    def createRow(rownum:int) = hssf_sheet.createRow(rownum-1) 
    8981     
    9082    def getLastRowNum = hssf_sheet.getLastRowNum 
    9183  } 
    92  
     84  implicit def HSSFSheetToExtSheet(sheet:HSSFSheet) = new MySheet(sheet) 
     85   
    9386  /** 
    9487  * Row  
    9588  */ 
    96   class Row(ri:int, hssf_row:HSSFRow) { 
     89  class MyRow(hssf_row:HSSFRow) { 
    9790    def get(cellnum:int) = hssf_row match { 
    9891      case null => None 
    9992      case _ => hssf_row.getCell((cellnum-1).toShort) match { 
    10093        case null => None 
    101         case v    => new Some(new Cell(v)) 
     94        case v    => new Some(v) 
    10295      } 
    10396    } 
    10497 
    10598    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)") 
    107100      case Some(v) => v 
    108101    } 
     
    115108    def cells(range:Range) = makeSomeList(apply, range:_*) 
    116109 
    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(_)} 
    119112  
    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) 
    122115       
    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) 
    124117 
    125118    //-------------------------------------------- 
    126     def createCell(cellnum:int) = new Cell(hssf_row.createCell(cellnum.toShort)) 
     119    def createCell(cellnum:int) = hssf_row.createCell(cellnum.toShort) 
    127120 
    128121    def setCell(cellnum:int, value:String, encoding:Short):HSSFCell = { 
     
    135128    def setCell(cellnum:int, value:String):HSSFCell = setCell(cellnum, value, ENCODING_UNCHANGED) 
    136129  } 
    137  
     130  implicit def HSSFRowToExtRow(row:HSSFRow) = new MyRow(row) 
     131   
    138132  /** 
    139133  * Col 
    140134  */ 
    141   class Col(sheet:Sheet, colIdx:int) { 
     135  class Col(sheet:MySheet, colIdx:int) { 
    142136    def get(ri:int) = sheet.row(ri).get(colIdx) 
    143137 
     
    151145    def cells(range:Range) = makeSomeList(apply, range:_*) 
    152146     
    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(_)} 
    155149  
    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) 
    158152       
    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) 
    160154  } 
    161155   
     
    163157  * Cell 
    164158  */ 
    165   class Cell(hssf_cell:HSSFCell) { 
     159  class MyCell(hssf_cell:HSSFCell) { 
    166160//    def get = apply match { 
    167161//      case null => None 
     
    180174    def value = apply 
    181175  } 
     176  implicit def HSSFCellToExtCell(cell:HSSFCell) = new MyCell(cell) 
    182177} 
  • lang/scala/sandbox/src/jp/ryugate/apache/POISpecification.scala

    r7415 r7772  
    66object POISpecification extends Specification { 
    77  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   
    914  import java.lang._ 
    10  
     15  import java.io.FileInputStream 
     16   
    1117  "POI module" should { 
    12     val book = new Book("testdata/test.xls") 
     18    val book = new HSSFWorkbook(new FileInputStream("testdata/test.xls")) 
    1319    val sheet1 = book.sheet(1) 
    1420 
     
    2430     
    2531    "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) 
    2834    } 
    2935     
     
    4450      sheet1.cell(100,100) must throwException(new NoSuchElementException) 
    4551 
    46       sheet1.row(100) must notBeNull 
     52      sheet1.row(100) must beNull 
    4753      sheet1.col(100) must notBeNull 
    4854 
     
    5763    } 
    5864 
    59     "new Book" in { 
    60       val bk = new Book 
    61       bk must notBeNull 
    62     } 
    63  
    6465    "create and get Sheet" in { 
    65       val bk = new Book 
     66      val bk = new HSSFWorkbook 
    6667      bk.createSheet("test") 
    6768      bk.sheet(1) must notBeNull