]> source.dussan.org Git - poi.git/commitdiff
made call Cell.setCellType(CellType.FORMULA) illegall. Deprecated Cell.setCellType...
authorVladislav Galas <gallon@apache.org>
Sat, 26 Jan 2019 19:41:25 +0000 (19:41 +0000)
committerVladislav Galas <gallon@apache.org>
Sat, 26 Jan 2019 19:41:25 +0000 (19:41 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1852246 13f79535-47bb-0310-9956-ffa450edef68

19 files changed:
src/examples/src/org/apache/poi/hssf/usermodel/examples/CellTypes.java
src/examples/src/org/apache/poi/hssf/usermodel/examples/HyperlinkFormula.java
src/java/org/apache/poi/hssf/usermodel/HSSFCell.java
src/java/org/apache/poi/ss/usermodel/Cell.java
src/java/org/apache/poi/ss/usermodel/CellBase.java
src/java/org/apache/poi/ss/usermodel/RangeCopier.java
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFPivotTable.java
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRow.java
src/ooxml/testcases/org/apache/poi/ss/formula/functions/TestProper.java
src/ooxml/testcases/org/apache/poi/xssf/extractor/TestXSSFExportToXML.java
src/ooxml/testcases/org/apache/poi/xssf/streaming/TestSXSSFCell.java
src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java
src/testcases/org/apache/poi/hssf/usermodel/TestHSSFDataFormatter.java
src/testcases/org/apache/poi/ss/formula/functions/TestCalendarFieldFunction.java
src/testcases/org/apache/poi/ss/formula/functions/TestDate.java
src/testcases/org/apache/poi/ss/formula/functions/TestFixed.java
src/testcases/org/apache/poi/ss/usermodel/BaseTestBugzillaIssues.java
src/testcases/org/apache/poi/ss/usermodel/BaseTestCell.java
src/testcases/org/apache/poi/ss/usermodel/BaseTestFormulaEvaluator.java

index 51dc89cc5d8c4a7dc5e0101849933fb369136041..f589bc6ad0c516ae2dce15849bb8f37da15defe3 100644 (file)
@@ -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")) {
index 5d4b18a3c201b1073dde65224dd51a4c6aa86bb7..52294360c698b049089436e36776df0068a8c50a 100644 (file)
@@ -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")) {
index eb92d5bddb7d79e85d18ae657c0d6541478cb45e..7926cbdb1aa4d3a3ef0cd4f6eb5cb99cdb4b7c6f 100644 (file)
@@ -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;
index 21e701490e302bd2bd81e3f1be7b1943fe5c9d74..4f344ba8e933c7096479d84f4f810b07c1ed7937 100644 (file)
@@ -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);
 
     /**
index 9777739fcf653008f3e1adfb36b5925b03897207..7a430257ef4920f4d2b184b8e01ee0a905a0b700 100644 (file)
@@ -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);
index 162100240f60ff7a34f2a0c71e634a20b41aeae9..6c73530b5c7d2c22cd046def137f69d63cf7a8da 100644 (file)
@@ -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)
index 6b77b91effba3479cc2f0af8923133d964a249ce..8ace06e95a8266f91b4de078c7cf0bdb3eda6b7c 100644 (file)
@@ -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()) {
index e7671c59683f79488fae59a4cb5cd2c071c5ee69..57df559f959097d9d661921ec14d4bda233b57f1 100644 (file)
@@ -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);
             }
 
index a56a8682ff9a8716abbc0916d4e48375ff67b02d..2296b6a4a9bda3038f0ad7b493436938ef2e279f 100644 (file)
@@ -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
index eda6dc0ac34fd3d0e0443f40ff2762ac2398fb9c..46f01eb850be19186b4bf7e356cac12ee4d6e825 100644 (file)
@@ -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()) {
 
index e696378667341ac6afe5705e2170027002383c67..c834cd528b0417bbc066f607519834b44f37140d 100644 (file)
@@ -187,4 +187,9 @@ public class TestSXSSFCell extends BaseTestXCell {
     @Ignore
     public void setCellFormula_isExceptionSafe_onBlankCell() {
     }
+
+    @Test
+    @Ignore
+    public void setCellType_FORMULA_onAnArrayFormulaCell_doesNothing() {
+    }
 }
index 7ed41f9c6d745e6622506e674f3e587ee2207562..99d95e1b5b2fc46d0493f879172368a4962d491f 100644 (file)
@@ -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
index 1ed7dd3a5da30f7b6d8afb255c1f814ad70f37d9..ed72f82a8f70d0125f419ccd08d986aaeabe4af8 100644 (file)
@@ -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%;"));
index e9af8c12ee91bc4b5f9e62a7b5b82a1d86ac5d7a..9a7f194a93d2e42da09cf9096d14ce59b64bb142 100644 (file)
@@ -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);
     }
 
index b9f5224c8d5ebbe72129ff8e45406fea373c8411..131df38d6d3e802f4e7e174637e735490f346660 100644 (file)
@@ -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);
     }
 
index e14ea3c45b26598b66d4b353556dc38c4796db16..6cc50e742bccbef0b1d84a81b3e955a4d8231efa 100644 (file)
@@ -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();
index c924a85674d1005d9025293ae74f724441b2d3c7..ae389c05b213e2082227376bc5ad1cdd37b659c1 100644 (file)
@@ -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();
index 3190c62f03f8bd3c1009e12dfcee5b527520c400..1e2b4d4f9611e0dded50ac0a70deb244ea6e0321 100644 (file)
@@ -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);
index 309da9472ce810b42fa951e752f07a951faa72b7..4712a85536ff2401b612d7fc5cbc805bf0927af7 100644 (file)
@@ -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);
     }