introduce CellReferenceType

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1897780 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
PJ Fanning 2022-02-04 20:29:55 +00:00
parent e6926f9779
commit 1bb6dddc06
6 changed files with 35 additions and 19 deletions

View File

@ -1362,7 +1362,7 @@ public class SXSSFWorkbook implements Workbook {
} }
@Override @Override
public void setUseR1C1CellReferences(boolean useR1C1CellReferences) { public void setCellReferenceType(CellReferenceType cellReferenceType) {
getXSSFWorkbook().setUseR1C1CellReferences(useR1C1CellReferences); getXSSFWorkbook().setCellReferenceType(cellReferenceType);
} }
} }

View File

@ -1565,11 +1565,17 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Date1904Su
} }
@Override @Override
public void setUseR1C1CellReferences(boolean useR1C1CellReferences) { public void setCellReferenceType(CellReferenceType cellReferenceType) {
CTCalcPr calcPr = getCTWorkbook().getCalcPr(); CTCalcPr calcPr = getCTWorkbook().getCalcPr();
if (calcPr == null) calcPr = getCTWorkbook().addNewCalcPr(); if (cellReferenceType == CellReferenceType.UNKNOWN) {
STRefMode.Enum refMode = useR1C1CellReferences ? R_1_C_1 : A_1; if (calcPr != null) {
calcPr.setRefMode(refMode); calcPr.unsetRefMode();
}
} else {
if (calcPr == null) calcPr = getCTWorkbook().addNewCalcPr();
STRefMode.Enum refMode = cellReferenceType == CellReferenceType.R1C1 ? R_1_C_1 : A_1;
calcPr.setRefMode(refMode);
}
} }
private static String getReferencePrintArea(String sheetName, int startC, int endC, int startR, int endR) { private static String getReferencePrintArea(String sheetName, int startC, int endC, int startR, int endR) {

View File

@ -1373,7 +1373,7 @@ public final class TestXSSFWorkbook extends BaseTestXWorkbook {
XSSFWorkbook wb = openSampleWorkbook("WithTable.xlsx") XSSFWorkbook wb = openSampleWorkbook("WithTable.xlsx")
) { ) {
assertEquals(CellReferenceType.A1, wb.getCellReferenceType()); assertEquals(CellReferenceType.A1, wb.getCellReferenceType());
wb.setUseR1C1CellReferences(true); wb.setCellReferenceType(CellReferenceType.R1C1);
assertEquals(CellReferenceType.R1C1, wb.getCellReferenceType()); assertEquals(CellReferenceType.R1C1, wb.getCellReferenceType());
wb.write(bos); wb.write(bos);
try (XSSFWorkbook wb2 = new XSSFWorkbook(bos.toInputStream())) { try (XSSFWorkbook wb2 = new XSSFWorkbook(bos.toInputStream())) {
@ -1389,7 +1389,7 @@ public final class TestXSSFWorkbook extends BaseTestXWorkbook {
XSSFWorkbook wb = new XSSFWorkbook() XSSFWorkbook wb = new XSSFWorkbook()
) { ) {
assertEquals(CellReferenceType.UNKNOWN, wb.getCellReferenceType()); assertEquals(CellReferenceType.UNKNOWN, wb.getCellReferenceType());
wb.setUseR1C1CellReferences(true); wb.setCellReferenceType(CellReferenceType.R1C1);
assertEquals(CellReferenceType.R1C1, wb.getCellReferenceType()); assertEquals(CellReferenceType.R1C1, wb.getCellReferenceType());
wb.write(bos); wb.write(bos);
try (XSSFWorkbook wb2 = new XSSFWorkbook(bos.toInputStream())) { try (XSSFWorkbook wb2 = new XSSFWorkbook(bos.toInputStream())) {

View File

@ -1757,7 +1757,10 @@ public final class HSSFWorkbook extends POIDocument implements Workbook {
RefModeRecord refModeRecord = null; RefModeRecord refModeRecord = null;
for (RecordBase record : records) { for (RecordBase record : records) {
if (record instanceof RefModeRecord) refModeRecord = (RefModeRecord)record; if (record instanceof RefModeRecord) {
refModeRecord = (RefModeRecord)record;
break;
}
} }
if (refModeRecord == null) { if (refModeRecord == null) {
continue; continue;
@ -1771,17 +1774,17 @@ public final class HSSFWorkbook extends POIDocument implements Workbook {
} }
/** /**
* Configure workbook to use R1C1 cell references (as opposed to A1 cell references). * Configure workbook to a specific cell reference type, e.g. R1C1 cell references (as opposed to A1 cell references).
* <p> * <p>
* Note that HSSF format stores this information at sheet level - so if the workbook has no sheets, * 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 * this call will have no effect. It is recommended that you call this (possibly again) just before
* writing HSSFWorkbook. * writing HSSFWorkbook.
* </p> * </p>
* @param useR1C1CellReferences set to true if you want to configure workbook to use R1C1 cell references (as opposed to A1 cell references). * @param cellReferenceType the type of cell references used
* @since POI 5.2.1 * @since POI 5.2.1
*/ */
@Override @Override
public void setUseR1C1CellReferences(boolean useR1C1CellReferences) { public void setCellReferenceType(CellReferenceType cellReferenceType) {
for (HSSFSheet hssfSheet : _sheets) { for (HSSFSheet hssfSheet : _sheets) {
InternalSheet internalSheet = hssfSheet.getSheet(); InternalSheet internalSheet = hssfSheet.getSheet();
@ -1790,20 +1793,27 @@ public final class HSSFWorkbook extends POIDocument implements Workbook {
RefModeRecord refModeRecord = null; RefModeRecord refModeRecord = null;
for (RecordBase record : records) { for (RecordBase record : records) {
if (record instanceof RefModeRecord) refModeRecord = (RefModeRecord)record; if (record instanceof RefModeRecord) {
refModeRecord = (RefModeRecord)record;
break;
}
} }
if (useR1C1CellReferences) { if (cellReferenceType == CellReferenceType.R1C1) {
if (refModeRecord == null) { if (refModeRecord == null) {
refModeRecord = new RefModeRecord(); refModeRecord = new RefModeRecord();
records.add(records.size() - 1, refModeRecord); records.add(records.size() - 1, refModeRecord);
} }
refModeRecord.setMode(RefModeRecord.USE_R1C1_MODE); refModeRecord.setMode(RefModeRecord.USE_R1C1_MODE);
} else { } else if (cellReferenceType == CellReferenceType.A1) {
if (refModeRecord == null) { if (refModeRecord == null) {
refModeRecord = new RefModeRecord(); refModeRecord = new RefModeRecord();
records.add(records.size() - 1, refModeRecord); records.add(records.size() - 1, refModeRecord);
} }
refModeRecord.setMode(RefModeRecord.USE_A1_MODE); refModeRecord.setMode(RefModeRecord.USE_A1_MODE);
} else {
if (refModeRecord != null) {
records.remove(refModeRecord);
}
} }
} }
} }

View File

@ -639,8 +639,8 @@ public interface Workbook extends Closeable, Iterable<Sheet> {
CellReferenceType getCellReferenceType(); CellReferenceType getCellReferenceType();
/** /**
* @param useR1C1CellReferences set to true if you want to configure workbook to use R1C1 cell references (as opposed to A1 cell references). * @param cellReferenceType the type of cell references used
* @since POI 5.2.1 * @since POI 5.2.1
*/ */
void setUseR1C1CellReferences(boolean useR1C1CellReferences); void setCellReferenceType(CellReferenceType cellReferenceType);
} }

View File

@ -1186,7 +1186,7 @@ public final class TestHSSFWorkbook extends BaseTestWorkbook {
HSSFWorkbook wb = openSampleWorkbook("49423.xls") HSSFWorkbook wb = openSampleWorkbook("49423.xls")
) { ) {
assertEquals(CellReferenceType.A1, wb.getCellReferenceType()); assertEquals(CellReferenceType.A1, wb.getCellReferenceType());
wb.setUseR1C1CellReferences(true); wb.setCellReferenceType(CellReferenceType.R1C1);
assertEquals(CellReferenceType.R1C1, wb.getCellReferenceType()); assertEquals(CellReferenceType.R1C1, wb.getCellReferenceType());
wb.write(bos); wb.write(bos);
try (HSSFWorkbook wb2 = new HSSFWorkbook(bos.toInputStream())) { try (HSSFWorkbook wb2 = new HSSFWorkbook(bos.toInputStream())) {
@ -1204,7 +1204,7 @@ public final class TestHSSFWorkbook extends BaseTestWorkbook {
assertEquals(CellReferenceType.UNKNOWN, wb.getCellReferenceType()); assertEquals(CellReferenceType.UNKNOWN, wb.getCellReferenceType());
HSSFSheet sheet = wb.createSheet(); HSSFSheet sheet = wb.createSheet();
assertEquals(CellReferenceType.A1, wb.getCellReferenceType()); assertEquals(CellReferenceType.A1, wb.getCellReferenceType());
wb.setUseR1C1CellReferences(true); wb.setCellReferenceType(CellReferenceType.R1C1);
assertEquals(CellReferenceType.R1C1, wb.getCellReferenceType()); assertEquals(CellReferenceType.R1C1, wb.getCellReferenceType());
wb.write(bos); wb.write(bos);
try (HSSFWorkbook wb2 = new HSSFWorkbook(bos.toInputStream())) { try (HSSFWorkbook wb2 = new HSSFWorkbook(bos.toInputStream())) {