Skip to content

Commit 679d7ff

Browse files
committed
- Added new function in org.jetbrains.kotlinx.dataframe.samples.api.Write for keepFile functionality
- Added example of keepFile use in writeExcel to write.md Signed-off-by: LeandroC89 <[email protected]>
1 parent a5f8a1b commit 679d7ff

File tree

2 files changed

+29
-0
lines changed
  • docs/StardustDocs/topics
  • tests/src/test/kotlin/org/jetbrains/kotlinx/dataframe/samples/api

2 files changed

+29
-0
lines changed

docs/StardustDocs/topics/write.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,18 @@ wb.close()
116116
```
117117

118118
<!---END-->
119+
120+
Add new sheets without using Apache POI directly by using a parameter to keep using the same file if it already exists
121+
122+
<!---FUN writeXlsWithMultipleSheets-->
123+
124+
```kotlin
125+
// Create a new Excel workbook with a single sheet called "allPersons", replacing the file if it already exists -> Current sheets: allPersons
126+
val allPersonsSheet = df.writeExcel(file, sheetName = "allPersons")
127+
// Add a new sheet to the previous file without replacing it, by setting keepFile = true -> Current sheets: allPersons, happyPersons
128+
val happyPersonsSheet = df.filter { person -> person.isHappy }.remove("isHappy").writeExcel(file, sheetName = "happyPersons", keepFile = true)
129+
// Add a new sheet to the previous file without replacing it, by setting keepFile = true -> Current sheets: allPersons, happyPersons, unhappyPersons
130+
val unhappyPersonsSheet = df.filter { person -> !person.isHappy }.remove("isHappy").writeExcel(file, sheetName = "unhappyPersons", keepFile = true)
131+
```
132+
133+
<!---END-->

tests/src/test/kotlin/org/jetbrains/kotlinx/dataframe/samples/api/Write.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,20 @@ class Write : TestBase() {
121121
}
122122
}
123123

124+
@Test
125+
fun writeXlsWithMultipleSheets() {
126+
useTempFile { file ->
127+
// SampleStart
128+
// Create a new Excel workbook with a single sheet called "allPersons", replacing the file if it already exists -> Current sheets: allPersons
129+
val allPersonsSheet = df.writeExcel(file, sheetName = "allPersons")
130+
// Add a new sheet to the previous file without replacing it, by setting keepFile = true -> Current sheets: allPersons, happyPersons
131+
val happyPersonsSheet = df.filter { person -> person.isHappy }.remove("isHappy").writeExcel(file, sheetName = "happyPersons", keepFile = true)
132+
// Add a new sheet to the previous file without replacing it, by setting keepFile = true -> Current sheets: allPersons, happyPersons, unhappyPersons
133+
val unhappyPersonsSheet = df.filter { person -> !person.isHappy }.remove("isHappy").writeExcel(file, sheetName = "unhappyPersons", keepFile = true)
134+
// SampleEnd
135+
}
136+
}
137+
124138
companion object {
125139
private fun String.rejoinWithSystemLineSeparator() = rejoinWithLineSeparator(System.lineSeparator())
126140

0 commit comments

Comments
 (0)