Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export different types in multiple sheets #598

Merged
merged 2 commits into from
Oct 4, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 55 additions & 42 deletions src/main/scala/ch/openolitor/core/RouteService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,12 @@ trait DefaultRouteService extends HttpService with ActorReferences with BaseJson
val sheet = dataDocument.getSheetByIndex(0)
sheet.setCellStyleInheritance(false)

def createNewSheet(name: String): Table = {
val newSheet = dataDocument.appendSheet(name)
newSheet.setCellStyleInheritance(false)
newSheet
}

def writeToRow(row: Row, element: Any, cellIndex: Int): Unit = {
element match {
case null =>
Expand All @@ -343,62 +349,69 @@ trait DefaultRouteService extends HttpService with ActorReferences with BaseJson
}

result match {
case genericList: List[Any] =>
case genericList: List[Any] => {
if (genericList.nonEmpty) {
genericList.head match {
case firstMapEntry: Map[_, _] =>
val listOfMaps = genericList.asInstanceOf[List[Map[String, Any]]]
val row = sheet.getRowByIndex(0);

listOfMaps.head.zipWithIndex foreach {
case ((fieldName, value), index) =>
row.getCellByIndex(index).setStringValue(fieldName)
val font = row.getCellByIndex(index).getFont
font.setFontStyle(StyleTypeDefinitions.FontStyle.BOLD)
font.setSize(10)
row.getCellByIndex(index).setFont(font)
}
genericList.groupBy(_.getClass) map {
case (clazz, clazzSortedList) => {
var clazzSheet = createNewSheet(clazz.getSimpleName())
clazzSortedList.head match {
case firstMapEntry: Map[_, _] =>
val listOfMaps = clazzSortedList.asInstanceOf[List[Map[String, Any]]]
val row = clazzSheet.getRowByIndex(0);

listOfMaps.head.zipWithIndex foreach {
case ((fieldName, value), index) =>
row.getCellByIndex(index).setStringValue(fieldName)
val font = row.getCellByIndex(index).getFont
font.setFontStyle(StyleTypeDefinitions.FontStyle.BOLD)
font.setSize(10)
row.getCellByIndex(index).setFont(font)
}

listOfMaps.zipWithIndex foreach {
case (entry, index) =>
val row = sheet.getRowByIndex(index + 1);
listOfMaps.zipWithIndex foreach {
case (entry, index) =>
val row = clazzSheet.getRowByIndex(index + 1);

entry.zipWithIndex foreach {
case ((fieldName, value), colIndex) =>
fieldName match {
case "passwort" => writeToRow(row, "Not available", colIndex)
case _ => writeToRow(row, value, colIndex)
entry.zipWithIndex foreach {
case ((fieldName, value), colIndex) =>
fieldName match {
case "passwort" => writeToRow(row, "Not available", colIndex)
case _ => writeToRow(row, value, colIndex)
}
}
}
}

case firstProductEntry: Product =>
val listOfProducts = genericList.asInstanceOf[List[Product]]
val row = sheet.getRowByIndex(0);
case firstProductEntry: Product =>
val listOfProducts = clazzSortedList.asInstanceOf[List[Product]]
val row = clazzSheet.getRowByIndex(0);

def getCCParams(cc: Product) = cc.getClass.getDeclaredFields.map(_.getName) // all field names
.zip(cc.productIterator.to).toMap // zipped with all values
def getCCParams(cc: Product) = cc.getClass.getDeclaredFields.map(_.getName) // all field names
.zip(cc.productIterator.to).toMap // zipped with all values

getCCParams(listOfProducts.head).zipWithIndex foreach {
case ((fieldName, value), index) =>
row.getCellByIndex(index).setStringValue(fieldName)
val font = row.getCellByIndex(index).getFont
font.setFontStyle(StyleTypeDefinitions.FontStyle.BOLD)
font.setSize(10)
row.getCellByIndex(index).setFont(font)
}
getCCParams(listOfProducts.head).zipWithIndex foreach {
case ((fieldName, value), index) =>
row.getCellByIndex(index).setStringValue(fieldName)
val font = row.getCellByIndex(index).getFont
font.setFontStyle(StyleTypeDefinitions.FontStyle.BOLD)
font.setSize(10)
row.getCellByIndex(index).setFont(font)
}

listOfProducts.zipWithIndex foreach {
case (entry, index) =>
val row = sheet.getRowByIndex(index + 1);
listOfProducts.zipWithIndex foreach {
case (entry, index) =>
val row = clazzSheet.getRowByIndex(index + 1);

getCCParams(entry).zipWithIndex foreach {
case ((fieldName, value), colIndex) =>
writeToRow(row, value, colIndex)
getCCParams(entry).zipWithIndex foreach {
case ((fieldName, value), colIndex) =>
writeToRow(row, value, colIndex)
}
}
}
}
}
}
dataDocument.removeSheet(0)
}
case x => sheet.getRowByIndex(0).getCellByIndex(0).setStringValue("Data of type" + x.toString + " could not be transfered to ODS file.")
}

Expand Down