Skip to content

Commit eee924f

Browse files
fix: span row group to the last row of the sheet (#10183) (CP: 24.10) (#10212)
Co-authored-by: totally-not-ai[bot] <290682512+totally-not-ai[bot]@users.noreply.github.com> Fixes #10164
1 parent b221085 commit eee924f

6 files changed

Lines changed: 95 additions & 7 deletions

File tree

‎vaadin-spreadsheet-flow-parent/vaadin-spreadsheet-flow-integration-tests/src/test/java/com/vaadin/flow/component/spreadsheet/test/GroupingIT.java‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ public class GroupingIT extends AbstractSpreadsheetIT {
2525

2626
private static final String IMAGE_CSS_SELECTOR = "div.sheet-image";
2727

28+
/**
29+
* The last row of row_group_at_last_row.xlsx, which is also the last row of
30+
* the row group in that file, 1-based.
31+
*/
32+
private static final int LAST_ROW = 205;
33+
private static final int GROUPED_ROWS = 4;
34+
2835
@Before
2936
public void init() {
3037
open();
@@ -99,6 +106,41 @@ public void grouping_expandRowGroup_imageInsideRowExpand()
99106
Assert.assertTrue("Image height", Float.parseFloat(height) > 300);
100107
}
101108

109+
@Test
110+
public void grouping_rowGroupEndsOnLastRow_groupSpansRowsCollapsesAndExpands() {
111+
loadFile("row_group_at_last_row.xlsx");
112+
SpreadsheetElement spreadsheetElement = $(SpreadsheetElement.class)
113+
.first();
114+
// the group is at the end of the sheet, scroll it into view
115+
spreadsheetElement.scroll(100000);
116+
waitUntil(driver -> !findElementsInShadowRoot(
117+
By.cssSelector(".rh.row" + LAST_ROW)).isEmpty());
118+
119+
WebElement rowGrouping = findElementInShadowRoot(
120+
By.cssSelector(".row-group-pane .grouping.minus"));
121+
int rowHeight = spreadsheetElement.getRowHeader(LAST_ROW).getSize()
122+
.getHeight();
123+
Assert.assertTrue("Row grouping height",
124+
rowGrouping.getSize().getHeight() >= GROUPED_ROWS * rowHeight);
125+
126+
// the collapse icon is at the end of the group, not at its start
127+
WebElement collapseIcon = rowGrouping
128+
.findElement(By.cssSelector(".expand"));
129+
Assert.assertTrue("Collapse icon position",
130+
collapseIcon.getLocation().getY() >= spreadsheetElement
131+
.getRowHeader(LAST_ROW).getLocation().getY());
132+
133+
collapseIcon.click();
134+
waitUntil(driver -> !spreadsheetElement.getRowHeader(LAST_ROW)
135+
.isDisplayed());
136+
137+
findElementInShadowRoot(
138+
By.cssSelector(".row-group-pane .grouping.plus .expand"))
139+
.click();
140+
waitUntil(driver -> spreadsheetElement.getRowHeader(LAST_ROW)
141+
.isDisplayed());
142+
}
143+
102144
// HELPERS
103145
private SpreadsheetElement loadImageFile() {
104146
loadFile("group_image.xlsx");

‎vaadin-spreadsheet-flow-parent/vaadin-spreadsheet-flow/src/main/java/com/vaadin/flow/component/spreadsheet/GroupingUtil.java‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -559,18 +559,19 @@ public static boolean checkHidden(CTCols colsArray, CTCol col,
559559
* given level. 0-based.
560560
*/
561561
public static long findEndOfRowGroup(Spreadsheet sheet, int rowindex,
562-
XSSFRow row, short level) {
562+
short level) {
563563

564564
while (rowindex < sheet.getRows()) {
565565
XSSFRow r = (XSSFRow) sheet.getActiveSheet().getRow(rowindex);
566566
if (r == null || r.getCTRow().getOutlineLevel() < level) {
567-
// end
568-
return rowindex - 1l;
567+
break;
569568
}
570569

571570
rowindex++;
572571
}
573-
return -1l;
572+
// the group ends on the row before the first row that is not part of
573+
// it, or on the last row of the sheet
574+
return rowindex - 1l;
574575
}
575576

576577
/**

‎vaadin-spreadsheet-flow-parent/vaadin-spreadsheet-flow/src/main/java/com/vaadin/flow/component/spreadsheet/Spreadsheet.java‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6128,8 +6128,7 @@ protected void levelHeaderClicked(boolean isCols, int level) {
61286128

61296129
// group starts here
61306130

6131-
int end = (int) GroupingUtil.findEndOfRowGroup(this, i, row,
6132-
l);
6131+
int end = (int) GroupingUtil.findEndOfRowGroup(this, i, l);
61336132
long uniqueIndex = GroupingUtil.findUniqueRowIndex(this, i,
61346133
end, l);
61356134

‎vaadin-spreadsheet-flow-parent/vaadin-spreadsheet-flow/src/main/java/com/vaadin/flow/component/spreadsheet/SpreadsheetFactory.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -799,7 +799,7 @@ static void loadGrouping(Spreadsheet spreadsheet) {
799799
lastlevel++;
800800

801801
int end = (int) GroupingUtil.findEndOfRowGroup(spreadsheet,
802-
i, row, lastlevel);
802+
i, lastlevel);
803803
long uniqueIndex = GroupingUtil
804804
.findUniqueRowIndex(spreadsheet, i, end, lastlevel);
805805

‎vaadin-spreadsheet-flow-parent/vaadin-spreadsheet-flow/src/test/java/com/vaadin/flow/component/spreadsheet/tests/GroupingTest.java‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
*/
99
package com.vaadin.flow.component.spreadsheet.tests;
1010

11+
import org.apache.poi.ss.usermodel.Sheet;
12+
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
1113
import org.junit.Assert;
1214
import org.junit.Before;
1315
import org.junit.Ignore;
@@ -332,6 +334,46 @@ public void clickColumnLevelHeader_clickLastLevelHeader_shouldUnhideGroups() {
332334
Assert.assertFalse(spreadsheet.isColumnHidden(4));
333335
}
334336

337+
@Test
338+
public void groupEndingOnLastRow_groupSpansToLastRow() {
339+
spreadsheet = createSpreadsheetWithGroupEndingOnLastRow();
340+
341+
Assert.assertEquals(
342+
"[{\"startIndex\":" + LAST_ROW_GROUP + ",\"endIndex\":"
343+
+ (ROW_COUNT - 1) + ",\"level\":1,\"uniqueIndex\":"
344+
+ LAST_ROW_GROUP + "}]",
345+
spreadsheet.getElement().getProperty("rowGroupingData"));
346+
}
347+
348+
@Test
349+
public void groupEndingOnLastRow_clickRowLevelHeaders_rowHiddenAndVisibleAgain() {
350+
spreadsheet = createSpreadsheetWithGroupEndingOnLastRow();
351+
352+
clickRowLevelHeader(1);
353+
Assert.assertTrue(spreadsheet.isRowHidden(ROW_COUNT - 1));
354+
clickRowLevelHeader(2);
355+
Assert.assertFalse(spreadsheet.isRowHidden(ROW_COUNT - 1));
356+
}
357+
358+
/**
359+
* Creates a spreadsheet with a single row group that ends on the last row
360+
* of the sheet, which is also the last row of the component.
361+
*/
362+
private Spreadsheet createSpreadsheetWithGroupEndingOnLastRow() {
363+
var workbook = new XSSFWorkbook();
364+
Sheet sheet = workbook.createSheet();
365+
for (int i = 0; i < ROW_COUNT; i++) {
366+
sheet.createRow(i).createCell(0).setCellValue("Row " + (i + 1));
367+
}
368+
sheet.groupRow(LAST_ROW_GROUP, ROW_COUNT - 1);
369+
370+
// no empty rows are added after the last row of the sheet
371+
var result = new Spreadsheet(ROW_COUNT, COLUMN_COUNT);
372+
result.setWorkbook(workbook);
373+
TestHelper.fireClientEvent(result, "onSheetScroll", "[1, 1, 1, 1]");
374+
return result;
375+
}
376+
335377
private void setActiveSheet(int sheetIndex) {
336378
spreadsheet.setActiveSheetIndex(sheetIndex);
337379
TestHelper.fireClientEvent(spreadsheet, "onSheetScroll",
@@ -392,4 +434,8 @@ private void clickColumnLevelHeader(int level) {
392434
private final int SHEET9_COLUMN_GROUP = 1;
393435
private final int SHEET9_COLUMN = 3;
394436

437+
private final int ROW_COUNT = 10;
438+
private final int COLUMN_COUNT = 5;
439+
private final int LAST_ROW_GROUP = ROW_COUNT - 4;
440+
395441
}

0 commit comments

Comments
 (0)