Changeset 6747 for lang/scala

Show
Ignore:
Timestamp:
02/16/08 00:36:57 (10 months ago)
Author:
ryugate
Message:

POI.scala: Update

Files:
1 modified

Legend:

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

    r6646 r6747  
    1313  import org.apache.poi.hssf.usermodel.HSSFCell 
    1414  import org.apache.poi.hssf.usermodel.HSSFCell._ 
     15 
     16  private def gets[T](f:int => T, n:int*) 
     17    = 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)} 
    1520   
    1621  /** 
     
    1823  */ 
    1924  class Book(book:HSSFWorkbook) { 
     25    def this()                    = this(new HSSFWorkbook()) 
    2026    def this(is:InputStream)      = this(new HSSFWorkbook(is)) 
    2127    def this(ist:FileInputStream) = this(new HSSFWorkbook(ist)) 
    2228    def this(filename:String)     = this(new FileInputStream(filename)) 
    23     def this()                    = this(new HSSFWorkbook()) 
    2429   
    25     def getSheet(index:int)      = new Sheet(book.getSheetAt(index)) 
    26     def getSheet(name:String)    = new Sheet(book.getSheet(name)) 
     30    def getSheet(index:int  ) = new Sheet(book.getSheetAt(index)) 
     31    def getSheet(name:String) = new Sheet(book.getSheet(name)) 
    2732 
    28     def sheetWith(index:int)(f:Sheet => Unit):Unit = f(getSheet(index)) 
     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:_*) 
    2938 
    30     def eachSheets(range:Range)(f:Sheet => Unit):Unit = range.foreach{i => f(new Sheet(book.getSheetAt(i)))} 
     39    def sheetWith(index:int  )(f:Sheet => Unit):Unit = f(getSheet(index)) 
     40    def sheetWith(name:String)(f:Sheet => Unit):Unit = f(getSheet(name)) 
     41 
     42    def eachSheets(idxs:int*  )(f:Sheet => Unit):Unit = sheets(idxs:_* ).foreach{f(_)} 
     43    def eachSheets(range:Range)(f:Sheet => Unit):Unit = sheets(range:_*).foreach{f(_)} 
     44 
     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:_*) 
    3147 
    3248    def each(f:Sheet => Unit):Unit = eachSheets(0 to book.getNumberOfSheets-1)(f) 
    33    
     49 
     50    //-------------------------------------------- 
    3451    def createSheet()            = new Sheet(book.createSheet) 
    3552    def createSheet(name:String) = new Sheet(book.createSheet(name))   
     
    4158  class Sheet(sheet:HSSFSheet) { 
    4259    def getRow(rownum:int) = new Row(sheet.getRow(rownum)) 
    43     def row(rownum:int) = getRow(rownum) 
    44      
    4560    def getCol(colnum:int) = new Col(sheet, colnum) 
    4661 
     62    def row(rownum:int) = getRow(rownum) 
     63    def col(colnum:int) = getCol(colnum) 
     64 
    4765    def getCell(row:int,col:int) = getRow(row).getCell(col) 
     66    def cell(row:int,col:int) = getCell(row,col) 
    4867 
    49     def gets[T](f:int => T, n:Array[int]) = n.foldLeft(List[T]()){(acc, i) => f(i) :: acc}.reverse 
    50     def rows(rs:int*) = gets(getRow, rs.toArray) 
    51     def rows(range:Range) = gets(getRow, range.toArray) 
    52     def cols(cs:int*) = gets(getCol, cs.toArray) 
     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:_*) 
    5372 
    54     def rowWith(rownum:int)(f:Row => Unit):Unit = f(getRow(rownum)) 
     73    def rowWith(num:int)(f:Row => Unit):Unit = f(getRow(num)) 
     74    def colWith(num:int)(f:Col => Unit):Unit = f(getCol(num)) 
    5575 
    56     def eachRows(range:Range)(f:Row => Unit) = range.foreach{i => f(new Row(sheet.getRow(i)))} 
    57     def eachCols(cs:int*)(f:Col => Unit) = cs.foreach {col => f(new Col(sheet, col))} 
     76    def eachRows(rowIdxs:int*)(f:Row => Unit) = rows(rowIdxs:_*).foreach{f(_)} 
     77    def eachCols(colIdxs:int*)(f:Col => Unit) = cols(colIdxs:_*).foreach{f(_)} 
     78    def eachRows(range:Range )(f:Row => Unit) = rows(range     ).foreach{f(_)} 
     79    def eachCols(range:Range )(f:Col => Unit) = cols(range     ).foreach{f(_)} 
     80 
     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:_*) 
    5885 
    5986    def each(f:Row => Unit):Unit = eachRows(0 to sheet.getLastRowNum)(f) 
    6087     
     88    //-------------------------------------------- 
    6189    def createRow(rownum:int) = new Row(sheet.createRow(rownum)) 
    6290  } 
     
    6896    def getCell(cellnum:int) = new Cell(row.getCell(cellnum.toShort)) 
    6997    def cell(cellnum:int) = getCell(cellnum) 
    70     def cells(cs:int*) = cs.foldLeft(List[Cell]()){(acc,cell) => getCell(cell) :: acc}.reverse 
     98     
     99    def cells(idxs:int*  ) = gets(getCell, idxs:_*) 
     100    def cells(range:Range) = gets(getCell, range:_*) 
    71101 
    72     def eachCells(range:Range)(f:Cell => Unit) = range.foreach{i => f(new Cell(row.getCell(i.toShort)))} 
    73     def eachCells(cols:int*)(f:Cell => Unit)   =  cols.foreach{i => f(new Cell(row.getCell(i.toShort)))} 
    74  
     102    def eachCells(idxs:int*  )(f:Cell => Unit) = cells(idxs:_* ).foreach{f(_)} 
     103    def eachCells(range:Range)(f:Cell => Unit) = cells(range:_*).foreach{f(_)} 
     104  
     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:_*) 
     107       
    75108    def each(f:Cell => Unit):Unit = eachCells(1 to row.getLastCellNum-1)(f) 
    76109 
     110    //-------------------------------------------- 
    77111    def createCell(cellnum:int) = new Cell(row.createCell(cellnum.toShort)) 
    78112 
     
    93127    def getCell(row:int) = new Cell(sheet.getRow(row).getCell(col.toShort)) 
    94128    def cell(row:int) = getCell(row) 
     129 
     130    def cells(idxs:int*  ) = gets(getCell, idxs:_*) 
     131    def cells(range:Range) = gets(getCell, range:_*) 
    95132     
    96     def eachCells(rows:int*)(f:Cell => Unit) = rows.foreach{i => f(getCell(i))} 
     133    def eachCells(idxs:int*  )(f:Cell => Unit) = cells(idxs:_* ).foreach{f(_)} 
     134    def eachCells(range:Range)(f:Cell => Unit) = cells(range:_*).foreach{f(_)} 
     135  
     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:_*) 
     138       
     139    def each(f:Cell => Unit):Unit = eachCells(1 to sheet.getLastRowNum-1)(f) 
    97140  } 
    98141   
     
    102145  class Cell(cell:HSSFCell) { 
    103146    def getNumericCellValue = cell.getNumericCellValue 
     147     
    104148    def getCellValue = { 
    105149      cell.getCellType match {