git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1852246 13f79535-47bb-0310-9956-ffa450edef68pull/142/head
@@ -24,7 +24,7 @@ import java.util.Date; | |||
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 { | |||
@@ -35,7 +35,7 @@ public class CellTypes { | |||
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")) { |
@@ -24,7 +24,6 @@ import org.apache.poi.hssf.usermodel.HSSFCell; | |||
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 | |||
@@ -36,7 +35,6 @@ public class HyperlinkFormula { | |||
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")) { |
@@ -292,8 +292,6 @@ public class HSSFCell extends CellBase { | |||
} | |||
if (getCellType() == CellType.BLANK) { | |||
frec.getFormulaRecord().setValue(0); | |||
} else if (setValue) { | |||
frec.getFormulaRecord().setValue(getNumericCellValue()); | |||
} | |||
frec.setXFIndex(styleIndex); | |||
_record = frec; |
@@ -78,15 +78,22 @@ public interface Cell { | |||
* <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); | |||
/** |
@@ -35,6 +35,15 @@ public abstract class CellBase implements Cell { | |||
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); |
@@ -94,9 +94,9 @@ public abstract class RangeCopier { | |||
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) |
@@ -425,8 +425,6 @@ public class XSSFPivotTable extends POIXMLDocumentPart { | |||
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(); | |||
@@ -435,9 +433,6 @@ public class XSSFPivotTable extends POIXMLDocumentPart { | |||
} | |||
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()) { |
@@ -28,6 +28,7 @@ import org.apache.poi.ss.usermodel.Cell; | |||
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; | |||
@@ -220,12 +221,33 @@ public class XSSFRow implements Row, Comparable<XSSFRow> { | |||
} | |||
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. | |||
@@ -588,7 +610,7 @@ public class XSSFRow implements Row, Comparable<XSSFRow> { | |||
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); | |||
} | |||
@@ -59,7 +59,6 @@ public final class TestProper { | |||
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 |
@@ -537,20 +537,16 @@ public final class TestXSSFExportToXML { | |||
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()) { | |||
@@ -187,4 +187,9 @@ public class TestSXSSFCell extends BaseTestXCell { | |||
@Ignore | |||
public void setCellFormula_isExceptionSafe_onBlankCell() { | |||
} | |||
@Test | |||
@Ignore | |||
public void setCellType_FORMULA_onAnArrayFormulaCell_doesNothing() { | |||
} | |||
} |
@@ -626,7 +626,7 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { | |||
// 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 |
@@ -217,7 +217,6 @@ public final class TestHSSFDataFormatter { | |||
{ // 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%;")); |
@@ -40,7 +40,6 @@ public final class TestCalendarFieldFunction extends TestCase { | |||
HSSFWorkbook wb = new HSSFWorkbook(); | |||
HSSFSheet sheet = wb.createSheet("new sheet"); | |||
cell11 = sheet.createRow(0).createCell(0); | |||
cell11.setCellType(CellType.FORMULA); | |||
evaluator = new HSSFFormulaEvaluator(wb); | |||
} | |||
@@ -40,7 +40,6 @@ public final class TestDate extends TestCase { | |||
HSSFWorkbook wb = new HSSFWorkbook(); | |||
HSSFSheet sheet = wb.createSheet("new sheet"); | |||
cell11 = sheet.createRow(0).createCell(0); | |||
cell11.setCellType(CellType.FORMULA); | |||
evaluator = new HSSFFormulaEvaluator(wb); | |||
} | |||
@@ -48,7 +48,6 @@ public final class TestFixed { | |||
try { | |||
HSSFSheet sheet = wb.createSheet("new sheet"); | |||
cell11 = sheet.createRow(0).createCell(0); | |||
cell11.setCellType(CellType.FORMULA); | |||
evaluator = new HSSFFormulaEvaluator(wb); | |||
} finally { | |||
wb.close(); |
@@ -1218,8 +1218,7 @@ public abstract class BaseTestBugzillaIssues { | |||
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(); |
@@ -1358,6 +1358,38 @@ public abstract class BaseTestCell { | |||
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); |
@@ -384,12 +384,10 @@ public abstract class BaseTestFormulaEvaluator { | |||
// 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(); | |||
@@ -589,7 +587,6 @@ public abstract class BaseTestFormulaEvaluator { | |||
if (cell == null) { | |||
cell = r.createCell(column); | |||
} | |||
cell.setCellType(CellType.FORMULA); | |||
cell.setCellFormula(formula); | |||
} | |||