Skip to content

Commit

Permalink
Copy Excel AutoFilters when copying XSSFSheets
Browse files Browse the repository at this point in the history
  • Loading branch information
alexpartsch committed Sep 26, 2023
1 parent 074cb7d commit 10376e3
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 5 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Expand Up @@ -8,7 +8,7 @@ plugins {
}

group 'com.docutools'
version = '1.6.8'
version = '1.6.9'

java {
toolchain {
Expand Down
Expand Up @@ -17,6 +17,7 @@
import org.apache.poi.ss.usermodel.Hyperlink;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.streaming.SXSSFFormulaEvaluator;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
Expand All @@ -25,6 +26,7 @@
import org.apache.poi.xssf.usermodel.XSSFPicture;
import org.apache.poi.xssf.usermodel.XSSFPictureData;
import org.apache.poi.xssf.usermodel.XSSFShape;
import org.apache.poi.xssf.usermodel.XSSFSheet;

/**
* This is a streamed implementation of the {@link com.docutools.jocument.impl.excel.interfaces.ExcelWriter} interface. The streaming is done so
Expand Down Expand Up @@ -130,6 +132,16 @@ public void newSheet(Sheet sheet) {
currentSheet.setSelected(sheet.isSelected());
currentSheet.setVerticallyCenter(sheet.getVerticallyCenter());

// copy auto filters to new sheet
if(sheet instanceof XSSFSheet xssfSheet) {
var autoFilter = xssfSheet.getCTWorksheet().getAutoFilter();
if(autoFilter != null) {
var ref = autoFilter.getRef();
var range = CellRangeAddress.valueOf(ref);
currentSheet.setAutoFilter(range);
}
}

var drawing = (XSSFDrawing) sheet.createDrawingPatriarch();
for (var shape : drawing.getShapes()) {
if (shape instanceof XSSFPicture) {
Expand Down
33 changes: 29 additions & 4 deletions src/test/java/com/docutools/jocument/excel/AutomatedXlsxTests.java
Expand Up @@ -3,6 +3,7 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;

import com.docutools.jocument.Document;
import com.docutools.jocument.PlaceholderResolver;
Expand All @@ -11,16 +12,15 @@
import com.docutools.jocument.impl.ReflectionResolver;
import com.docutools.jocument.sample.model.SampleModelData;
import com.docutools.poipath.xssf.XSSFWorkbookWrapper;
import com.docutools.poipath.xwpf.XWPFDocumentWrapper;
import java.awt.Desktop;
import java.io.IOException;
import org.apache.poi.ss.util.CellRangeAddress;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

@DisplayName("Word Generator Tests")
@DisplayName("Excel Generator Tests")
@Tag("automated")
@Tag("xwpf")
@Tag("xssf")
class AutomatedXlsxTests {

@Test
Expand All @@ -41,4 +41,29 @@ void copiesHyperlink() throws InterruptedException, IOException {
assertThat(documentWrapper.sheet(0).row(0).cell(0).text(), equalTo("orf.at"));
assertThat(documentWrapper.sheet(0).row(0).cell(0).cell().getHyperlink().getAddress(), equalTo("https://orf.at/"));
}

@Test
@DisplayName("Keep Auto Filter")
void keepAutoFilters() throws InterruptedException, IOException {
// Arrange
Template template = Template.fromClassPath("/templates/excel/AutoFilters.xlsx")
.orElseThrow();
PlaceholderResolver resolver = new ReflectionResolver(SampleModelData.PICARD);

// Act
Document document = template.startGeneration(resolver);
document.blockUntilCompletion(5_000L); // 5 seconds

// Assert
assertThat(document.completed(), is(true));
var xssfWorkbook = TestUtils.getXSSFWorkbookFromDocument(document);
var xssf = new XSSFWorkbookWrapper(xssfWorkbook);
var sheet = xssf.sheet(0);
var autoFilter = sheet.sheet().getCTWorksheet().getAutoFilter();
assertThat(autoFilter, notNullValue());
var autoFilterRef = autoFilter.getRef();
var rangeAddress = CellRangeAddress.valueOf(autoFilterRef);
assertThat(rangeAddress.isInRange(sheet.row(0).cell(0).cell()), is(true));
}

}
17 changes: 17 additions & 0 deletions src/test/java/com/docutools/jocument/excel/ExcelDocuments.java
Expand Up @@ -160,4 +160,21 @@ void inheritStylePerCell() throws InterruptedException, IOException {

Desktop.getDesktop().open(document.getPath().toFile());
}

@Test
@DisplayName("Keep Auto Filter")
void keepAutoFilters() throws InterruptedException, IOException {
// Arrange
Template template = Template.fromClassPath("/templates/excel/AutoFilters.xlsx")
.orElseThrow();
PlaceholderResolver resolver = new ReflectionResolver(SampleModelData.PICARD);

// Act
Document document = template.startGeneration(resolver);
document.blockUntilCompletion(5_000L); // 5 seconds

// Assert
assertThat(document.completed(), is(true));
Desktop.getDesktop().open(document.getPath().toFile());
}
}
Binary file not shown.

0 comments on commit 10376e3

Please sign in to comment.