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,11 +491,32 @@ public class CellReference implements GenericRecord {
*
'O''Brien''s Sales'!A1' | Sheet name with special characters |
*
* @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.
+ *
+ * Example return values:
+ *
+ * Example return values
+ * Result | Comment |
+ * R1C1 | Cell reference without sheet |
+ * Sheet1!R1C1 | Standard sheet name |
+ * 'O''Brien''s Sales'!R1C1' | Sheet name with special characters |
+ *
+ * @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.
+ *
+ *
+ * Example return values:
+ *
+ * Example return values
+ * Result | Comment |
+ * R1C1 | Cell reference without sheet |
+ * Sheet1!R1C1 | Standard sheet name |
+ * 'O''Brien''s Sales'!R1C1' | Sheet name with special characters |
+ *
+ * @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() + "]";
@@ -564,6 +616,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.
*
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