当前位置: 动力学知识库 > 问答 > 编程问答 >

apache poi - Groovy ExcelBuilder - help to understand example

问题描述:

I'am trying to understand how this code example works. Unfortunately isn't commented at all.

HSSFRow.metaClass.getAt = { int idx ->

def cell = delegate.getCell(idx)

if(!cell) {

return null

}

def value

switch(cell.cellType) {

case HSSFCell.CELL_TYPE_NUMERIC:

if(HSSFDateUtil.isCellDateFormatted(cell)) {

value = cell.dateCellValue

} else {

value = cell.numericCellValue

}

break

case HSSFCell.CELL_TYPE_BOOLEAN:

value = cell.booleanCellValue

break

default:

value = cell.stringCellValue

break

}

return value

}

}

Can some one please explain me

  1. Why need to override the MetaClass.getAt() method? It's not used directly. How does it work?

  2. What's the cellType, dateCellValue, numericCellValue etc (methods or variables)? Where are they defined?

网友答案:
  1. You don't need to override getAt method but it's overridden to make coding easier. When getAt is overridden it enables using [] syntax when You refer to object as in this line of code return row[idx] (taken from the example). It's operator overloading, see here. Summing up it's not used directly via getAt but with [] syntax.

  2. In groovy when getXXX() method is provided (no arguments) on object x You can just refer to it omitting get so: x.getXXX() is the same as x.XXX. cell is just invocation of getCell() on HSSFRow object, the other methods are invoked on HSSFCell object and exactly the same rule applies.

分享给朋友:
您可能感兴趣的文章:
随机阅读: