From 20dfe361ca2933199deb65a6570b40740f86125b Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Tue, 1 Feb 2022 10:36:34 +0000 Subject: [PATCH] add excel Workbook.usesR1C1CellReferences() git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1897649 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/xssf/streaming/SXSSFWorkbook.java | 6 ++- .../poi/xssf/usermodel/XSSFWorkbook.java | 14 +++++++ .../poi/hssf/usermodel/HSSFWorkbook.java | 39 +++++++++++-------- .../org/apache/poi/ss/usermodel/Workbook.java | 7 ++++ 4 files changed, 49 insertions(+), 17 deletions(-) diff --git a/poi-ooxml/src/main/java/org/apache/poi/xssf/streaming/SXSSFWorkbook.java b/poi-ooxml/src/main/java/org/apache/poi/xssf/streaming/SXSSFWorkbook.java index 189787f96b..584a1f3f23 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xssf/streaming/SXSSFWorkbook.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xssf/streaming/SXSSFWorkbook.java @@ -1350,9 +1350,13 @@ public class SXSSFWorkbook implements Workbook { return _wb.addOlePackage(oleData, label, fileName, command); } - @Override public EvaluationWorkbook createEvaluationWorkbook() { return SXSSFEvaluationWorkbook.create(this); } + + @Override + public Boolean usesR1C1CellReferences() { + return getXSSFWorkbook().usesR1C1CellReferences(); + } } diff --git a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java index 7988e4e6e0..47e19e2956 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java @@ -20,6 +20,7 @@ package org.apache.poi.xssf.usermodel; import static org.apache.poi.ooxml.POIXMLTypeLoader.DEFAULT_XML_OPTIONS; import static org.apache.poi.xssf.usermodel.helpers.XSSFPasswordHelper.setPassword; import static org.apache.poi.xssf.usermodel.helpers.XSSFPasswordHelper.validatePassword; +import static org.openxmlformats.schemas.spreadsheetml.x2006.main.STRefMode.*; import java.io.File; import java.io.IOException; @@ -1566,6 +1567,19 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Date1904Su setPrintArea(sheetIndex, reference); } + @Override + public Boolean usesR1C1CellReferences() { + final CTCalcPr calcPr = getCTWorkbook().getCalcPr(); + if (calcPr == null) { + return null; + } else if (calcPr.getRefMode() == R_1_C_1) { + return Boolean.TRUE; + } else if (calcPr.getRefMode() == A_1) { + return Boolean.FALSE; + } + return null; + } + private static String getReferencePrintArea(String sheetName, int startC, int endC, int startR, int endR) { //windows excel example: Sheet1!$C$3:$E$4 CellReference colRef = new CellReference(sheetName, startR, startC, true, true); 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 e369488d10..86cd7440fd 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 @@ -69,22 +69,7 @@ import org.apache.poi.hssf.model.InternalSheet.UnsupportedBOFType; import org.apache.poi.hssf.model.InternalWorkbook; import org.apache.poi.hssf.model.RecordStream; import org.apache.poi.hssf.model.WorkbookRecordList; -import org.apache.poi.hssf.record.AbstractEscherHolderRecord; -import org.apache.poi.hssf.record.BackupRecord; -import org.apache.poi.hssf.record.BoundSheetRecord; -import org.apache.poi.hssf.record.DrawingGroupRecord; -import org.apache.poi.hssf.record.ExtendedFormatRecord; -import org.apache.poi.hssf.record.FilePassRecord; -import org.apache.poi.hssf.record.FontRecord; -import org.apache.poi.hssf.record.FormatRecord; -import org.apache.poi.hssf.record.LabelRecord; -import org.apache.poi.hssf.record.LabelSSTRecord; -import org.apache.poi.hssf.record.NameRecord; -import org.apache.poi.hssf.record.RecalcIdRecord; -import org.apache.poi.hssf.record.Record; -import org.apache.poi.hssf.record.RecordFactory; -import org.apache.poi.hssf.record.SSTRecord; -import org.apache.poi.hssf.record.UnknownRecord; +import org.apache.poi.hssf.record.*; import org.apache.poi.hssf.record.aggregates.RecordAggregate.RecordVisitor; import org.apache.poi.hssf.record.common.UnicodeString; import org.apache.poi.hssf.record.crypto.Biff8DecryptingStream; @@ -1762,6 +1747,28 @@ public final class HSSFWorkbook extends POIDocument implements Workbook { return newName; } + @Override + public Boolean usesR1C1CellReferences() { + for (HSSFSheet hssfSheet : _sheets) { + InternalSheet internalSheet = hssfSheet.getSheet(); + + List records = internalSheet.getRecords(); + + RefModeRecord refModeRecord = null; + for (RecordBase record : records) { + if (record instanceof RefModeRecord) refModeRecord = (RefModeRecord)record; + } + if (refModeRecord == null) { + return null; + } else if (refModeRecord.getMode() == RefModeRecord.USE_R1C1_MODE) { + return Boolean.TRUE; + } else if (refModeRecord.getMode() == RefModeRecord.USE_A1_MODE) { + return Boolean.FALSE; + } + } + return null; + } + int getNameIndex(String name) { for (int k = 0; k < names.size(); k++) { diff --git a/poi/src/main/java/org/apache/poi/ss/usermodel/Workbook.java b/poi/src/main/java/org/apache/poi/ss/usermodel/Workbook.java index e76637f5fd..041d7a785f 100644 --- a/poi/src/main/java/org/apache/poi/ss/usermodel/Workbook.java +++ b/poi/src/main/java/org/apache/poi/ss/usermodel/Workbook.java @@ -631,4 +631,11 @@ public interface Workbook extends Closeable, Iterable { * @return an evaluation workbook */ EvaluationWorkbook createEvaluationWorkbook(); + + /** + * @return true if workbook is configured to use R1C1 cell references (as opposed to A1 cell references). + * Returns null if the configuration is not explicitly set on the workbook. + * @since POI 5.2.1 + */ + Boolean usesR1C1CellReferences(); } -- 2.39.5