]> source.dussan.org Git - poi.git/commitdiff
Merged revisions 638786-638802,638805-638811,638813-638814,638816-639230,639233-63924...
authorNick Burch <nick@apache.org>
Fri, 23 May 2008 13:12:43 +0000 (13:12 +0000)
committerNick Burch <nick@apache.org>
Fri, 23 May 2008 13:12:43 +0000 (13:12 +0000)
https://svn.apache.org:443/repos/asf/poi/trunk

........
  r659525 | nick | 2008-05-23 13:58:56 +0100 (Fri, 23 May 2008) | 1 line

  Extend the support for specifying a policy to HSSF on missing / blank cells when fetching, to be able to specify the policy at the HSSFWorkbook level
........
  Also, port this to XSSF

git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ooxml@659533 13f79535-47bb-0310-9956-ffa450edef68

src/documentation/content/xdocs/changes.xml
src/documentation/content/xdocs/status.xml
src/java/org/apache/poi/hssf/usermodel/HSSFRow.java
src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java
src/ooxml/interfaces-jdk14/org/apache/poi/ss/usermodel/Workbook.java
src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Workbook.java
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRow.java
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java
src/testcases/org/apache/poi/hssf/usermodel/TestHSSFRow.java

index fc755ea79ab8c54f14d04944226c407576d69a35..5a90a0463a9880dbbd2d6bf36563120f9b67a04b 100644 (file)
@@ -37,6 +37,7 @@
 
                <!-- Don't forget to update status.xml too! -->
         <release version="3.5.1-alpha1" date="2008-04-??">
+           <action dev="POI-DEVELOPERS" type="add">Port support for setting a policy on missing / blank cells when fetching, to XSSF too</action>
            <action dev="POI-DEVELOPERS" type="add">Common text extraction factory, which returns the correct POITextExtractor for the supplied data</action>
            <action dev="POI-DEVELOPERS" type="add">Text Extraction support for the new OOXML files (.xlsx, .docx and .pptx)</action>
            <action dev="POI-DEVELOPERS" type="add">Initial support for processing OOXML Excel files (.xlsx), both directly through XSSF, and also through the new common UserModel</action>
            <action dev="POI-DEVELOPERS" type="add">Created a common interface for handling Excel files, irrespective of if they are .xls or .xlsx</action>
         </release>
         <release version="3.1-final" date="2008-06-??">
-           <action dev="POI-DEVELOPERS" type="add">45025 - improved FormulaParser parse error messages</action>
-           <action dev="POI-DEVELOPERS" type="add">45046 - allowed EXTERNALBOOK(0x01AE) to be optional in the LinkTable</action>
-           <action dev="POI-DEVELOPERS" type="add">45066 - fixed sheet encoding size mismatch problems</action>
+           <action dev="POI-DEVELOPERS" type="add">Extend the support for specifying a policy to HSSF on missing / blank cells when fetching, to be able to specify the policy at the HSSFWorkbook level</action>
+           <action dev="POI-DEVELOPERS" type="fix">45025 - improved FormulaParser parse error messages</action>
+           <action dev="POI-DEVELOPERS" type="fix">45046 - allowed EXTERNALBOOK(0x01AE) to be optional in the LinkTable</action>
+           <action dev="POI-DEVELOPERS" type="fix">45066 - fixed sheet encoding size mismatch problems</action>
            <action dev="POI-DEVELOPERS" type="add">45003 - Support embeded HDGF visio documents</action>
            <action dev="POI-DEVELOPERS" type="fix">45001 - Partial fix for HWPF Range.insertBefore() and Range.delete() with unicode characters</action>
            <action dev="POI-DEVELOPERS" type="fix">44977 - Support for AM/PM in excel date formats</action>
index ba2c99ec68d6f459f073d32f5ab3e0735f615f7c..6709b3271919652a0a6fd2dd91c7141bee99d65e 100644 (file)
@@ -34,6 +34,7 @@
        <!-- Don't forget to update changes.xml too! -->
     <changes>
         <release version="3.5.1-alpha1" date="2008-04-??">
