From 2e5617fe43837b6149fc41c86410fa854b684de4 Mon Sep 17 00:00:00 2001 From: CodeMan-cmd <2291415248@qq.com> Date: Wed, 23 Sep 2026 09:37:46 +0800 Subject: [PATCH] fix: keep the coordinates in a cloned ReadCellData A cell read into a ReadCellData field, or read with READ_CELL_DATA, went through ReadCellData.clone(), which copied the value and the format but not rowIndex and columnIndex. The parsers do set them (CellTagHandler, FormulaRecordHandler, BoolErrRecordHandler, CsvExcelReadExecutor), so the coordinates were available in the very call that clones the cell, and every ReadCellData handed to a listener came back with null coordinates. The clone now keeps them, which also matches WriteCellData, where the coordinates are set on the way out. --- .../sheet/metadata/data/ReadCellData.java | 2 ++ .../sheet/converter/CellDataDataTest.java | 9 +++++++++ .../sheet/metadata/data/ReadCellDataTest.java | 18 ++++++++++++++++++ 3 files changed, 29 insertions(+) diff --git a/fesod-sheet/src/main/java/org/apache/fesod/sheet/metadata/data/ReadCellData.java b/fesod-sheet/src/main/java/org/apache/fesod/sheet/metadata/data/ReadCellData.java index 4656ee3cd..efcec629d 100644 --- a/fesod-sheet/src/main/java/org/apache/fesod/sheet/metadata/data/ReadCellData.java +++ b/fesod-sheet/src/main/java/org/apache/fesod/sheet/metadata/data/ReadCellData.java @@ -186,6 +186,8 @@ public static ReadCellData newInstanceOriginal(BigDecimal numberValue, Intege public ReadCellData clone() { ReadCellData readCellData = new ReadCellData<>(); readCellData.setType(getType()); + readCellData.setRowIndex(getRowIndex()); + readCellData.setColumnIndex(getColumnIndex()); readCellData.setNumberValue(getNumberValue()); readCellData.setOriginalNumberValue(getOriginalNumberValue()); readCellData.setStringValue(getStringValue()); diff --git a/fesod-sheet/src/test/java/org/apache/fesod/sheet/converter/CellDataDataTest.java b/fesod-sheet/src/test/java/org/apache/fesod/sheet/converter/CellDataDataTest.java index 5f778dc45..98911e6ae 100644 --- a/fesod-sheet/src/test/java/org/apache/fesod/sheet/converter/CellDataDataTest.java +++ b/fesod-sheet/src/test/java/org/apache/fesod/sheet/converter/CellDataDataTest.java @@ -59,6 +59,15 @@ void readAndWrite(ExcelFormat format) throws Exception { Assertions.assertEquals("2020年01月01日", row.getDate().getData()); Assertions.assertEquals(2L, (long) row.getInteger1().getData()); Assertions.assertEquals(2L, (long) row.getInteger2()); + + // A cell read as ReadCellData must tell where the cell is + Assertions.assertEquals(1, row.getDate().getRowIndex()); + Assertions.assertEquals(0, row.getDate().getColumnIndex()); + Assertions.assertEquals(1, row.getInteger1().getRowIndex()); + Assertions.assertEquals(1, row.getInteger1().getColumnIndex()); + Assertions.assertEquals(1, row.getFormulaValue().getRowIndex()); + Assertions.assertEquals(3, row.getFormulaValue().getColumnIndex()); + if (format.toExcelTypeEnum() != ExcelTypeEnum.CSV) { Assertions.assertEquals( "B2+C2", row.getFormulaValue().getFormulaData().getFormulaValue()); diff --git a/fesod-sheet/src/test/java/org/apache/fesod/sheet/metadata/data/ReadCellDataTest.java b/fesod-sheet/src/test/java/org/apache/fesod/sheet/metadata/data/ReadCellDataTest.java index a36b3a006..ac4d7ac85 100644 --- a/fesod-sheet/src/test/java/org/apache/fesod/sheet/metadata/data/ReadCellDataTest.java +++ b/fesod-sheet/src/test/java/org/apache/fesod/sheet/metadata/data/ReadCellDataTest.java @@ -19,6 +19,7 @@ package org.apache.fesod.sheet.metadata.data; +import org.apache.fesod.sheet.enums.CellDataTypeEnum; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; @@ -44,4 +45,21 @@ private static Boolean booleanFrom(String str) { cellData.setBooleanValueFromString(str); return cellData.getBooleanValue(); } + + /** + * A clone must describe the same cell, including where the cell is. + */ + @Test + void cloneKeepsCoordinates() { + ReadCellData cellData = new ReadCellData<>(CellDataTypeEnum.STRING, "value"); + cellData.setRowIndex(1); + cellData.setColumnIndex(2); + + ReadCellData clone = cellData.clone(); + + Assertions.assertEquals(CellDataTypeEnum.STRING, clone.getType()); + Assertions.assertEquals("value", clone.getStringValue()); + Assertions.assertEquals(1, clone.getRowIndex()); + Assertions.assertEquals(2, clone.getColumnIndex()); + } }