Skip to content

fix: fix error in using relativeHeadRowIndex for writing tables only#810

Open
32154678925 wants to merge 16 commits into
apache:mainfrom
32154678925:fix/fix-relativeHeadRowIndex-invalid-#794
Open

fix: fix error in using relativeHeadRowIndex for writing tables only#810
32154678925 wants to merge 16 commits into
apache:mainfrom
32154678925:fix/fix-relativeHeadRowIndex-invalid-#794

Conversation

@32154678925

@32154678925 32154678925 commented Jan 18, 2026

Copy link
Copy Markdown

Purpose of the pull request

Closed: #794
When you do not write directly in the sheet, but only write data in the table and set the head, the offset value set with relativeHeadRowIndex() will make an error, and the blank line with the specified offset will be empty again between the head and the data. If the head is not set, the offset value set with relativeHeadRowIndex() will not take effect

What's changed?

Change the judgment of whether to add offset in the add method in the ExcelWriteAddExecutor class from writeSheetHolder to currentWriteHolder

Before:

WriteSheetHolder writeSheetHolder = writeContext.writeSheetHolder();
int newRowIndex = writeSheetHolder.getNewRowIndexAndStartDoWrite();
if (writeSheetHolder.isNew()
        && !writeSheetHolder.getExcelWriteHeadProperty().hasHead()) {
    newRowIndex += writeContext.currentWriteHolder().relativeHeadRowIndex();
}

After:

WriteSheetHolder writeSheetHolder = writeContext.writeSheetHolder();
WriteHolder currentWriteHolder = writeContext.currentWriteHolder();
int newRowIndex = writeSheetHolder.getNewRowIndexAndStartDoWrite();
if (currentWriteHolder.isNew()) {
    AbstractWriteHolder writeHolder = (AbstractWriteHolder) currentWriteHolder;
    if(!writeHolder.getExcelWriteHeadProperty().hasHead()){
        newRowIndex += currentWriteHolder.relativeHeadRowIndex();
    }
}

Checklist

  • I have read the Contributor Guide.
  • I have written the necessary doc or comment.
  • I have added the necessary unit tests and all cases have passed.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 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 check currentWriteHolder instead of writeSheetHolder for applying the row index offset
  • Added import for AbstractWriteHolder class
  • 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.

Comment on lines +70 to +74
if (currentWriteHolder instanceof AbstractWriteHolder) {
AbstractWriteHolder writeHolder = (AbstractWriteHolder) currentWriteHolder;
if (!writeHolder.getExcelWriteHeadProperty().hasHead()) {
newRowIndex += currentWriteHolder.relativeHeadRowIndex();
}

Copilot AI Feb 21, 2026

Copy link

Choose a reason for hiding this comment

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

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.

Suggested change
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();

Copilot uses AI. Check for mistakes.
@bengbengbalabalabeng

Copy link
Copy Markdown
Contributor

These tests contain no assertions and therefore do not validate the fix. Please enhance them with assertions that check the actual output. (Also ensure that both XLSX and XLS formats are covered)

 - Add read-back verification via WorkbookFactory for both test cases
 - Parameterize with @EnumSource to cover both .xls and .xlsx formats
 - Use randomized offsets (0–9) and data sizes (1–10) for broader coverage
 - Replace non-deterministic Random data with predictable prefix‑index pattern
 - Resolve header string from @ExcelProperty annotation via reflection
   to avoid coupling to a hardcoded value in WriteSheetData
 - Calculate expected sequential row layout (tables are sequential, not
   overlapping; B's position = A's last row + offsetB + 1)

Signed-off-by: 苗泽楠 <anan_laolu@foxmail.com>
Signed-off-by: 苗泽楠 <anan_laolu@foxmail.com>
@32154678925

Copy link
Copy Markdown
Author

这些测试不包含断言,因此不验证修复。请用检查实际输出的断言来增强它们。(同时确保涵盖XLSX和XLS格式)

谢谢,我修改了测试代码,并且成功跑通测试,且测试了未修改bug的代码,测试是跑不同的。我的代码经验不多,还有什么需要修改的么?

Thank you. I modified the test code and successfully ran through the test. I also tested the code without modifying the bug. The test runs differently. I don't have much code experience. Is there anything I need to change?

@@ -0,0 +1,247 @@
package org.apache.fesod.sheet.writesheet;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please supplement the copyright header. license-header.txt

value = ExcelTypeEnum.class,
names = {"XLS", "XLSX"})
public void testWriteTableWithTitle(ExcelTypeEnum excelType) throws Exception {
Random random = new Random();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Random numbers may lead to non-deterministic, it is recommended to use @MethodSource to provide N sets of representative boundary values for testing.

@bengbengbalabalabeng

Copy link
Copy Markdown
Contributor

Suggest covering multi-sheet table writing scenarios.

@bengbengbalabalabeng bengbengbalabalabeng added PR: first-time contributor first-time contributor PR: reviewing Currently under active review. labels Jun 16, 2026
@32154678925

Copy link
Copy Markdown
Author

Suggest covering multi-sheet table writing scenarios.

我添加了版权声明,移除了随机数,添加了多组偏移量,它们现在能够正常工作,还有什么需要修改的么?

I added a copyright notice, removed random numbers, and added multiple sets of offsets. They now work properly. Is there anything else I need to modify?

package org.apache.fesod.sheet.writesheet;

import java.io.File;
import java.util.*;

@bengbengbalabalabeng bengbengbalabalabeng Jun 24, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Avoid using import * (34 lines have the same issue)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Avoid using import * (34 lines have the same issue)

我已经修改了引入规则,还有其他需要注意的么?或者本项目有什么规则之类的说明?或许我可以去看看。

I have modified the import rules. Is there anything else I should pay attention to? Or what are the rules of this project? Maybe I can go and have a look.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The unit tests for the fesod-sheet module have recently undergone systematic refactoring, and the original TestFileUtil has been completely removed. It is recommended to refer to the following latest test case examples:

org.apache.fesod.sheet.sheet.WriteSheetTest
org.apache.fesod.sheet.sheet.HiddenSheetsTest
......

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I remove import TestFileUtil

32154678925 and others added 3 commits June 24, 2026 12:06
Signed-off-by: 苗泽楠 <anan_laolu@foxmail.com>
Signed-off-by: 苗泽楠 <anan_laolu@foxmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

PR: first-time contributor first-time contributor PR: reviewing Currently under active review.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] 使用table的relativeHeadRowIndex()设置写入行偏移量时效果不对

4 participants