]> source.dussan.org Git - poi.git/commitdiff
Allow formatting of formula error cells, giving back the error string that Excel...
authorNick Burch <nick@apache.org>
Thu, 31 Oct 2013 16:53:03 +0000 (16:53 +0000)
committerNick Burch <nick@apache.org>
Thu, 31 Oct 2013 16:53:03 +0000 (16:53 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1537552 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/ss/usermodel/DataFormatter.java
src/testcases/org/apache/poi/ss/usermodel/TestDataFormatter.java

index 4e3718e956a3d6a961d852a0dd0588ead5f2570b..273dd89ef29b4ea88eefb917bfe7124cb6a5d5bd 100644 (file)
@@ -829,6 +829,8 @@ public class DataFormatter {
                 return String.valueOf(cell.getBooleanCellValue());
             case Cell.CELL_TYPE_BLANK :
                 return "";
+            case Cell.CELL_TYPE_ERROR:
+               return FormulaError.forInt(cell.getErrorCellValue()).getString();
         }
         throw new RuntimeException("Unexpected celltype (" + cellType + ")");
     }
index 6ce19fb5e942707facc150028785d3347949985d..20825e576eefc2f982afbd6baa23636ba122f702 100644 (file)
@@ -28,6 +28,7 @@ import java.util.Locale;
 
 import junit.framework.TestCase;
 
+import org.apache.poi.hssf.usermodel.HSSFWorkbook;
 import org.apache.poi.hssf.usermodel.TestHSSFDataFormatter;
 
 /**
@@ -491,6 +492,22 @@ public class TestDataFormatter extends TestCase {
         assertEquals(" -   ", dfUS.formatRawCellContents(0.0, -1, "_-* #,##0.00_-;-* #,##0.00_-;_-* \"-\"??_-;_-@_-"));
         assertEquals(" $-   ", dfUS.formatRawCellContents(0.0, -1, "_-$* #,##0.00_-;-$* #,##0.00_-;_-$* \"-\"??_-;_-@_-"));
     }
+    
+    public void testErrors() {
+        DataFormatter dfUS = new DataFormatter(Locale.US, true);
+
+        // Create a spreadsheet with some formula errors in it
+        Workbook wb = new HSSFWorkbook();
+        Sheet s = wb.createSheet();
+        Row r = s.createRow(0);
+        Cell c = r.createCell(0, Cell.CELL_TYPE_ERROR);
+        
+        c.setCellErrorValue(FormulaError.DIV0.getCode());
+        assertEquals(FormulaError.DIV0.getString(), dfUS.formatCellValue(c));
+        
+        c.setCellErrorValue(FormulaError.REF.getCode());
+        assertEquals(FormulaError.REF.getString(), dfUS.formatCellValue(c));
+    }
 
     /**
      * TODO Fix these so that they work