From 2cd0b991d596d88588a4145659e5be13ff1a4460 Mon Sep 17 00:00:00 2001 From: Romuald Rousseau Date: Fri, 15 Nov 2024 18:01:41 +0800 Subject: [PATCH] fix: Do not generate row when all pivot values are empty --- .../loader/excel/xlsx/ContentHandler.java | 18 ++-- .../archery/intelli/IntelliTable.java | 87 ++++++++++--------- 2 files changed, 59 insertions(+), 46 deletions(-) diff --git a/archery-excel/src/main/java/com/github/romualdrousseau/archery/loader/excel/xlsx/ContentHandler.java b/archery-excel/src/main/java/com/github/romualdrousseau/archery/loader/excel/xlsx/ContentHandler.java index 0f14e299..aa7efbf6 100644 --- a/archery-excel/src/main/java/com/github/romualdrousseau/archery/loader/excel/xlsx/ContentHandler.java +++ b/archery-excel/src/main/java/com/github/romualdrousseau/archery/loader/excel/xlsx/ContentHandler.java @@ -67,16 +67,20 @@ public DataFrame getRows() throws IOException { public void startElement(final String uri, final String localName, final String name, final Attributes attributes) { if ("row".equals(name)) { - assert (attributes.getValue("r") != null) : "Row malformed without ref"; - this.fillMissingRows(Integer.valueOf(attributes.getValue("r")) - 1); - this.row = new ArrayList(); - this.prevCell = null; - this.currCell = null; + if (attributes.getValue("r") != null) { + if (attributes.getValue("r") != null) { + this.fillMissingRows(Integer.valueOf(attributes.getValue("r")) - 1); + } + this.row = new ArrayList(); + this.prevCell = null; + this.currCell = null; + } } else if ("c".equals(name)) { - assert (attributes.getValue("r") != null) : "Cell malformed without ref"; this.prevCell = this.currCell; this.currCell = new Cell(); - this.currCell.address = new CellAddress(attributes.getValue("r")); + this.currCell.address = (attributes.getValue("r") != null) + ? new CellAddress(attributes.getValue("r")) + : CellAddress.A1; this.currCell.type = this.getCellTypeFromString(attributes.getValue("t")); this.currCell.style = this.getCellStyleFromString(attributes.getValue("s")); } else if ("v".equals(name)) { diff --git a/archery/src/main/java/com/github/romualdrousseau/archery/intelli/IntelliTable.java b/archery/src/main/java/com/github/romualdrousseau/archery/intelli/IntelliTable.java index 38bb8467..0a4071a1 100644 --- a/archery/src/main/java/com/github/romualdrousseau/archery/intelli/IntelliTable.java +++ b/archery/src/main/java/com/github/romualdrousseau/archery/intelli/IntelliTable.java @@ -1,6 +1,7 @@ package com.github.romualdrousseau.archery.intelli; import java.util.List; +import java.util.Optional; import java.io.IOException; import java.io.UncheckedIOException; import java.util.ArrayList; @@ -127,57 +128,69 @@ private List buildRowsForOneRow(final BaseTableGraph graph, final DataTable } if (pivot == null) { - newRows.add(buildOneRowWithoutPivot(graph, orgTable, orgRow, rowGroup)); + buildOneRowWithoutPivot(graph, orgTable, orgRow, rowGroup).ifPresent(newRows::add); } else if (this.getSheet().getPivotOption() == PivotOption.WITH_TYPE_AND_VALUE) { - for (final var value : pivot.getEntryValues()) { - newRows.add(buildOneRowWithPivotTypeAndValue(graph, orgTable, orgRow, pivot, value, rowGroup)); - } + pivot.getEntryValues() + .forEach(x -> buildOneRowWithPivotTypeAndValue(graph, orgTable, orgRow, pivot, x, rowGroup) + .ifPresent(newRows::add)); } else if (this.getSheet().getPivotOption() == PivotOption.WITH_TYPE) { - for (final var pivotEntry : pivot.getEntries()) { - if (orgRow.getCellAt(pivotEntry.getCell().getColumnIndex()).hasValue()) { - newRows.add(buildOneRowWithPivotAndType(graph, orgTable, orgRow, pivotEntry, rowGroup)); - } - } + pivot.getEntries() + .forEach(x -> buildOneRowWithPivotAndType(graph, orgTable, orgRow, x, rowGroup) + .ifPresent(newRows::add)); } else { - for (final var pivotEntry : pivot.getEntries()) { - if (orgRow.getCellAt(pivotEntry.getCell().getColumnIndex()).hasValue()) { - newRows.add(buildOneRowWithPivot(graph, orgTable, orgRow, pivotEntry, rowGroup)); - } - } + pivot.getEntries() + .forEach(x -> buildOneRowWithPivot(graph, orgTable, orgRow, x, rowGroup) + .ifPresent(newRows::add)); } return newRows; } - private Row buildOneRowWithoutPivot(final BaseTableGraph graph, final DataTable orgTable, + private Optional buildOneRowWithoutPivot(final BaseTableGraph graph, final DataTable orgTable, final BaseRow orgRow, final RowGroup rowGroup) { final var newRow = new Row(this.tmpHeaders.size()); for (final var abstractHeader : this.tmpHeaders) { final var orgHeaders = orgTable.findAllHeaders(abstractHeader); this.generateCells(graph, orgHeaders, abstractHeader, rowGroup, orgRow, newRow); } - return newRow; + return Optional.of(newRow); } - private Row buildOneRowWithPivot(final BaseTableGraph graph, final DataTable orgTable, - final BaseRow orgRow, final PivotEntry pivotEntry, final RowGroup rowGroup) { + private Optional buildOneRowWithPivotTypeAndValue(final BaseTableGraph graph, final DataTable orgTable, + final BaseRow orgRow, final PivotKeyHeader pivot, final String value, final RowGroup rowGroup) { final var newRow = new Row(this.tmpHeaders.size()); + boolean hasPivotedValues = false; for (final var abstractHeader : this.tmpHeaders) { final var orgHeaders = orgTable.findAllHeaders(abstractHeader); if (abstractHeader instanceof PivotKeyHeader) { if (orgHeaders.size() > 0) { - newRow.set(abstractHeader.getColumnIndex() + 0, pivotEntry.getCell().getValue()); - newRow.set(abstractHeader.getColumnIndex() + 1, - orgRow.getCellAt(pivotEntry.getCell().getColumnIndex()).getValue()); + newRow.set(abstractHeader.getColumnIndex(), value); + int i = 1; + for (final var typeValue : pivot.getEntryTypes()) { + final var ci = abstractHeader.getColumnIndex() + i; + hasPivotedValues |= pivot.getEntries().stream() + .filter(x -> x.getValue().equals(value) && x.getTypeValue().equals(typeValue)) + .findFirst() + .map(x -> { + newRow.set(ci, orgRow.getCellAt(x.getCell().getColumnIndex()).getValue()); + return orgRow.getCellAt(x.getCell().getColumnIndex()).hasValue(); + }) + .orElse(false); + + i++; + } } } else { this.generateCells(graph, orgHeaders, abstractHeader, rowGroup, orgRow, newRow); } } - return newRow; + return hasPivotedValues ? Optional.of(newRow) : Optional.empty(); } - private Row buildOneRowWithPivotAndType(final BaseTableGraph graph, final DataTable orgTable, + private Optional buildOneRowWithPivotAndType(final BaseTableGraph graph, final DataTable orgTable, final BaseRow orgRow, final PivotEntry pivotEntry, final RowGroup rowGroup) { + if (!orgRow.getCellAt(pivotEntry.getCell().getColumnIndex()).hasValue()) { + return Optional.empty(); + } final var newRow = new Row(this.tmpHeaders.size()); for (final var abstractHeader : this.tmpHeaders) { final var orgHeaders = orgTable.findAllHeaders(abstractHeader); @@ -192,33 +205,28 @@ private Row buildOneRowWithPivotAndType(final BaseTableGraph graph, final DataTa this.generateCells(graph, orgHeaders, abstractHeader, rowGroup, orgRow, newRow); } } - return newRow; + return Optional.of(newRow); } - private Row buildOneRowWithPivotTypeAndValue(final BaseTableGraph graph, final DataTable orgTable, - final BaseRow orgRow, final PivotKeyHeader pivot, final String value, final RowGroup rowGroup) { + private Optional buildOneRowWithPivot(final BaseTableGraph graph, final DataTable orgTable, + final BaseRow orgRow, final PivotEntry pivotEntry, final RowGroup rowGroup) { + if (!orgRow.getCellAt(pivotEntry.getCell().getColumnIndex()).hasValue()) { + return Optional.empty(); + } final var newRow = new Row(this.tmpHeaders.size()); for (final var abstractHeader : this.tmpHeaders) { final var orgHeaders = orgTable.findAllHeaders(abstractHeader); if (abstractHeader instanceof PivotKeyHeader) { if (orgHeaders.size() > 0) { - newRow.set(abstractHeader.getColumnIndex(), value); - int i = 1; - for (final var typeValue : pivot.getEntryTypes()) { - final var ci = abstractHeader.getColumnIndex() + i; - pivot.getEntries().stream() - .filter(x -> x.getValue().equals(value) && x.getTypeValue().equals(typeValue)) - .findFirst() - .ifPresent( - x -> newRow.set(ci, orgRow.getCellAt(x.getCell().getColumnIndex()).getValue())); - i++; - } + newRow.set(abstractHeader.getColumnIndex() + 0, pivotEntry.getCell().getValue()); + newRow.set(abstractHeader.getColumnIndex() + 1, + orgRow.getCellAt(pivotEntry.getCell().getColumnIndex()).getValue()); } } else { this.generateCells(graph, orgHeaders, abstractHeader, rowGroup, orgRow, newRow); } } - return newRow; + return Optional.of(newRow); } private void generateCells(final BaseTableGraph graph, final List orgHeaders, @@ -244,7 +252,8 @@ private void generateCells(final BaseTableGraph graph, final List or } } - private void generateCell(final BaseHeader abstractHeader, final String prevValue, final String currValue, final Row newRow) { + private void generateCell(final BaseHeader abstractHeader, final String prevValue, final String currValue, + final Row newRow) { abstractHeader.setColumnEmpty(abstractHeader.isColumnEmpty() && StringUtils.isFastBlank(currValue)); if (prevValue == null) { newRow.set(abstractHeader.getColumnIndex(), currValue);