summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPJ Fanning <fanningpj@apache.org>2022-02-01 19:46:52 +0000
committerPJ Fanning <fanningpj@apache.org>2022-02-01 19:46:52 +0000
commit893034c806cbf118dcbb7f29591b716c23e31965 (patch)
tree28f440bc05ca9c0c920c21fe6629e6dc0b229477
parent2a25bcce1ee0ce1b65ba0ba114cd2c42c98476a6 (diff)
downloadpoi-893034c806cbf118dcbb7f29591b716c23e31965.tar.gz
poi-893034c806cbf118dcbb7f29591b716c23e31965.zip
add R1C1 test
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1897667 13f79535-47bb-0310-9956-ffa450edef68
-rw-r--r--poi/src/main/java/org/apache/poi/ss/util/CellReference.java65
-rw-r--r--poi/src/test/java/org/apache/poi/hssf/util/TestAreaReference.java1
-rw-r--r--poi/src/test/java/org/apache/poi/hssf/util/TestCellReference.java2
3 files changed, 68 insertions, 0 deletions
diff --git a/poi/src/main/java/org/apache/poi/ss/util/CellReference.java b/poi/src/main/java/org/apache/poi/ss/util/CellReference.java
index ec498d8ea1..6e2412f9e2 100644
--- a/poi/src/main/java/org/apache/poi/ss/util/CellReference.java
+++ b/poi/src/main/java/org/apache/poi/ss/util/CellReference.java
@@ -491,12 +491,33 @@ public class CellReference implements GenericRecord {
* <tr><td>'O''Brien''s Sales'!A1'&nbsp;</td><td>Sheet name with special characters</td></tr>
* </table>
* @return the text representation of this cell reference as it would appear in a formula.
+ * @see #formatAsString(boolean)
*/
public String formatAsString() {
return formatAsString(true);
}
/**
+ * Returns a text representation of this cell reference in R1C1 format.
+ * <p>
+ * Example return values:
+ * <table>
+ * <caption>Example return values</caption>
+ * <tr><th>Result</th><th>Comment</th></tr>
+ * <tr><td>R1C1</td><td>Cell reference without sheet</td></tr>
+ * <tr><td>Sheet1!R1C1</td><td>Standard sheet name</td></tr>
+ * <tr><td>'O''Brien''s Sales'!R1C1'&nbsp;</td><td>Sheet name with special characters</td></tr>
+ * </table>
+ * @return the text representation of this cell reference as it would appear in a formula.
+ * @see #formatAsString()
+ * @see #formatAsR1C1String(boolean)
+ * @since POI 5.2.1
+ */
+ public String formatAsR1C1String() {
+ return formatAsR1C1String(true);
+ }
+
+ /**
* Returns a text representation of this cell reference and allows to control
* if the sheetname is included in the reference.
*
@@ -512,6 +533,7 @@ public class CellReference implements GenericRecord {
* @param includeSheetName If true and there is a sheet name set for this cell reference,
* the reference is prefixed with the sheet name and '!'
* @return the text representation of this cell reference as it would appear in a formula.
+ * @see #formatAsString()
*/
public String formatAsString(boolean includeSheetName) {
StringBuilder sb = new StringBuilder(32);
@@ -523,6 +545,36 @@ public class CellReference implements GenericRecord {
return sb.toString();
}
+ /**
+ * Returns a text representation of this cell reference in R1C1 format and allows to control
+ * if the sheetname is included in the reference.
+ *
+ * <p>
+ * Example return values:
+ * <table>
+ * <caption>Example return values</caption>
+ * <tr><th>Result</th><th>Comment</th></tr>
+ * <tr><td>R1C1</td><td>Cell reference without sheet</td></tr>
+ * <tr><td>Sheet1!R1C1</td><td>Standard sheet name</td></tr>
+ * <tr><td>'O''Brien''s Sales'!R1C1'&nbsp;</td><td>Sheet name with special characters</td></tr>
+ * </table>
+ * @param includeSheetName If true and there is a sheet name set for this cell reference,
+ * the reference is prefixed with the sheet name and '!'
+ * @return the text representation of this cell reference as it would appear in a formula.
+ * @see #formatAsString(boolean)
+ * @see #formatAsR1C1String()
+ * @since POI 5.2.1
+ */
+ public String formatAsR1C1String(boolean includeSheetName) {
+ StringBuilder sb = new StringBuilder(32);
+ if(includeSheetName && _sheetName != null) {
+ SheetNameFormatter.appendFormat(sb, _sheetName);
+ sb.append(SHEET_NAME_DELIMITER);
+ }
+ appendR1C1CellReference(sb);
+ return sb.toString();
+ }
+
@Override
public String toString() {
return getClass().getName() + " [" + formatAsString() + "]";
@@ -565,6 +617,19 @@ public class CellReference implements GenericRecord {
}
/**
+ * Appends R1C1 cell reference with '$' markers for absolute values as required.
+ * Sheet name is not included.
+ */
+ /* package */ void appendR1C1CellReference(StringBuilder sb) {
+ if (_rowIndex != -1) {
+ sb.append('R').append(_rowIndex+1);
+ }
+ if (_colIndex != -1) {
+ sb.append('C').append(_colIndex+1);
+ }
+ }
+
+ /**
* Checks whether this cell reference is equal to another object.
* <p>
* Two cells references are assumed to be equal if their string representations
diff --git a/poi/src/test/java/org/apache/poi/hssf/util/TestAreaReference.java b/poi/src/test/java/org/apache/poi/hssf/util/TestAreaReference.java
index 1fe92ec022..28170dbf12 100644
--- a/poi/src/test/java/org/apache/poi/hssf/util/TestAreaReference.java
+++ b/poi/src/test/java/org/apache/poi/hssf/util/TestAreaReference.java
@@ -97,6 +97,7 @@ final class TestAreaReference {
ar = new AreaReference("Tabelle1!B5:B5", SpreadsheetVersion.EXCEL97);
assertTrue(ar.isSingleCell());
TestCellReference.confirmCell(ar.getFirstCell(), "Tabelle1", 4, 1, false, false, "Tabelle1!B5");
+ assertEquals("Tabelle1!R5C2", ar.getFirstCell().formatAsR1C1String(true));
assertEquals(1, ar.getAllReferencedCells().length);
diff --git a/poi/src/test/java/org/apache/poi/hssf/util/TestCellReference.java b/poi/src/test/java/org/apache/poi/hssf/util/TestCellReference.java
index f761e251dd..33d98d50f0 100644
--- a/poi/src/test/java/org/apache/poi/hssf/util/TestCellReference.java
+++ b/poi/src/test/java/org/apache/poi/hssf/util/TestCellReference.java
@@ -64,6 +64,8 @@ final class TestCellReference {
void testAbsRef1(){
CellReference cf = new CellReference("$B$5");
confirmCell(cf, null, 4, 1, true, true, "$B$5");
+ //absolute refs are not supported for R1C1 style
+ assertEquals("R5C2", cf.formatAsR1C1String());
}
@Test