Changeset 8079 for lang/scala

Show
Ignore:
Timestamp:
03/18/08 18:29:01 (9 months ago)
Author:
ryugate
Message:

POI.scala

add MyBook#eachSheetsByName?
add MySheet#cell?(String)
add MySheet#cells?(String*)
add MySheet#setCell?
add MySheet#applyOrCreateCell?
add MyRow#applyOrCreateCell?
add Col#applyOrCreateCell
add MyCell#get?
add MyCell#set?
add MyCell#setFormula?
change MySheet#cell?(Int,Int) 行、桁の順で指定したのを、桁、行の順にした

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

Legend:

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

    r7820 r8079  
    33/** 
    44* POI wrapper 
    5 * 2008/3/12 
     5* 2008/3/17 
    66*/ 
    77object POI { 
    88 
     9  import scala.Math 
    910  import java.io.InputStream 
    1011 
    11   import org.apache.poi.hssf.usermodel.HSSFWorkbook 
    12   import org.apache.poi.hssf.usermodel.HSSFSheet 
    13   import org.apache.poi.hssf.usermodel.HSSFRow 
    14   import org.apache.poi.hssf.usermodel.HSSFCell 
     12  import org.apache.poi.hssf.usermodel.{HSSFWorkbook, HSSFSheet, HSSFRow, HSSFCell, HSSFRichTextString} 
    1513  import org.apache.poi.hssf.usermodel.HSSFCell._ 
    1614 
    17   private def makeSomeList[T](f:int => T, n:int*) 
    18     = n.foldLeft(List[T]()){(acc, i) => f(i) :: acc}.reverse 
    19      
     15  private def makeSomeList[A,B](f:A => B, n:A*) 
     16    = n.foldLeft(List[B]()){(acc, i) => f(i) :: acc}.reverse 
     17 
    2018  private def eachWithIndex[T,V](lst:List[T], f2:(T,int) => V) 
    2119    = lst.zipWithIndex.foreach{case (row,idx) => f2(row,idx)} 
    2220 
     21  private def stoi(str:String, conv:Char=>Int, radix:Int) = { 
     22    def pow(n:Int,i:Int):Int = i match { 
     23      case 0 => 1 
     24      case _ => n * pow(n, i-1) 
     25    } 
     26   
     27    str.toArray.reverse.zipWithIndex.foldLeft(0) { 
     28      case (acc, (chr,digit)) => acc + conv(chr) * pow(radix,digit) 
     29    } 
     30  } 
     31   
     32  private def colstrToInt(str:String) = stoi(str.replaceAll("[0-9]", ""), (c:Char)=>c-'A'+1, 26) 
     33  private def rowstrToInt(str:String) = str.replaceAll("[A-Z]", "").toInt 
     34 
    2335  /** 
    2436  * Book  
    2537  */ 
    2638  class MyBook(hssf_book:HSSFWorkbook) { 
    27     def sheet(index:int  ) = hssf_book.getSheetAt(index-1)                      // index start from 1 
    28     def sheet(name:String) = hssf_book.getSheet(name) match { 
     39    def sheet(index:Int  ):HSSFSheet = hssf_book.getSheetAt(index-1)            // index start from 1 
     40    def sheet(name:String):HSSFSheet = hssf_book.getSheet(name) match { 
    2941      case null => throw new NoSuchElementException(name + "(Sheet not found)") 
    3042      case v => v 
     
    3446    def withSheet[V](name:String)(f:HSSFSheet => V) = f(sheet(name)) 
    3547 
    36     def getSheets(idxs:int*  ) = makeSomeList(sheet, idxs:_*) 
    37     def getSheets(range:Range) = makeSomeList(sheet, range:_*) 
     48    def getSheets(idxs:Int*   ) = makeSomeList[Int,HSSFSheet](sheet, idxs:_*) 
     49    def getSheets(range:Range ) = makeSomeList[Int,HSSFSheet](sheet, range:_*) 
    3850 
    3951    def eachSheets[V](idxs:int*  )(f:HSSFSheet => V) = getSheets(idxs:_* ).foreach{f(_)} 
    4052    def eachSheets[V](range:Range)(f:HSSFSheet => V) = getSheets(range:_*).foreach{f(_)} 
     53 
     54    def getSheetsByName(idxs:String*) = makeSomeList[String,HSSFSheet](sheet, idxs:_*) 
     55    def eachSheetsByName[V](idxs:String*)(f:HSSFSheet => V) = getSheetsByName(idxs:_* ).foreach{f(_)} 
    4156     
    4257    def eachSheetsWithIndex[V](idxs:int*  )(f:(HSSFSheet,int) => V) = eachWithIndex(getSheets(idxs:_*), f) 
     
    5570      case row => row  
    5671    } 
    57     def col(ci:int) = new Col(this, ci) 
    58  
    59     def getCell(ri:int,ci:int) = row(ri).get(ci) 
    60     def cell(ri:int,ci:int) = row(ri).apply(ci) 
    61  
     72    def col(ci:int    ):Col = new Col(this, ci) 
     73    def col(cis:String):Col = col(colstrToInt(cis)) 
     74 
     75    def getCell(ci:int,ri:int ):Option[HSSFCell] = row(ri).get(ci) 
     76    def getCell(cellstr:String):Option[HSSFCell] = getCell(colstrToInt(cellstr), rowstrToInt(cellstr)) 
     77 
     78    def cell(ci:int,ri:int ):HSSFCell = row(ri).apply(ci) 
     79    def cell(cellstr:String):HSSFCell = cell(colstrToInt(cellstr), rowstrToInt(cellstr)) 
     80 
     81    def cells(cellstrs:String*) = makeSomeList(cell, cellstrs:_*) 
     82 
     83    def applyOrCreateCell(ci:int,ri:int ):HSSFCell = row(ri).applyOrCreate(ci) 
     84    def applyOrCreateCell(cellstr:String):HSSFCell = applyOrCreateCell(colstrToInt(cellstr), rowstrToInt(cellstr)) 
     85     
    6286    def withRow[V](ri:int)(f:HSSFRow => V) = f(row(ri)) 
    63     def withCol[V](ci:int)(f:Col => V) = f(col(ci)) 
    64  
    65     def rows(rowIdxs:int*) = makeSomeList(row, rowIdxs:_*) 
    66     def cols(colIdxs:int*) = makeSomeList(col, colIdxs:_*) 
    67     def rows(range:Range ) = makeSomeList(row, range:_*) 
    68     def cols(range:Range ) = makeSomeList(col, range:_*) 
     87    def withCol[V](ci:int)(f:Col     => V) = f(col(ci)) 
     88 
     89    def rows(rowIdxs:int*) = makeSomeList(row,          rowIdxs:_*) 
     90    def cols(colIdxs:int*) = makeSomeList(col:int=>Col, colIdxs:_*) 
     91    def rows(range:Range ) = makeSomeList(row,          range:_*) 
     92    def cols(range:Range ) = makeSomeList(col:int=>Col, range:_*) 
    6993 
    7094    def eachRows[V](rowIdxs:int*)(f:HSSFRow => V) = rows(rowIdxs:_*).foreach{f(_)} 
    71     def eachCols[V](colIdxs:int*)(f:Col => V) = cols(colIdxs:_*).foreach{f(_)} 
     95    def eachCols[V](colIdxs:int*)(f:Col     => V) = cols(colIdxs:_*).foreach{f(_)} 
    7296    def eachRows[V](range:Range )(f:HSSFRow => V) = rows(range     ).foreach{f(_)} 
    73     def eachCols[V](range:Range )(f:Col => V) = cols(range     ).foreach{f(_)} 
     97    def eachCols[V](range:Range )(f:Col     => V) = cols(range     ).foreach{f(_)} 
    7498 
    7599    def eachRowsWithIndex[V](idxs:int*  )(f:(HSSFRow,int) => V) = eachWithIndex(rows(idxs:_*), f) 
    76     def eachColsWithIndex[V](idxs:int*  )(f:(Col,int) => V) = eachWithIndex(cols(idxs:_*), f) 
     100    def eachColsWithIndex[V](idxs:int*  )(f:(Col,    int) => V) = eachWithIndex(cols(idxs:_*), f) 
    77101    def eachRowsWithIndex[V](range:Range)(f:(HSSFRow,int) => V) = eachWithIndex(rows(range:_*), f) 
    78     def eachColsWithIndex[V](range:Range)(f:(Col,int) => V) = eachWithIndex(cols(range:_*), f) 
     102    def eachColsWithIndex[V](range:Range)(f:(Col,    int) => V) = eachWithIndex(cols(range:_*), f) 
    79103 
    80104    def each[V](f:HSSFRow => V) = eachRows(0 to getLastRowNum)(f) 
     
    83107 
    84108    //-------------------------------------------- 
    85     def setCell(ri:Int, ci:Int, value:String, encoding:Short):HSSFCell = row(ri).setCell(ci, value, encoding) 
    86     def setCell(ri:Int, ci:Int, value:String):HSSFCell = row(ri).setCell(ci, value) 
     109    def setCell(ri:Int, ci:Int, value:Any, encoding:Short):HSSFCell = row(ri).setCell(ci, value, encoding) 
     110    def setCell(ri:Int, ci:Int, value:Any                ):HSSFCell = row(ri).setCell(ci, value) 
     111    def setCell(cellstr:String, value:Any                ):HSSFCell = row(rowstrToInt(cellstr)).setCell(colstrToInt(cellstr), value) 
    87112  } 
    88113  implicit def HSSFSheetToExtSheet(sheet:HSSFSheet) = new MySheet(sheet) 
     
    97122    } 
    98123 
    99     def getOrCreate(ci:int) = get(ci) match {                                   // index start from 1 
    100       case None    => hssf_row.createCell((ci-1).toShort) 
    101       case Some(v) => v 
    102     } 
    103  
    104124    def apply(cellnum:int) = get(cellnum) match { 
    105125      case None => throw new NoSuchElementException(cellnum + " (Cell not found)") 
     
    108128    def col(cellnum:int) = apply(cellnum) 
    109129    def cell(cellnum:int) = apply(cellnum) 
     130 
     131    def applyOrCreate(ci:int) = get(ci) match {                                 // index start from 1 
     132      case None    => hssf_row.createCell((ci-1).toShort) 
     133      case Some(v) => v 
     134    } 
    110135 
    111136    def getCells(idxs:int*  ) = makeSomeList(get, idxs:_*) 
     
    113138    def cells(idxs:int*  ) = makeSomeList(apply, idxs:_*) 
    114139    def cells(range:Range) = makeSomeList(apply, range:_*) 
     140 
     141    def applyOrCreateCells(idxs:Int*  ) = makeSomeList(applyOrCreate, idxs:_*) 
     142    def applyOrCreateCells(range:Range) = makeSomeList(applyOrCreate, range:_*) 
    115143 
    116144    def eachCells[V](idxs:int*  )(f:HSSFCell => V) = cells(idxs:_* ).foreach{f(_)} 
     
    123151 
    124152    //-------------------------------------------- 
    125     def setCell(ci:int, value:String, encoding:Short):HSSFCell = { 
    126       val cell = getOrCreate(ci) 
    127       cell.setEncoding(encoding) 
    128       cell.setCellValue(value) 
    129       cell 
    130     } 
    131  
    132     def setCell(cellnum:int, value:String):HSSFCell = setCell(cellnum, value, ENCODING_UNCHANGED) 
     153    def setCell(ci:int, value:Any, encoding:Short):HSSFCell = applyOrCreate(ci).set(value, encoding) 
     154    def setCell(ci:int, value:Any                ):HSSFCell = setCell(ci, value, ENCODING_UNCHANGED) 
    133155  } 
    134156  implicit def HSSFRowToExtRow(row:HSSFRow) = new MyRow(row) 
     
    143165    def row(ri:int) = apply(ri) 
    144166    def cell(ri:int) = apply(ri) 
     167 
     168    def applyOrCreate(ri:int) = sheet.row(ri).applyOrCreate(colIdx) 
    145169 
    146170    def getCells(idxs:int*  ) = makeSomeList(get, idxs:_*) 
     
    148172    def cells(idxs:int*  ) = makeSomeList(apply, idxs:_*) 
    149173    def cells(range:Range) = makeSomeList(apply, range:_*) 
     174     
     175    def applyOrCreateCells(idxs:Int*  ) = makeSomeList(applyOrCreate, idxs:_*) 
     176    def applyOrCreateCells(range:Range) = makeSomeList(applyOrCreate, range:_*) 
    150177     
    151178    def eachCells[V](idxs:int*  )(f:HSSFCell => V) = cells(idxs:_* ).foreach{f(_)} 
     
    158185 
    159186    //-------------------------------------------- 
    160     def setCell(cellnum:int, value:String, encoding:Short):HSSFCell = sheet.row(cellnum).setCell(colIdx, value, encoding) 
    161     def setCell(cellnum:int, value:String):HSSFCell = sheet.row(cellnum).setCell(colIdx, value) 
     187    def setCell(ri:int, value:Any, encoding:Short):HSSFCell = sheet.row(ri).setCell(colIdx, value, encoding) 
     188    def setCell(ri:int, value:Any                ):HSSFCell = sheet.row(ri).setCell(colIdx, value) 
    162189  } 
    163190   
     
    166193  */ 
    167194  class MyCell(hssf_cell:HSSFCell) { 
    168 //    def get = apply match { 
    169 //      case null => None 
    170 //      case v    => new Some(v) 
    171 //    } 
     195    def get = apply match { 
     196      case null => None 
     197      case v    => new Some(v) 
     198    } 
    172199 
    173200    def apply = hssf_cell.getCellType match { 
     
    179206      case _ => hssf_cell.toString 
    180207    } 
    181      
    182208    def value = apply 
     209     
     210    def set(value:Any, encoding:Short):HSSFCell = { 
     211      import java.util.{Calendar,Date} 
     212 
     213      hssf_cell.setEncoding(encoding) 
     214      value match { 
     215        case v:boolean  => hssf_cell.setCellValue(v) 
     216        case v:Calendar => hssf_cell.setCellValue(v) 
     217        case v:Date     => hssf_cell.setCellValue(v) 
     218        case v:double   => hssf_cell.setCellValue(v) 
     219        case v:Int      => hssf_cell.setCellValue(v.toDouble) 
     220        case v:String   => hssf_cell.setCellValue(new HSSFRichTextString(v)) 
     221      } 
     222      hssf_cell 
     223    } 
     224    def set(value:Any):HSSFCell = set(value, ENCODING_UNCHANGED) 
     225 
     226    def setFormula(value:String) = { 
     227      hssf_cell.setCellFormula(value) 
     228      hssf_cell 
     229    } 
    183230  } 
    184231  implicit def HSSFCellToExtCell(cell:HSSFCell) = new MyCell(cell) 
  • lang/scala/sandbox/src/jp/ryugate/apache/POISpecification.scala

    r7820 r8079  
    77  import jp.ryugate.apache.POI._ 
    88  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._ 
    139  import org.apache.poi.hssf.usermodel.HSSFCellStyle 
     10//  import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator 
    1411   
    1512  import java.lang._ 
     
    4138 
    4239      book.eachSheets(1 to 3) {sheet => sheet must notBeNull} 
     40      book.eachSheetsByName("Sheet1","Sheet3") {sheet => sheet must notBeNull} 
    4341      book.eachSheets(1,10) {sheet => ; } must throwException(new IndexOutOfBoundsException) 
    4442 
     
    6058     
    6159    "get cell" in {      
    62       sheet1.cell(1,1).value must is_==(1.0) 
    63       sheet1.col(1).row(1).value must is_==(1.0) 
    6460      sheet1.row(1).col(1).value must is_==(1.0) 
     61      sheet1.cell("B1").value    must is_==(2.0) 
     62      sheet1.cell(2,1).value     must is_==(2.0) 
     63      sheet1.col(3).row(1).value must is_==(3.0) 
     64 
     65      sheet1.row(1).col(1).get must is_==(Some(1.0)) 
     66      sheet1.cell("B1").get    must is_==(Some(2.0)) 
     67      sheet1.cell(2,1).get     must is_==(Some(2.0)) 
     68      sheet1.col(3).row(1).get must is_==(Some(3.0)) 
     69 
     70      sheet1.cells("A1","C1") 
    6571    } 
    6672 
     
    7278      val sheet = bk.createSheet("test") 
    7379      bk.sheet(1) must notBeNull 
    74       sheet.row(1).setCell(1,"hoge").setCellStyle(style)  
    75       sheet.col(1).setCell(1,"piyo") 
    76       sheet.setCell(3,3,"HOGE").setCellStyle(style) 
     80      sheet.row(1).setCell(1, "hoge").setCellStyle(style)  
     81      sheet.col(1).setCell(1, "piyo") 
     82      sheet.setCell(3,3, "HOGE").setCellStyle(style) 
     83      sheet.setCell("A5", 1) 
     84      sheet.setCell("B5", 2) 
     85      sheet.applyOrCreateCell(5,3).setFormula("A5+B5") 
     86      sheet.row(5).col(2).value must is_==(2.0) 
     87       
     88      sheet.row(10).applyOrCreateCells(1 to 10).foreach {cell => 
     89        cell.setCellStyle(style) 
     90      } 
     91       
     92//      val ev = new HSSFFormulaEvaluator(sheet, bk) 
     93//      ev.evaluate(sheet.col(3).row(5)).getNumberValue must is_==(3.0) 
    7794 
    7895      bk.write(new FileOutputStream("testdata/testoutput.xls"))