Skip to content

Render and fill the template with automatic handling of cell merging functionality 渲染填充模板自动处理单元格合并功能 #397

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

wangmiscoding
Copy link
Contributor

Resolve the issue where newly added rows do not inherit the original row's merge information during template filling:

  1. Only effective when direction=VERTICAL in FillConfig.
  2. Only effective when autoStyle=true in FillConfig.
  3. Only effective when forceNewRow=true in FillConfig.
  4. Only effective when the merge region of the original row does not span across columns.

@wangmiscoding
Copy link
Contributor Author

#378

@wangmiscoding
Copy link
Contributor Author

效果:
image
image

@psxjoy psxjoy requested a review from Copilot July 1, 2025 02:17
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This pull request implements automatic handling of cell merging when filling Excel templates, ensuring that newly added rows inherit merge information under specific FillConfig settings. Key changes include:

  • Addition of a new test case in TemplateMergeTest to validate the merge functionality.
  • Modifications in ExcelWriteFillExecutor to capture original row merged region information and apply these merges to newly added rows.
  • Updates to record and propagate cell styles and merged regions when creating new cells in vertical fill mode with forceNewRow and autoStyle enabled.

Reviewed Changes

Copilot reviewed 2 out of 3 changed files in this pull request and generated 1 comment.

File Description
fastexcel-test/src/test/java/cn/idev/excel/test/core/template/TemplateMergeTest.java Adds a new test ensuring that fill processing properly handles merged cell regions.
fastexcel-core/src/main/java/cn/idev/excel/write/executor/ExcelWriteFillExecutor.java Implements logic to record merged regions in the original row and apply them to new rows.
Comments suppressed due to low confidence (1)

fastexcel-core/src/main/java/cn/idev/excel/write/executor/ExcelWriteFillExecutor.java:77

  • [nitpick] Consider renaming 'originalMergeRegionMap' to 'initialRowMergeRegions' to more clearly indicate that it holds merged region information from the initial row.
    private final Map<AnalysisCell,List<CellRangeAddress>> originalMergeRegionMap = MapUtils.newHashMap();

Comment on lines +139 to +140
for (CellRangeAddress cellAddresses : regionList) {
cachedSheet.addMergedRegionUnsafe(new CellRangeAddress(index, index, cellAddresses.getFirstColumn(), cellAddresses.getLastColumn()));
Copy link
Preview

Copilot AI Jul 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure that the loop range based on 'collectionData.size()' is valid and that the new merged region does not exceed the sheet's boundaries, as using 'addMergedRegionUnsafe' without boundary checks may lead to runtime exceptions.

Suggested change
for (CellRangeAddress cellAddresses : regionList) {
cachedSheet.addMergedRegionUnsafe(new CellRangeAddress(index, index, cellAddresses.getFirstColumn(), cellAddresses.getLastColumn()));
if (index < 0 || index >= cachedSheet.getWorkbook().getSpreadsheetVersion().getMaxRows()) {
continue; // Skip if the row index is out of bounds
}
for (CellRangeAddress cellAddresses : regionList) {
int firstColumn = cellAddresses.getFirstColumn();
int lastColumn = cellAddresses.getLastColumn();
if (firstColumn < 0 || lastColumn >= cachedSheet.getWorkbook().getSpreadsheetVersion().getMaxColumns()) {
continue; // Skip if the column range is out of bounds
}
cachedSheet.addMergedRegionUnsafe(new CellRangeAddress(index, index, firstColumn, lastColumn));

Copilot uses AI. Check for mistakes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant