Changeset 6890

Show
Ignore:
Timestamp:
02/19/08 01:17:36 (5 years ago)
Author:
ryugate
Message:

POI.scala: Update

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

Legend:

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

    r6747 r6890  
    22 
    33/** 
    4 * POI wrapper  
     4* POI wrapper 
     5* 2008/2/18 
    56*/ 
    67object POI { 
     
    1415  import org.apache.poi.hssf.usermodel.HSSFCell._ 
    1516 
    16   private def gets[T](f:int => T, n:int*) 
     17  private def makeSomeList[T](f:int => T, n:int*) 
    1718    = 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 
    2123  /** 
    2224  * Book  
    2325  */ 
    24   class Book(book:HSSFWorkbook) { 
     26  class Book(hssf_book:HSSFWorkbook) { 
    2527    def this()                    = this(new HSSFWorkbook()) 
    2628    def this(is:InputStream)      = this(new HSSFWorkbook(is)) 
    2729    def this(ist:FileInputStream) = this(new HSSFWorkbook(ist)) 
    2830    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)) 
    3231 
    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    } 
    3837 
    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:_*) 
    4143 
    4244    def eachSheets(idxs:int*  )(f:Sheet => Unit):Unit = sheets(idxs:_* ).foreach{f(_)} 
    4345    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) 
    4449 
    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) 
    4951 
    5052    //-------------------------------------------- 
    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))   
    5355  } 
    5456 
     
    5658  * Sheet  
    5759  */ 
    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) 
    6163 
    62     def row(rownum:int) = getRow(rownum) 
    63     def col(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) 
    6466 
    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)) 
    6769 
    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:_*) 
    7574 
    7675    def eachRows(rowIdxs:int*)(f:Row => Unit) = rows(rowIdxs:_*).foreach{f(_)} 
     
    7978    def eachCols(range:Range )(f:Col => Unit) = cols(range     ).foreach{f(_)} 
    8079 
    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) 
    8584 
    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) 
    8786     
    8887    //-------------------------------------------- 
    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 
    9091  } 
    9192 
     
    9394  * Row  
    9495  */ 
    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:_*) 
    101116 
    102117    def eachCells(idxs:int*  )(f:Cell => Unit) = cells(idxs:_* ).foreach{f(_)} 
    103118    def eachCells(range:Range)(f:Cell => Unit) = cells(range:_*).foreach{f(_)} 
    104119  
    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) 
    107122       
    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) 
    109124 
    110125    //-------------------------------------------- 
    111     def createCell(cellnum:int) = new Cell(row.createCell(cellnum.toShort)) 
     126    def createCell(cellnum:int) = new Cell(hssf_row.createCell(cellnum.toShort)) 
    112127 
    113128    def setCell(cellnum:int, value:String, encoding:Short):HSSFCell = { 
    114       val cell = row.createCell(cellnum.toShort) 
     129      val cell = hssf_row.createCell(cellnum.toShort) 
    115130      cell.setEncoding(encoding) 
    116131      cell.setCellValue(value) 
     
    124139  * Col 
    125140  */ 
    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) 
    129143 
    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:_*) 
    132152     
    133153    def eachCells(idxs:int*  )(f:Cell => Unit) = cells(idxs:_* ).foreach{f(_)} 
    134154    def eachCells(range:Range)(f:Cell => Unit) = cells(range:_*).foreach{f(_)} 
    135155  
    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) 
    138158       
    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) 
    140160  } 
    141161   
     
    143163  * Cell 
    144164  */ 
    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    } 
    147179     
    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 
    159181  } 
    160182} 
  • lang/scala/sandbox/src/jp/ryugate/apache/POISpecification.scala

    r6646 r6890  
    22 
    33import org.specs._ 
    4  
    5 import jp.ryugate.apache.POI._ 
     4import org.specs.matcher._ 
     5import org.specs.matcher.AnyMatchers 
     6import org.specs.matcher.PatternMatchers.CaseMatcher 
    67 
    78object 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    } 
    960 
    1061    "new Book" in { 
     
    1667      val bk = new Book 
    1768      bk.createSheet("test") 
    18       bk.getSheet(0) must notBeNull 
    19       bk.getSheet("hoge") must notBeNull 
     69      bk.sheet(1) must notBeNull 
     70      bk.sheet("hoge") must throwException(new NoSuchElementException) 
    2071    } 
    2172