+           <action dev="POI-DEVELOPERS" type="add">Port support for setting a policy on missing / blank cells when fetching, to XSSF too</action>
            <action dev="POI-DEVELOPERS" type="add">Common text extraction factory, which returns the correct POITextExtractor for the supplied data</action>
            <action dev="POI-DEVELOPERS" type="add">Text Extraction support for the new OOXML files (.xlsx, .docx and .pptx)</action>
            <action dev="POI-DEVELOPERS" type="add">Initial support for processing OOXML Excel files (.xlsx), both directly through XSSF, and also through the new common UserModel</action>
            <action dev="POI-DEVELOPERS" type="add">Created a common interface for handling Excel files, irrespective of if they are .xls or .xlsx</action>
         </release>
         <release version="3.1-final" date="2008-06-??">
-           <action dev="POI-DEVELOPERS" type="add">45025 - improved FormulaParser parse error messages</action>
-           <action dev="POI-DEVELOPERS" type="add">45046 - allowed EXTERNALBOOK(0x01AE) to be optional in the LinkTable</action>
-           <action dev="POI-DEVELOPERS" type="add">45066 - fixed sheet encoding size mismatch problems</action>
+           <action dev="POI-DEVELOPERS" type="add">Extend the support for specifying a policy to HSSF on missing / blank cells when fetching, to be able to specify the policy at the HSSFWorkbook level</action>
+           <action dev="POI-DEVELOPERS" type="fix">45025 - improved FormulaParser parse error messages</action>
+           <action dev="POI-DEVELOPERS" type="fix">45046 - allowed EXTERNALBOOK(0x01AE) to be optional in the LinkTable</action>
+           <action dev="POI-DEVELOPERS" type="fix">45066 - fixed sheet encoding size mismatch problems</action>
            <action dev="POI-DEVELOPERS" type="add">45003 - Support embeded HDGF visio documents</action>
            <action dev="POI-DEVELOPERS" type="fix">45001 - Partial fix for HWPF Range.insertBefore() and Range.delete() with unicode characters</action>
            <action dev="POI-DEVELOPERS" type="fix">44977 - Support for AM/PM in excel date formats</action>
