* @return the text representation of this cell reference as it would appear in a formula.
*/
public String formatAsString() {
+ return formatAsString(true);
+ }
+
+ /**
+ * Returns a text representation of this cell reference and allows to control
+ * if the sheetname is included in the reference.
+ *
+ * <p>
+ * Example return values:
+ * <table border="0" cellpadding="1" cellspacing="0" summary="Example return values">
+ * <tr><th align='left'>Result</th><th align='left'>Comment</th></tr>
+ * <tr><td>A1</td><td>Cell reference without sheet</td></tr>
+ * <tr><td>Sheet1!A1</td><td>Standard sheet name</td></tr>
+ * <tr><td>'O''Brien''s Sales'!A1' </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.
+ */
+ public String formatAsString(boolean includeSheetName) {
StringBuilder sb = new StringBuilder(32);
- if(_sheetName != null) {
+ if(includeSheetName && _sheetName != null) {
SheetNameFormatter.appendFormat(sb, _sheetName);
sb.append(SHEET_NAME_DELIMITER);
}
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.FormulaError;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
+import org.apache.poi.ss.usermodel.IgnoredErrorType;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Name;
import org.apache.poi.ss.usermodel.PrintSetup;
assertEquals(1, ctMergeCellCount);
assertEquals(1, ctMergeCellListSize);
}
+
+ @Test
+ public void testBug63509() throws IOException {
+ XSSFWorkbook workbook = new XSSFWorkbook();
+
+ XSSFSheet sheet = workbook.createSheet("sheet1");
+
+ Row row = sheet.createRow(0);
+
+ Cell cell = row.createCell(0);
+ cell.setCellValue("1000");
+
+ // This causes the error
+ sheet.addIgnoredErrors(new CellReference(cell), IgnoredErrorType.NUMBER_STORED_AS_TEXT);
+
+ // Workaround
+ // sheet.addIgnoredErrors(new CellReference(cell.getRowIndex(), cell.getColumnIndex(), false, false),
+ // IgnoredErrorType.NUMBER_STORED_AS_TEXT);
+
+ /*File file = new File("/tmp/63509.xlsx");
+ try(FileOutputStream outputStream = new FileOutputStream(file)) {
+ workbook.write(outputStream);
+ }*/
+
+ workbook.close();
+ }
}
cellReference = new CellReference(row, col);
assertEquals("A1", cellReference.formatAsString());
+ assertEquals("A1", cellReference.formatAsString(true));
+ assertEquals("A1", cellReference.formatAsString(false));
cellReference = new CellReference(row, col, absRow, absCol);
assertEquals("A$1", cellReference.formatAsString());
+ assertEquals("A$1", cellReference.formatAsString(true));
+ assertEquals("A$1", cellReference.formatAsString(false));
cellReference = new CellReference(row, (short)col);
assertEquals("A1", cellReference.formatAsString());
+ assertEquals("A1", cellReference.formatAsString(true));
+ assertEquals("A1", cellReference.formatAsString(false));
cellReference = new CellReference(cellRef);
assertEquals("A1", cellReference.formatAsString());
+ assertEquals("A1", cellReference.formatAsString(true));
+ assertEquals("A1", cellReference.formatAsString(false));
cellReference = new CellReference(sheet, row, col, absRow, absCol);
assertEquals("Sheet1!A$1", cellReference.formatAsString());
+ assertEquals("Sheet1!A$1", cellReference.formatAsString(true));
+ assertEquals("A$1", cellReference.formatAsString(false));
cellReference = new CellReference(sheet+"!$A1");
assertFalse(cellReference.isRowAbsolute());
cellReference = new CellReference(null, 0, 0, false, false);
assertEquals("A1", cellReference.formatAsString());
+ assertEquals("A1", cellReference.formatAsString(true));
+ assertEquals("A1", cellReference.formatAsString(false));
//absolute references
cellReference = new CellReference(null, 0, 0, true, false);
assertEquals("A$1", cellReference.formatAsString());
+ assertEquals("A$1", cellReference.formatAsString(true));
+ assertEquals("A$1", cellReference.formatAsString(false));
//sheet name with no spaces
cellReference = new CellReference("Sheet1", 0, 0, true, false);
assertEquals("Sheet1!A$1", cellReference.formatAsString());
+ assertEquals("Sheet1!A$1", cellReference.formatAsString(true));
+ assertEquals("A$1", cellReference.formatAsString(false));
//sheet name with spaces
cellReference = new CellReference("Sheet 1", 0, 0, true, false);
assertEquals("'Sheet 1'!A$1", cellReference.formatAsString());
+ assertEquals("'Sheet 1'!A$1", cellReference.formatAsString(true));
+ assertEquals("A$1", cellReference.formatAsString(false));
}
@Test
final CellReference badCellRef = new CellReference(cell);
assertEquals("'Ctor test'!$A$1", goodCellRef.formatAsString());
+ assertEquals("'Ctor test'!$A$1", goodCellRef.formatAsString(true));
+ assertEquals("$A$1", goodCellRef.formatAsString(false));
+
assertEquals("'Ctor test'!A1", badCellRef.formatAsString());
+ assertEquals("'Ctor test'!A1", badCellRef.formatAsString(true));
+ assertEquals("A1", badCellRef.formatAsString(false));
}
}