This is an automated email from the ASF dual-hosted git repository.
pjfanning pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/poi.git
The following commit(s) were added to refs/heads/trunk by this push:
new 359f69344b HSSFRow.copyRowFrom: shift formulas with the .xls row limit
(#1417)
359f69344b is described below
commit 359f69344bc4f3ca3ab62b20cb823e0af714d516
Author: PJ Fanning <[email protected]>
AuthorDate: Fri Oct 16 20:25:43 2026 +0100
HSSFRow.copyRowFrom: shift formulas with the .xls row limit (#1417)
copyRowFrom built its FormulaShifter with SpreadsheetVersion.EXCEL2007,
so a relative reference copied past row 65536 became e.g. B65537. That
is not a valid .xls reference, and setting the shifted formula threw a
FormulaParseException. Use EXCEL97 so such references become #REF!, as
HSSFSheet.shiftRows does.
Fixes #1414
Co-authored-by: Claude Opus 5.5 <[email protected]>
---
.../org/apache/poi/hssf/usermodel/HSSFRow.java | 2 +-
.../org/apache/poi/hssf/usermodel/TestHSSFRow.java | 25 ++++++++++++++++++++++
2 files changed, 26 insertions(+), 1 deletion(-)
diff --git a/poi/src/main/java/org/apache/poi/hssf/usermodel/HSSFRow.java
b/poi/src/main/java/org/apache/poi/hssf/usermodel/HSSFRow.java
index b96375d09e..d1866444dc 100644
--- a/poi/src/main/java/org/apache/poi/hssf/usermodel/HSSFRow.java
+++ b/poi/src/main/java/org/apache/poi/hssf/usermodel/HSSFRow.java
@@ -845,7 +845,7 @@ public final class HSSFRow implements Row,
Comparable<HSSFRow> {
final int destRowNum = getRowNum();
final int rowDifference = destRowNum - srcRowNum;
- final FormulaShifter formulaShifter =
FormulaShifter.createForRowCopy(sheetIndex, sheetName, srcRowNum, srcRowNum,
rowDifference, SpreadsheetVersion.EXCEL2007);
+ final FormulaShifter formulaShifter =
FormulaShifter.createForRowCopy(sheetIndex, sheetName, srcRowNum, srcRowNum,
rowDifference, SpreadsheetVersion.EXCEL97);
final HSSFRowShifter rowShifter = new HSSFRowShifter(sheet);
rowShifter.updateRowFormulas(this, formulaShifter);
diff --git a/poi/src/test/java/org/apache/poi/hssf/usermodel/TestHSSFRow.java
b/poi/src/test/java/org/apache/poi/hssf/usermodel/TestHSSFRow.java
index 46a1c2e089..460d6f02b5 100644
--- a/poi/src/test/java/org/apache/poi/hssf/usermodel/TestHSSFRow.java
+++ b/poi/src/test/java/org/apache/poi/hssf/usermodel/TestHSSFRow.java
@@ -29,6 +29,7 @@ import java.io.IOException;
import org.apache.poi.hssf.HSSFITestDataProvider;
import org.apache.poi.hssf.record.BlankRecord;
import org.apache.poi.hssf.record.RowRecord;
+import org.apache.poi.ss.SpreadsheetVersion;
import org.apache.poi.ss.usermodel.*;
import org.junit.jupiter.api.Test;
@@ -142,6 +143,30 @@ final class TestHSSFRow extends BaseTestRow {
workbook.close();
}
+ @Test
+ void testCopyRowFromShiftsPastExcel97RowLimitToRefError() throws
IOException {
+ try (HSSFWorkbook workbook = new HSSFWorkbook()) {
+ final HSSFSheet sheet = workbook.createSheet("Sheet1");
+ final HSSFRow srcRow = sheet.createRow(0);
+ srcRow.createCell(0).setCellFormula("B2");
+ srcRow.createCell(1).setCellFormula("SUM(B2:B3)");
+ srcRow.createCell(2).setCellFormula("B$2");
+
+ // the last row in .xls: B2 would move to row 65537, which does
not exist
+ final HSSFRow destRow =
sheet.createRow(SpreadsheetVersion.EXCEL97.getLastRowIndex());
+ destRow.copyRowFrom(srcRow, new CellCopyPolicy());
+
+ assertEquals("#REF!", destRow.getCell(0).getCellFormula());
+ assertEquals("SUM(#REF!)", destRow.getCell(1).getCellFormula());
+ assertEquals("B$2", destRow.getCell(2).getCellFormula());
+
+ // still within the .xls row limit
+ final HSSFRow nearRow =
sheet.createRow(SpreadsheetVersion.EXCEL97.getLastRowIndex() - 1);
+ nearRow.copyRowFrom(srcRow, new CellCopyPolicy());
+ assertEquals("B65536", nearRow.getCell(0).getCellFormula());
+ }
+ }
+
@Test
void testCopyRowFromExternalSheet() throws IOException {
final HSSFWorkbook workbook = new HSSFWorkbook();
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]