index 056c872e7c2ae842f428267580e5b4ecd8f12de8..3e95aaee4c4b93424759a8f1b78d3d008d8a52c0 100644 (file)
@@ -295,8 +295,23 @@ public final class HSSFRow implements Comparable, Row {
 
     /**
      * Get the hssfcell representing a given column (logical cell)
-     *  0-based.  If you ask for a cell that is not defined....
+     *  0-based. If you ask for a cell that is not defined, then
      *  you get a null.
+     * This is the basic call, with no policies applied
+     *
+     * @param cellnum  0 based column number
+     * @return HSSFCell representing that column or null if undefined.
+     */
+    private HSSFCell retrieveCell(int cellnum) {
+        if(cellnum<0||cellnum>=cells.length) return null;
+        return cells[cellnum];
+    }
+    
+    /**
+     * Get the hssfcell representing a given column (logical cell)
+     *  0-based.  If you ask for a cell that is not defined then
+     *  you get a null, unless you have set a different
+     *  {@link MissingCellPolicy} on the base workbook.
      * Short method signature provided to retain binary
      *  compatibility.
      *
@@ -307,17 +322,18 @@ public final class HSSFRow implements Comparable, Row {
         int ushortCellNum = cellnum & 0x0000FFFF; // avoid sign extension
         return getCell(ushortCellNum);
     }
+    
     /**
      * Get the hssfcell representing a given column (logical cell)
-     *  0-based.  If you ask for a cell that is not defined....
-     *  you get a null.
+     *  0-based.  If you ask for a cell that is not defined then
+     *  you get a null, unless you have set a different
+     *  {@link MissingCellPolicy} on the base workbook.
      *
      * @param cellnum  0 based column number
      * @return HSSFCell representing that column or null if undefined.
      */
     public HSSFCell getCell(int cellnum) {
-        if(cellnum<0||cellnum>=cells.length) return null;
-        return cells[cellnum];
+       return getCell(cellnum, book.getMissingCellPolicy());
     }
     
     /**
@@ -330,7 +346,7 @@ public final class HSSFRow implements Comparable, Row {
      * @return representing that column or null if undefined + policy allows.
      */
     public HSSFCell getCell(int cellnum, MissingCellPolicy policy) {
-       HSSFCell cell = getCell(cellnum);
+       HSSFCell cell = retrieveCell(cellnum);
        if(policy == RETURN_NULL_AND_BLANK) {
                return cell;
        }
@@ -354,7 +370,6 @@ public final class HSSFRow implements Comparable, Row {
      * get the number of the first cell contained in this row.
      * @return short representing the first logical cell in the row, or -1 if the row does not contain any cells.
      */
-
     public short getFirstCellNum()
     {
         if (getPhysicalNumberOfCells() == 0)
index 32fce2f0992413bd146a402914d8be91d09b4250..c059b028aff583e32d4a9bc64f535ee71fbd9d2c 100644 (file)
@@ -51,7 +51,6 @@ import org.apache.poi.hssf.record.RecordFactory;
 import org.apache.poi.hssf.record.SSTRecord;
 import org.apache.poi.hssf.record.UnicodeString;
 import org.apache.poi.hssf.record.UnknownRecord;
-import org.apache.poi.hssf.record.WindowTwoRecord;
 import org.apache.poi.hssf.record.formula.Area3DPtg;
 import org.apache.poi.hssf.record.formula.MemFuncPtg;
 import org.apache.poi.hssf.record.formula.UnionPtg;
@@ -60,6 +59,7 @@ import org.apache.poi.hssf.util.SheetReferences;
 import org.apache.poi.poifs.filesystem.DirectoryNode;
 import org.apache.poi.poifs.filesystem.POIFSFileSystem;
 import org.apache.poi.ss.usermodel.CreationHelper;
+import org.apache.poi.ss.usermodel.Row.MissingCellPolicy;
 import org.apache.poi.util.POILogFactory;
 import org.apache.poi.util.POILogger;
 
@@ -119,6 +119,13 @@ public class HSSFWorkbook extends POIDocument implements org.apache.poi.ss.userm
      * someplace else.
      */
     private HSSFDataFormat formatter;
+    
+    /**
+     * The policy to apply in the event of missing or
+     *  blank cells when fetching from a row.
+     * See {@link MissingCellPolicy}
+     */
+    private MissingCellPolicy missingCellPolicy = HSSFRow.RETURN_NULL_AND_BLANK;
 
 
     /** Extended windows meta file */
@@ -362,8 +369,28 @@ public class HSSFWorkbook extends POIDocument implements org.apache.poi.ss.userm
              log.log(POILogger.DEBUG, "convertLabelRecords exit");
      }
 
+       /**
+        * Retrieves the current policy on what to do when
+        *  getting missing or blank cells from a row.
+        * The default is to return blank and null cells.
+        *  {@link MissingCellPolicy}
+        */
+       public MissingCellPolicy getMissingCellPolicy() {
+               return missingCellPolicy;
+       }
+
+       /**
+        * Sets the policy on what to do when
+        *  getting missing or blank cells from a row.
+        * This will then apply to all calls to 
+        *  {@link Row.getCell()}. See
+        *  {@link MissingCellPolicy}
+        */
+       public void setMissingCellPolicy(MissingCellPolicy missingCellPolicy) {
+               this.missingCellPolicy = missingCellPolicy;
+       }
 
-    /**
+       /**
      * sets the order of appearance for a given sheet.
      *
      * @param sheetname the name of the sheet to reorder
index f69bb726cf638af548fad9e2c620a740a064a328..f68fd35c620e3c253543d19476d07da74c2052d0 100644 (file)
@@ -17,6 +17,7 @@
 
 package org.apache.poi.ss.usermodel;
 
+import org.apache.poi.ss.usermodel.Row.MissingCellPolicy;
 import org.apache.poi.hssf.usermodel.*;
 import org.apache.poi.hssf.util.SheetReferences;
 
@@ -44,4 +45,20 @@ public interface Workbook {
        int getExternalSheetIndex(int internalSheetIndex);
 
     CreationHelper getCreationHelper();
+
+       /**
+        * Retrieves the current policy on what to do when
+        *  getting missing or blank cells from a row.
+        * The default is to return blank and null cells.
+        *  {@link MissingCellPolicy}
+        */
+       MissingCellPolicy getMissingCellPolicy();
+       /**
+        * Sets the policy on what to do when
+        *  getting missing or blank cells from a row.
+        * This will then apply to all calls to 
+        *  {@link Row.getCell()}. See
+        *  {@link MissingCellPolicy}
+        */
+       void setMissingCellPolicy(MissingCellPolicy missingCellPolicy);
 }
index 53f439b71388ec1cdc73f58d0453624a47e85566..dbc7fdc97af2e0e1cde8d40048b597f87e6b1787 100644 (file)
@@ -21,6 +21,7 @@ import java.io.IOException;
 import java.io.OutputStream;
 import java.util.List;
 
+import org.apache.poi.ss.usermodel.Row.MissingCellPolicy;
 import org.apache.poi.ss.util.SheetReferences;
 
 public interface Workbook {
@@ -406,6 +407,22 @@ public interface Workbook {
      */
     void removePrintArea(int sheetIndex);
 
+       /**
+        * Retrieves the current policy on what to do when
+        *  getting missing or blank cells from a row.
+        * The default is to return blank and null cells.
+        *  {@link MissingCellPolicy}
+        */
+       MissingCellPolicy getMissingCellPolicy();
+       /**
+        * Sets the policy on what to do when
+        *  getting missing or blank cells from a row.
+        * This will then apply to all calls to 
+        *  {@link Row.getCell()}. See
+        *  {@link MissingCellPolicy}
+        */
+       void setMissingCellPolicy(MissingCellPolicy missingCellPolicy);
+
     /** creates a new named range and add it to the model
      * @return named range high level
      */
@@ -493,4 +510,4 @@ public interface Workbook {
      *  classes.
      */
     CreationHelper getCreationHelper();
-}
\ No newline at end of file
+}
index 3047368e22a022d6a31dbf5419fcd637ac00d6ef..a2d188a3d23243db30b7879b0007f825c4d0f295 100644 (file)
@@ -129,7 +129,7 @@ public class XSSFRow implements Row {
         return xcell;
     }
 
-    public Cell getCell(int cellnum) {
+    private Cell retrieveCell(int cellnum) {
         Iterator<Cell> it = cellIterator();
         for ( ; it.hasNext() ; ) {
                Cell cell = it.next();
@@ -140,8 +140,21 @@ public class XSSFRow implements Row {
         return null;
     }
     
+    /**
+     * Returns the cell at the given (0 based) index,
+     *  with the {@link MissingCellPolicy} from the
+     *  parent Workbook.
+     */
+    public Cell getCell(int cellnum) {
+       return getCell(cellnum, sheet.getWorkbook().getMissingCellPolicy());
+    }
+    
+    /**
+     * Returns the cell at the given (0 based) index,
+     *  with the specified {@link MissingCellPolicy}
+     */
     public Cell getCell(int cellnum, MissingCellPolicy policy) {
-       Cell cell = getCell(cellnum);
+       Cell cell = retrieveCell(cellnum);
        if(policy == RETURN_NULL_AND_BLANK) {
                return cell;
        }
index 37f130a74e9b39db8ca8f11f7288805a0ad20cc4..9fe9ff80ae753158efad0630a1cc8a7c7ca0d2af 100644 (file)
@@ -35,10 +35,12 @@ import org.apache.poi.ss.usermodel.DataFormat;
 import org.apache.poi.ss.usermodel.Font;
 import org.apache.poi.ss.usermodel.Palette;
 import org.apache.poi.ss.usermodel.PictureData;
+import org.apache.poi.ss.usermodel.Row;
 import org.apache.poi.ss.usermodel.SharedStringSource;
 import org.apache.poi.ss.usermodel.Sheet;
 import org.apache.poi.ss.usermodel.StylesSource;
 import org.apache.poi.ss.usermodel.Workbook;
+import org.apache.poi.ss.usermodel.Row.MissingCellPolicy;
 import org.apache.poi.ss.util.SheetReferences;
 import org.apache.poi.util.POILogFactory;
 import org.apache.poi.util.POILogger;
@@ -220,6 +222,8 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
     
     private SharedStringSource sharedStringSource;
     private StylesSource stylesSource;
+    
+    private MissingCellPolicy missingCellPolicy = Row.RETURN_NULL_AND_BLANK;
 
     private static POILogger log = POILogFactory.getLogger(XSSFWorkbook.class);
     
@@ -634,6 +638,26 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
         this.workbook.getSheets().removeSheet(index);
     }
 
+       /**
+        * Retrieves the current policy on what to do when
+        *  getting missing or blank cells from a row.
+        * The default is to return blank and null cells.
+        *  {@link MissingCellPolicy}
+        */
+       public MissingCellPolicy getMissingCellPolicy() {
+               return missingCellPolicy;
+       }
+       /**
+        * Sets the policy on what to do when
+        *  getting missing or blank cells from a row.
+        * This will then apply to all calls to 
+        *  {@link Row.getCell()}. See
+        *  {@link MissingCellPolicy}
+        */
+       public void setMissingCellPolicy(MissingCellPolicy missingCellPolicy) {
+               this.missingCellPolicy = missingCellPolicy;
+       }
+       
     public void setBackupFlag(boolean backupValue) {
         // TODO Auto-generated method stub
 
index d307a0e2514e4c352bc9b4bf3b845fa04e1ba741..3c8f3a87394f94145717fe96f1d78eb4e508613c 100644 (file)
@@ -20,6 +20,7 @@ package org.apache.poi.hssf.usermodel;
 import junit.framework.TestCase;
 
 import org.apache.poi.hssf.HSSFTestDataSamples;
+import org.apache.poi.ss.usermodel.Row.MissingCellPolicy;
 
 /**
  * Test HSSFRow is okay.
@@ -224,7 +225,7 @@ public final class TestHSSFRow extends TestCase {
         row.createCell((short)4, HSSFCell.CELL_TYPE_BLANK);
         row.createCell((short)5).setCellValue(4);
         
-        // First up, no policy
+        // First up, no policy given, uses default
         assertEquals(HSSFCell.CELL_TYPE_STRING,  row.getCell(0).getCellType());
         assertEquals(HSSFCell.CELL_TYPE_NUMERIC, row.getCell(1).getCellType());
         assertEquals(null, row.getCell(2));
@@ -263,5 +264,17 @@ public final class TestHSSFRow extends TestCase {
         assertEquals((short)3, row.getCell(3, HSSFRow.CREATE_NULL_AS_BLANK).getCellNum());
         assertEquals((short)4, row.getCell(4, HSSFRow.CREATE_NULL_AS_BLANK).getCellNum());
         assertEquals((short)5, row.getCell(5, HSSFRow.CREATE_NULL_AS_BLANK).getCellNum());
+        
+        
+        // Now change the cell policy on the workbook, check
+        //  that that is now used if no policy given
+        book.setMissingCellPolicy(HSSFRow.RETURN_BLANK_AS_NULL);
+        
+        assertEquals(HSSFCell.CELL_TYPE_STRING,  row.getCell(0).getCellType());
+        assertEquals(HSSFCell.CELL_TYPE_NUMERIC, row.getCell(1).getCellType());
+        assertEquals(null, row.getCell(2));
+        assertEquals(null, row.getCell(3));
+        assertEquals(null, row.getCell(4));
+        assertEquals(HSSFCell.CELL_TYPE_NUMERIC, row.getCell(5).getCellType());
     }
 }