introduce CellReferenceType

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1897779 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
PJ Fanning 2022-02-04 20:16:44 +00:00
parent b5d5077681
commit e6926f9779
7 changed files with 117 additions and 49 deletions

View File

@ -50,6 +50,7 @@ import org.apache.poi.openxml4j.util.ZipSecureFile;
import org.apache.poi.ss.SpreadsheetVersion;
import org.apache.poi.ss.formula.EvaluationWorkbook;
import org.apache.poi.ss.formula.udf.UDFFinder;
import org.apache.poi.ss.usermodel.CellReferenceType;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.DataFormat;
@ -1356,8 +1357,8 @@ public class SXSSFWorkbook implements Workbook {
}
@Override
public Boolean usesR1C1CellReferences() {
return getXSSFWorkbook().usesR1C1CellReferences();
public CellReferenceType getCellReferenceType() {
return getXSSFWorkbook().getCellReferenceType();
}
@Override

View File

@ -71,6 +71,7 @@ import org.apache.poi.ss.formula.SheetNameFormatter;
import org.apache.poi.ss.formula.udf.AggregatingUDFFinder;
import org.apache.poi.ss.formula.udf.IndexedUDFFinder;
import org.apache.poi.ss.formula.udf.UDFFinder;
import org.apache.poi.ss.usermodel.CellReferenceType;
import org.apache.poi.ss.usermodel.DataFormat;
import org.apache.poi.ss.usermodel.Date1904Support;
import org.apache.poi.ss.usermodel.Name;
@ -1551,16 +1552,16 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Date1904Su
}
@Override
public Boolean usesR1C1CellReferences() {
public CellReferenceType getCellReferenceType() {
final CTCalcPr calcPr = getCTWorkbook().getCalcPr();
if (calcPr == null) {
return null;
return CellReferenceType.UNKNOWN;
} else if (calcPr.getRefMode() == R_1_C_1) {
return Boolean.TRUE;
return CellReferenceType.R1C1;
} else if (calcPr.getRefMode() == A_1) {
return Boolean.FALSE;
return CellReferenceType.A1;
}
return null;
return CellReferenceType.UNKNOWN;
}
@Override

View File

@ -17,38 +17,38 @@
package org.apache.poi.xssf.usermodel;
import static org.apache.commons.io.output.NullOutputStream.NULL_OUTPUT_STREAM;
import static org.apache.poi.hssf.HSSFTestDataSamples.openSampleFileStream;
import static org.apache.poi.xssf.XSSFTestDataSamples.*;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.zip.CRC32;
import org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream;
import org.apache.poi.POIDataSamples;
import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.ooxml.POIXMLProperties;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.opc.*;
import org.apache.poi.openxml4j.opc.ContentTypes;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.openxml4j.opc.PackageAccess;
import org.apache.poi.openxml4j.opc.PackagePart;
import org.apache.poi.openxml4j.opc.PackagePartName;
import org.apache.poi.openxml4j.opc.PackageRelationship;
import org.apache.poi.openxml4j.opc.PackageRelationshipCollection;
import org.apache.poi.openxml4j.opc.PackagingURIHelper;
import org.apache.poi.openxml4j.opc.ZipPackage;
import org.apache.poi.openxml4j.opc.internal.FileHelper;
import org.apache.poi.openxml4j.opc.internal.MemoryPackagePart;
import org.apache.poi.openxml4j.opc.internal.PackagePropertiesPart;
import org.apache.poi.openxml4j.util.ZipInputStreamZipEntrySource;
import org.apache.poi.ss.tests.usermodel.BaseTestXWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellReferenceType;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Comment;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.FormulaError;
import org.apache.poi.ss.usermodel.RichTextString;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Row.MissingCellPolicy;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.ss.util.AreaReference;
import org.apache.poi.ss.util.CellAddress;
import org.apache.poi.ss.util.CellReference;
@ -68,6 +68,31 @@ import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorkbook;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorkbookPr;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STCalcMode;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.zip.CRC32;
import static org.apache.commons.io.output.NullOutputStream.NULL_OUTPUT_STREAM;
import static org.apache.poi.hssf.HSSFTestDataSamples.openSampleFileStream;
import static org.apache.poi.xssf.XSSFTestDataSamples.openSampleWorkbook;
import static org.apache.poi.xssf.XSSFTestDataSamples.writeOut;
import static org.apache.poi.xssf.XSSFTestDataSamples.writeOutAndReadBack;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
public final class TestXSSFWorkbook extends BaseTestXWorkbook {
public TestXSSFWorkbook() {
@ -1347,12 +1372,12 @@ public final class TestXSSFWorkbook extends BaseTestXWorkbook {
UnsynchronizedByteArrayOutputStream bos = new UnsynchronizedByteArrayOutputStream();
XSSFWorkbook wb = openSampleWorkbook("WithTable.xlsx")
) {
assertFalse(wb.usesR1C1CellReferences());
assertEquals(CellReferenceType.A1, wb.getCellReferenceType());
wb.setUseR1C1CellReferences(true);
assertTrue(wb.usesR1C1CellReferences());
assertEquals(CellReferenceType.R1C1, wb.getCellReferenceType());
wb.write(bos);
try (XSSFWorkbook wb2 = new XSSFWorkbook(bos.toInputStream())) {
assertTrue(wb2.usesR1C1CellReferences());
assertEquals(CellReferenceType.R1C1, wb2.getCellReferenceType());
}
}
}
@ -1363,12 +1388,12 @@ public final class TestXSSFWorkbook extends BaseTestXWorkbook {
UnsynchronizedByteArrayOutputStream bos = new UnsynchronizedByteArrayOutputStream();
XSSFWorkbook wb = new XSSFWorkbook()
) {
assertNull(wb.usesR1C1CellReferences());
assertEquals(CellReferenceType.UNKNOWN, wb.getCellReferenceType());
wb.setUseR1C1CellReferences(true);
assertTrue(wb.usesR1C1CellReferences());
assertEquals(CellReferenceType.R1C1, wb.getCellReferenceType());
wb.write(bos);
try (XSSFWorkbook wb2 = new XSSFWorkbook(bos.toInputStream())) {
assertTrue(wb2.usesR1C1CellReferences());
assertEquals(CellReferenceType.R1C1, wb2.getCellReferenceType());
}
}
}

View File

@ -96,6 +96,7 @@ import org.apache.poi.ss.formula.SheetNameFormatter;
import org.apache.poi.ss.formula.udf.AggregatingUDFFinder;
import org.apache.poi.ss.formula.udf.IndexedUDFFinder;
import org.apache.poi.ss.formula.udf.UDFFinder;
import org.apache.poi.ss.usermodel.CellReferenceType;
import org.apache.poi.ss.usermodel.Name;
import org.apache.poi.ss.usermodel.Row.MissingCellPolicy;
import org.apache.poi.ss.usermodel.Sheet;
@ -1748,7 +1749,7 @@ public final class HSSFWorkbook extends POIDocument implements Workbook {
}
@Override
public Boolean usesR1C1CellReferences() {
public CellReferenceType getCellReferenceType() {
for (HSSFSheet hssfSheet : _sheets) {
InternalSheet internalSheet = hssfSheet.getSheet();
@ -1759,14 +1760,14 @@ public final class HSSFWorkbook extends POIDocument implements Workbook {
if (record instanceof RefModeRecord) refModeRecord = (RefModeRecord)record;
}
if (refModeRecord == null) {
return null;
continue;
} else if (refModeRecord.getMode() == RefModeRecord.USE_R1C1_MODE) {
return Boolean.TRUE;
return CellReferenceType.R1C1;
} else if (refModeRecord.getMode() == RefModeRecord.USE_A1_MODE) {
return Boolean.FALSE;
return CellReferenceType.A1;
}
}
return null;
return CellReferenceType.UNKNOWN;
}
/**

View File

@ -0,0 +1,40 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.ss.usermodel;
/**
* Types of cell references.
* @since POI 5.2.1
*/
public enum CellReferenceType {
/**
* Cells are referenced in the form A1, B4, etc.
*/
A1,
/**
* Cells are referenced in the form R1C1, R4C2, etc.
*/
R1C1,
/**
* The cell reference type is not defined explicitly by <code>A1</code> is the default in this case.
*/
UNKNOWN
}

View File

@ -633,11 +633,10 @@ public interface Workbook extends Closeable, Iterable<Sheet> {
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.
* @return the type of cell references used
* @since POI 5.2.1
*/
Boolean usesR1C1CellReferences();
CellReferenceType getCellReferenceType();
/**
* @param useR1C1CellReferences set to true if you want to configure workbook to use R1C1 cell references (as opposed to A1 cell references).

View File

@ -61,6 +61,7 @@ import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.formula.ptg.Area3DPtg;
import org.apache.poi.ss.usermodel.BaseTestWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellReferenceType;
import org.apache.poi.ss.usermodel.ConditionalFormatting;
import org.apache.poi.ss.usermodel.ConditionalFormattingRule;
import org.apache.poi.ss.usermodel.Name;
@ -1184,12 +1185,12 @@ public final class TestHSSFWorkbook extends BaseTestWorkbook {
UnsynchronizedByteArrayOutputStream bos = new UnsynchronizedByteArrayOutputStream();
HSSFWorkbook wb = openSampleWorkbook("49423.xls")
) {
assertFalse(wb.usesR1C1CellReferences());
assertEquals(CellReferenceType.A1, wb.getCellReferenceType());
wb.setUseR1C1CellReferences(true);
assertTrue(wb.usesR1C1CellReferences());
assertEquals(CellReferenceType.R1C1, wb.getCellReferenceType());
wb.write(bos);
try (HSSFWorkbook wb2 = new HSSFWorkbook(bos.toInputStream())) {
assertTrue(wb2.usesR1C1CellReferences());
assertEquals(CellReferenceType.R1C1, wb2.getCellReferenceType());
}
}
}
@ -1200,14 +1201,14 @@ public final class TestHSSFWorkbook extends BaseTestWorkbook {
UnsynchronizedByteArrayOutputStream bos = new UnsynchronizedByteArrayOutputStream();
HSSFWorkbook wb = new HSSFWorkbook()
) {
assertNull(wb.usesR1C1CellReferences());
assertEquals(CellReferenceType.UNKNOWN, wb.getCellReferenceType());
HSSFSheet sheet = wb.createSheet();
assertFalse(wb.usesR1C1CellReferences());
assertEquals(CellReferenceType.A1, wb.getCellReferenceType());
wb.setUseR1C1CellReferences(true);
assertTrue(wb.usesR1C1CellReferences());
assertEquals(CellReferenceType.R1C1, wb.getCellReferenceType());
wb.write(bos);
try (HSSFWorkbook wb2 = new HSSFWorkbook(bos.toInputStream())) {
assertTrue(wb2.usesR1C1CellReferences());
assertEquals(CellReferenceType.R1C1, wb2.getCellReferenceType());
}
}
}