fix: fix error in using relativeHeadRowIndex for writing tables only#810
fix: fix error in using relativeHeadRowIndex for writing tables only#81032154678925 wants to merge 3 commits intoapache:mainfrom
Conversation
There was a problem hiding this comment.
Pull request overview
This pull request fixes a bug where relativeHeadRowIndex() was being applied twice when writing data to tables with headers, causing extra blank rows to appear between the header and data. The fix changes the condition from checking whether the sheet is new to checking whether the current write holder (which could be a table) is new and has no head.
Changes:
- Modified logic in
ExcelWriteAddExecutor.add()to checkcurrentWriteHolderinstead ofwriteSheetHolderfor applying the row index offset - Added import for
AbstractWriteHolderclass - Added instanceof check before casting to
AbstractWriteHolder(unnecessary based on codebase architecture)
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| if (currentWriteHolder instanceof AbstractWriteHolder) { | ||
| AbstractWriteHolder writeHolder = (AbstractWriteHolder) currentWriteHolder; | ||
| if (!writeHolder.getExcelWriteHeadProperty().hasHead()) { | ||
| newRowIndex += currentWriteHolder.relativeHeadRowIndex(); | ||
| } |
There was a problem hiding this comment.
The instanceof check for AbstractWriteHolder is unnecessary. All implementations of WriteHolder (WriteSheetHolder, WriteTableHolder, WriteWorkbookHolder) extend AbstractWriteHolder. The rest of the codebase directly casts currentWriteHolder() to AbstractWriteHolder without instanceof checks (see WriteHandlerUtils.java lines 79, 124, 160, 168, 176, 184). For consistency with the existing codebase patterns, the cast should be done directly without the instanceof check.
| if (currentWriteHolder instanceof AbstractWriteHolder) { | |
| AbstractWriteHolder writeHolder = (AbstractWriteHolder) currentWriteHolder; | |
| if (!writeHolder.getExcelWriteHeadProperty().hasHead()) { | |
| newRowIndex += currentWriteHolder.relativeHeadRowIndex(); | |
| } | |
| AbstractWriteHolder writeHolder = (AbstractWriteHolder) currentWriteHolder; | |
| if (!writeHolder.getExcelWriteHeadProperty().hasHead()) { | |
| newRowIndex += currentWriteHolder.relativeHeadRowIndex(); |
Purpose of the pull request
Fixes(#794)
When you do not write directly in the
sheet, but only write data in thetableand set thehead, the offset value set withrelativeHeadRowIndex()will make an error, and the blank line with the specified offset will be empty again between the head and the data. If theheadis not set, the offset value set withrelativeHeadRowIndex()will not take effectWhat's changed?
Change the judgment of whether to add offset in the
addmethod in theExcelWriteAddExecutorclass fromwriteSheetHoldertocurrentWriteHolderBefore:
After:
Checklist