From 607eb0fabe7eb9963f981cc051aaf6596e4e5e49 Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Tue, 1 Feb 2022 13:26:13 +0000 Subject: [PATCH] add R1C1 test git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1897657 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/xssf/usermodel/TestXSSFWorkbook.java | 16 ++++++++++++++++ .../poi/hssf/usermodel/HSSFWorkbook.java | 10 ++++++++++ .../poi/hssf/usermodel/TestHSSFWorkbook.java | 18 ++++++++++++++++++ 3 files changed, 44 insertions(+) diff --git a/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java b/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java index 1087a4327e..de1e1cc312 100644 --- a/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java +++ b/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java @@ -1357,6 +1357,22 @@ public final class TestXSSFWorkbook extends BaseTestXWorkbook { } } + @Test + void checkNewFileForR1C1Refs() throws IOException { + try ( + UnsynchronizedByteArrayOutputStream bos = new UnsynchronizedByteArrayOutputStream(); + XSSFWorkbook wb = new XSSFWorkbook() + ) { + assertNull(wb.usesR1C1CellReferences()); + wb.setUseR1C1CellReferences(true); + assertTrue(wb.usesR1C1CellReferences()); + wb.write(bos); + try (XSSFWorkbook wb2 = new XSSFWorkbook(bos.toInputStream())) { + assertTrue(wb2.usesR1C1CellReferences()); + } + } + } + private static void expectFormattedContent(Cell cell, String value) { assertEquals(value, new DataFormatter().formatCellValue(cell), "Cell " + ref(cell) + " has wrong formatted content."); diff --git a/poi/src/main/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java b/poi/src/main/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java index 139e19e2fe..a6080ff833 100644 --- a/poi/src/main/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java +++ b/poi/src/main/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java @@ -1769,6 +1769,16 @@ public final class HSSFWorkbook extends POIDocument implements Workbook { return null; } + /** + * Configure workbook to use R1C1 cell references (as opposed to A1 cell references). + *

+ * Note that HSSF format stores this information at sheet level - so if the workbook has no sheets, + * this call will have no effect. It is recommended that you call this (possibly again) just before + * writing HSSFWorkbook. + *

+ * @param useR1C1CellReferences set to true if you want to configure workbook to use R1C1 cell references (as opposed to A1 cell references). + * @since POI 5.2.1 + */ @Override public void setUseR1C1CellReferences(boolean useR1C1CellReferences) { for (HSSFSheet hssfSheet : _sheets) { diff --git a/poi/src/test/java/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java b/poi/src/test/java/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java index d0a5129cc8..35caef0690 100644 --- a/poi/src/test/java/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java +++ b/poi/src/test/java/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java @@ -1194,6 +1194,24 @@ public final class TestHSSFWorkbook extends BaseTestWorkbook { } } + @Test + void checkNewFileForR1C1Refs() throws IOException { + try ( + UnsynchronizedByteArrayOutputStream bos = new UnsynchronizedByteArrayOutputStream(); + HSSFWorkbook wb = new HSSFWorkbook() + ) { + assertNull(wb.usesR1C1CellReferences()); + HSSFSheet sheet = wb.createSheet(); + assertFalse(wb.usesR1C1CellReferences()); + wb.setUseR1C1CellReferences(true); + assertTrue(wb.usesR1C1CellReferences()); + wb.write(bos); + try (HSSFWorkbook wb2 = new HSSFWorkbook(bos.toInputStream())) { + assertTrue(wb2.usesR1C1CellReferences()); + } + } + } + @Disabled void createDrawing() { // the dimensions for this image are different than for XSSF and SXSSF