import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
-import org.apache.poi.ss.usermodel.CellType;
+import org.apache.poi.ss.usermodel.FormulaError;
public class CellTypes {
public static void main(String[] args) throws IOException {
row.createCell(1).setCellValue(new Date());
row.createCell(2).setCellValue("a string");
row.createCell(3).setCellValue(true);
- row.createCell(4).setCellType(CellType.ERROR);
+ row.createCell(4).setCellErrorValue(FormulaError.NUM);
// Write the output to a file
try (FileOutputStream fileOut = new FileOutputStream("workbook.xls")) {
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
-import org.apache.poi.ss.usermodel.CellType;
/**
* Test if hyperlink formula, with url that got more than 127 characters, works
HSSFRow row = sheet.createRow(0);
HSSFCell cell = row.createCell(0);
- cell.setCellType(CellType.FORMULA);
cell.setCellFormula("HYPERLINK(\"http://127.0.0.1:8080/toto/truc/index.html?test=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\", \"test\")");
try (FileOutputStream fileOut = new FileOutputStream("workbook.xls")) {
}
if (getCellType() == CellType.BLANK) {
frec.getFormulaRecord().setValue(0);
- } else if (setValue) {
- frec.getFormulaRecord().setValue(getNumericCellValue());
}
frec.setXFIndex(styleIndex);
_record = frec;
* <p>to NUMERIC: numeric value is left as is. True converts to 1.0, false converts to 0. otherwise, the
* value is set to 0. Formula is removed.</p>
* <p>If what you want to do is get a String value for your
- * numeric cell, <i>stop!</i>. This is not the way to do it.
+ * numeric cell, <i>stop!</i> This is not the way to do it.
* Instead, for fetching the string value of a numeric or boolean
* or date cell, use {@link DataFormatter} instead.</p>
* <p>If cell is a member of an array formula group containing more than 1 cell, an {@link IllegalStateException}
- * is thrown. If the array formula group contains only this cell, it is removed</p>
- * @throws IllegalArgumentException if the specified cell type is invalid (null or _NONE)
+ * is thrown. If the array formula group contains only this cell, it is removed.</p>
+ * <p>Passing {@link CellType#FORMULA} is illegal and will result in an {@link IllegalArgumentException}.</p>
+ *
+ * @deprecated This method is deprecated and will be removed in POI 5.0.
+ * Use explicit {@link #setCellFormula(String)}, <code>setCellValue(...)</code> or {@link #setBlank()}
+ * to get the desired result.
+ * @throws IllegalArgumentException if the specified cell type is invalid (null, _NONE or FORMULA)
* @throws IllegalStateException if the current value cannot be converted to the new type or
* if the cell is a part of an array formula group containing other cells
*/
+ @Deprecated
+ @Removal(version = "5.0")
void setCellType(CellType cellType);
/**
throw new IllegalArgumentException("cellType shall not be null nor _NONE");
}
+ if (cellType == CellType.FORMULA) {
+ if (getCellType() != CellType.FORMULA){
+ throw new IllegalArgumentException("Calling Cell.setCellType(CellType.FORMULA) is illegal. " +
+ "Use setCellFormula(String) directly.");
+ } else {
+ return;
+ }
+ }
+
tryToDeleteArrayFormulaIfSet();
setCellTypeImpl(cellType);
destRow = destSheet.createRow(rowNo + deltaY);
Cell newCell = destRow.getCell(columnIndex + deltaX);
- if(newCell != null)
- newCell.setCellType(sourceCell.getCellType());
- else newCell = destRow.createCell(columnIndex + deltaX, sourceCell.getCellType());
+ if(newCell == null) {
+ newCell = destRow.createCell(columnIndex + deltaX);
+ }
cloneCellContent(sourceCell, newCell, null);
if(newCell.getCellType() == CellType.FORMULA)
private void addDataField(DataConsolidateFunction function, int columnIndex, String valueFieldName, String valueFormat) {
checkColumnIndex(columnIndex);
- AreaReference pivotArea = getPivotArea();
-
CTDataFields dataFields;
if(pivotTableDefinition.getDataFields() != null) {
dataFields = pivotTableDefinition.getDataFields();
}
CTDataField dataField = dataFields.addNewDataField();
dataField.setSubtotal(STDataConsolidateFunction.Enum.forInt(function.getValue()));
- Cell cell = getDataSheet().getRow(pivotArea.getFirstCell().getRow())
- .getCell(pivotArea.getFirstCell().getCol() + columnIndex);
- cell.setCellType(CellType.STRING);
dataField.setName(valueFieldName);
dataField.setFld(columnIndex);
if (valueFormat != null && !valueFormat.trim().isEmpty()) {
import org.apache.poi.ss.usermodel.CellCopyPolicy;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CellType;
+import org.apache.poi.ss.usermodel.FormulaError;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.helpers.RowShifter;
import org.apache.poi.ss.util.CellRangeAddress;
}
XSSFCell xcell = new XSSFCell(this, ctCell);
xcell.setCellNum(columnIndex);
- if (type != CellType.BLANK) {
- xcell.setCellType(type);
+ if (type != CellType.BLANK && type != CellType.FORMULA) {
+ setDefaultValue(xcell, type);
}
+
_cells.put(colI, xcell);
return xcell;
}
+
+ private static void setDefaultValue(XSSFCell cell, CellType type) {
+ switch (type) {
+ case NUMERIC:
+ cell.setCellValue(0);
+ break;
+ case STRING:
+ cell.setCellValue("");
+ break;
+ case BOOLEAN:
+ cell.setCellValue(false);
+ break;
+ case ERROR:
+ cell.setCellErrorValue(FormulaError._NO_ERROR);
+ break;
+ default:
+ throw new AssertionError();
+ }
+ }
+
/**
* Returns the cell at the given (0 based) index,
* with the {@link org.apache.poi.ss.usermodel.Row.MissingCellPolicy} from the parent Workbook.
else {
for (final Cell c : srcRow){
final XSSFCell srcCell = (XSSFCell)c;
- final XSSFCell destCell = createCell(srcCell.getColumnIndex(), srcCell.getCellType());
+ final XSSFCell destCell = createCell(srcCell.getColumnIndex());
destCell.copyCellFrom(srcCell, policy);
}
private void confirm(Workbook wb) {
Sheet sheet = wb.createSheet("new sheet");
cell11 = sheet.createRow(0).createCell(0);
- cell11.setCellType(CellType.FORMULA);
confirm("PROPER(\"hi there\")", "Hi There"); //simple case
confirm("PROPER(\"what's up\")", "What'S Up"); //apostrophes are treated as word breaks
Cell cFormulaString = row.createCell(3);
cFormulaString.setCellFormula("A1");
- cFormulaString.setCellType(CellType.FORMULA);
-
+
Cell cFormulaNumeric = row.createCell(4);
cFormulaNumeric.setCellFormula("F1");
- cFormulaNumeric.setCellType(CellType.FORMULA);
-
+
Cell cNumeric = row.createCell(5);
cNumeric.setCellValue(1.2);
- cNumeric.setCellType(CellType.NUMERIC);
-
+
Cell cDate = row.createCell(6);
cDate.setCellValue(new Date());
- cDate.setCellType(CellType.NUMERIC);
-
+
boolean found = false;
for (POIXMLDocumentPart p : wb.getRelations()) {
@Ignore
public void setCellFormula_isExceptionSafe_onBlankCell() {
}
+
+ @Test
+ @Ignore
+ public void setCellType_FORMULA_onAnArrayFormulaCell_doesNothing() {
+ }
}
// Otherwise should go
sheet.getRow(1).getCell(0).setCellFormula("A1"); // stay
sheet.getRow(2).getCell(0).setCellFormula(null); // go
- sheet.getRow(3).getCell(0).setCellType(CellType.FORMULA); // stay
+ sheet.getRow(3).getCell(0).setCellFormula("14"); // stay
XSSFTestDataSamples.writeOutAndReadBack(wb1).close();
sheet.getRow(4).getCell(0).setCellType(CellType.STRING); // go
{ // formula cell
row = sheet.createRow(7);
HSSFCell cell = row.createCell(0);
- cell.setCellType(CellType.FORMULA);
cell.setCellFormula("SUM(12.25,12.25)/100");
HSSFCellStyle cellStyle = wb.createCellStyle();
cellStyle.setDataFormat(format.getFormat("##.00%;"));
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("new sheet");
cell11 = sheet.createRow(0).createCell(0);
- cell11.setCellType(CellType.FORMULA);
evaluator = new HSSFFormulaEvaluator(wb);
}
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("new sheet");
cell11 = sheet.createRow(0).createCell(0);
- cell11.setCellType(CellType.FORMULA);
evaluator = new HSSFFormulaEvaluator(wb);
}
try {
HSSFSheet sheet = wb.createSheet("new sheet");
cell11 = sheet.createRow(0).createCell(0);
- cell11.setCellType(CellType.FORMULA);
evaluator = new HSSFFormulaEvaluator(wb);
} finally {
wb.close();
value == null || value.length() == 0);
cell = row.createCell(1);
- // also verify that setting formulas to null works
- cell.setCellType(CellType.FORMULA);
+ cell.setCellFormula("0");
cell.setCellValue((String)null);
wb.getCreationHelper().createFormulaEvaluator().evaluateAll();
assertTrue(cell.getBooleanCellValue());
}
+ @Test(expected = IllegalArgumentException.class)
+ public void setCellType_FORMULA_onANonFormulaCell_throwsIllegalArgumentException() {
+ Cell cell = getInstance();
+ cell.setCellType(CellType.FORMULA);
+ }
+
+ @Test
+ public void setCellType_FORMULA_onAFormulaCell_doesNothing() {
+ Cell cell = getInstance();
+ cell.setCellFormula("3");
+ cell.setCellValue("foo");
+
+ cell.setCellType(CellType.FORMULA);
+
+ assertEquals(CellType.FORMULA, cell.getCellType());
+ assertEquals(CellType.STRING, cell.getCachedFormulaResultType());
+ assertEquals("foo", cell.getStringCellValue());
+ }
+
+ @Test
+ public void setCellType_FORMULA_onAnArrayFormulaCell_doesNothing() {
+ Cell cell = getInstance();
+ cell.getSheet().setArrayFormula("3", CellRangeAddress.valueOf("A1:A2"));
+ cell.setCellValue("foo");
+
+ cell.setCellType(CellType.FORMULA);
+
+ assertEquals(CellType.FORMULA, cell.getCellType());
+ assertEquals(CellType.STRING, cell.getCachedFormulaResultType());
+ assertEquals("foo", cell.getStringCellValue());
+ }
+
@Test
public final void setBlank_delegatesTo_setCellType_BLANK() {
Cell cell = mock(CellBase.class);
// sheet1 A2 formulae
cell = sheet1.createRow(1).createCell(0);
- cell.setCellType(CellType.FORMULA);
cell.setCellFormula("SUM(Sheet1:Sheet3!A1)");
// sheet1 A3 formulae
cell = sheet1.createRow(2).createCell(0);
- cell.setCellType(CellType.FORMULA);
cell.setCellFormula("SUM(Sheet1:Sheet3!A1:B1)");
wb.getCreationHelper().createFormulaEvaluator().evaluateAll();
if (cell == null) {
cell = r.createCell(column);
}
- cell.setCellType(CellType.FORMULA);
cell.setCellFormula(formula);
}