Skip to content

Instantly share code, notes, and snippets.

@tg44
Created January 15, 2020 16:20
Show Gist options
  • Save tg44/8aa4ba088c6c91262063bf39b3c8a785 to your computer and use it in GitHub Desktop.
Save tg44/8aa4ba088c6c91262063bf39b3c8a785 to your computer and use it in GitHub Desktop.
Spoiwo Grid Examples
"Grid" should {
import com.norbitltd.spoiwo.natures.xlsx.Model2XlsxConversions._
def generateGrid(n: Int, m: Int, c: Color) = {
val cell = Cell("").withStyle(CellStyle(fillForegroundColor = c, fillPattern = CellFill.Solid))
val row = Row().withCells(Seq.fill(m)(cell))
Grid { f =>
Seq.fill(n)(row)
}
}
def generateGridWithFormula(n: Int, m: Int, c: Color) = {
val cell = Cell("").withStyle(CellStyle(fillForegroundColor = c, fillPattern = CellFill.Solid))
val row = Row().withCells(Seq.fill(m)(cell))
Grid { f =>
def cellWithFormula(x: Int) =
Cell(s"=${f.getCellByIndexes(x, 0)}")
.withStyle(CellStyle(fillForegroundColor = c, fillPattern = CellFill.Solid))
val rowWithFormula = Row().withCells((1 to m).map(cellWithFormula))
Seq.fill(n - 1)(row) ++ Seq(rowWithFormula)
}
}
"render" in {
import Grid._
val rows = Grid.render(generateGrid(3, 3, Color.LightBlue))
Sheet(name = "new sheet").withRows(rows).saveAsXlsx("test.xlsx")
}
"compose horizontal" in {
val g1 = generateGrid(5, 5, Color.LightBlue)
val g2 = generateGrid(2, 2, Color.Green)
val g3 = generateGrid(7, 7, Color.IndianRed)
val g = Grid.addHorizontal(g1, Grid.addHorizontal(g2, g3))
val rows = Grid.render(g)
Sheet(name = "new sheet").withRows(rows).saveAsXlsx("test.xlsx")
}
"compose vertical" in {
val g1 = generateGrid(5, 5, Color.LightBlue)
val g2 = generateGrid(2, 2, Color.Green)
val g3 = generateGrid(7, 7, Color.IndianRed)
val g = Grid.addVertical(g1, Grid.addVertical(g2, g3))
val rows = Grid.render(g)
Sheet(name = "new sheet").withRows(rows).saveAsXlsx("test.xlsx")
}
"compose complex" in {
val g1 = generateGrid(5, 5, Color.LightBlue)
val g2 = generateGrid(2, 2, Color.Green)
val g3 = generateGrid(7, 7, Color.IndianRed)
val g4 = generateGrid(12, 8, Color.LightYellow)
val gc1 = Grid.addHorizontal(g1, g2)
val gc2 = Grid.addVertical(gc1, g3)
val gc3 = Grid.addHorizontal(gc2, g4)
val rows = Grid.render(gc3)
Sheet(name = "new sheet").withRows(rows).saveAsXlsx("test.xlsx")
}
"formulas move" in {
val g1 = generateGrid(5, 5, Color.LightBlue)
val g2 = generateGridWithFormula(6, 6, Color.Green)
val gc1 = Grid.addHorizontal(g1, g2)
val rows = Grid.render(gc1)
Sheet(name = "new sheet").withRows(rows).saveAsXlsx("test.xlsx")
}
"cellMerge works" in {
val g1 = generateGrid(5, 5, Color.LightBlue).addMergedRegion(CellRange(2 -> 3, 2 -> 3))
val g2 = generateGrid(2, 2, Color.Green)
val g3 = generateGrid(7, 7, Color.IndianRed).addMergedRegion(CellRange(2 -> 5, 2 -> 5))
val g4 = generateGrid(12, 8, Color.LightYellow).addMergedRegion(CellRange(2 -> 3, 2 -> 3))
val gc1 = Grid.addHorizontal(g1, g2)
val gc2 = Grid.addVertical(gc1, g3)
val gc3 = Grid.addHorizontal(gc2, g4)
val rows = Grid.render(gc3)
Sheet(name = "new sheet").withRows(rows).withMergedRegions(gc3.mergedRegions).saveAsXlsx("test.xlsx")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment