Skip to content

Instantly share code, notes, and snippets.

#[derive(Serialize,Deserialize)]
struct Posts {
children: Vec<Post>
}
{
"kind": "Listing",
"data": {
"children": [....]
}
}
fun Sheet.row(rowNumber: Int? = null, block: Row.() -> Unit): Row {
return createRow(rowNumber ?: physicalNumberOfRows).apply(block)
}
fun Row.cell(value: Any? = null, column: Int? = null, block: Cell.() -> Unit = {}): Cell {
val cell = createCell(column ?: physicalNumberOfCells)
when (value) {
is Calendar -> cell.setCellValue(value)
is Date -> cell.setCellValue(value)
is Boolean -> cell.setCellValue(value)
fun Workbook.sheet(name: String? = null, block: Sheet.() -> Unit = {}): Sheet {
return if (name != null) {
createSheet(name)
} else {
createSheet()
}.apply(block)
}
public void createWorkbook() {
Workbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet();
Row row = sheet.createRow(0);
row.createCell(0);
}
fun workbook(wb: Workbook = HSSFWorkbook(), block: Workbook.() -> Unit = {}): Workbook {
block(wb)
return wb
}
fun tryItOut() {
// use the default HSSFWorkbook
workbook {
}
fun tryItOut() {
// Also since the block is the only parameter we can even drop the parenthesis all together!
workbook {
// We can directly set the value for isHidden!
isHidden = true
// We cannot change setSelectedTab because Workbook only has a setter for this and no getter (because POI is a PITA).
// This prevents Kotlin from autogenerating the "synthetic accessor"
setSelectedTab(2)
}
// This example is lovingly lifted directly from the Kotlin documentation
class Foo() {
// Note that in Kotlin variable types are inferred, you can say "var" (or val if you want it immutable) instead of specifying the type.
// Here we are defining a field named "counter" with a value of 1.
var counter = 0
// Here we are providing a setter that only updates the field if the new value is positive.
set(value) {
if (value >= 0) field = value
}
}
// Notice the parameter is now of type "Workbook.() -> Unit" rather than "(Workbook) -> Unit"
fun workbook(block: Workbook.() -> Unit): Workbook {
var wb = HSSFWorkbook()
block(wb)
return wb
}
fun tryItOut() {
// Since we defined the lambda as using a Receiver Type of "Workbook.()" we can now directly call the methods without referencing the paramter
// In Kotlin function definitions start with the keywork "fun"
// Additionally Kotlin lets you have "top level" functions that do not need to be inside a class
// "(Workbook) -> Unit" means "a function that takes a workbook and returns nothing (Unit)
fun workbook(block: (Workbook) -> Unit): Workbook {
var wb = HSSFWorkbook()
// Note that in Kotlin we can direcly call "block" rather than needing to call "accept" on it
block(wb)
return wb
}