add R1C1 test

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1897667 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
PJ Fanning 2022-02-01 19:46:52 +00:00
parent 2a25bcce1e
commit 893034c806
3 changed files with 68 additions and 0 deletions

View File

@ -491,11 +491,32 @@ 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() + "]";
@ -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.
* <p>

View File

@ -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);

View File

@ -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