]> source.dussan.org Git - poi.git/commitdiff
fixed ColumnHelper to correctly handle columns included in a column span
authorYegor Kozlov <yegor@apache.org>
Sun, 4 Oct 2009 10:08:47 +0000 (10:08 +0000)
committerYegor Kozlov <yegor@apache.org>
Sun, 4 Oct 2009 10:08:47 +0000 (10:08 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@821497 13f79535-47bb-0310-9956-ffa450edef68

src/documentation/content/xdocs/status.xml
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java
src/ooxml/java/org/apache/poi/xssf/usermodel/helpers/ColumnHelper.java
src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java
test-data/spreadsheet/47804.xlsx [new file with mode: 0644]
test-data/spreadsheet/47862.xlsx [new file with mode: 0644]

index 446790656cfa9b513956dd8cd80fdcc84dc9f60d..a84c71bfb9fdf41be1480adcb7733686cd323e74 100644 (file)
@@ -33,6 +33,9 @@
 
     <changes>
         <release version="3.6-beta1" date="2009-??-??">
+           <action dev="POI-DEVELOPERS" type="add">47839 - improved API for OOXML custom properties</action>
+           <action dev="POI-DEVELOPERS" type="fix">47862 - fixed XSSFSheet.setColumnWidth to handle columns included in a column span</action>
+           <action dev="POI-DEVELOPERS" type="fix">47804 - fixed XSSFSheet.setColumnHidden to handle columns included in a column span</action>
            <action dev="POI-DEVELOPERS" type="fix">47889 - fixed XSSFCell.getStringCellValue() to properly handle cached formula results</action>
         </release>
         <release version="3.5-FINAL" date="2009-09-28">
index 4fb9185b9054c939dd76f701749c8d97853ae935..8db42bc183c229dc8e9d5962bc3762b33d0c0519 100644 (file)
@@ -1086,7 +1086,8 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
      * @return hidden - <code>false</code> if the column is visible
      */
     public boolean isColumnHidden(int columnIndex) {
-        return columnHelper.getColumn(columnIndex, false).getHidden();
+        CTCol col = columnHelper.getColumn(columnIndex, false);
+        return col != null && col.getHidden();
     }
 
     /**
index 4f8f33b5bcd690facbbc330e4d16a75b7fb31e03..8359ab818d06c9656059571345f79b4bc99061bc 100644 (file)
@@ -72,7 +72,7 @@ public class ColumnHelper {
         worksheet.setColsArray(0, newCols);
     }
 
-    public void sortColumns(CTCols newCols) {
+    public static void sortColumns(CTCols newCols) {
         CTCol[] colArray = newCols.getColArray();
         Arrays.sort(colArray, new CTColComparator());
         newCols.setColArray(colArray);
@@ -235,17 +235,17 @@ public class ColumnHelper {
         col.setBestFit(bestFit);
     }
     public void setCustomWidth(long index, boolean bestFit) {
-        CTCol col = getOrCreateColumn1Based(index+1, false);
+        CTCol col = getOrCreateColumn1Based(index+1, true);
         col.setCustomWidth(bestFit);
     }
 
     public void setColWidth(long index, double width) {
-        CTCol col = getOrCreateColumn1Based(index+1, false);
+        CTCol col = getOrCreateColumn1Based(index+1, true);
         col.setWidth(width);
     }
 
     public void setColHidden(long index, boolean hidden) {
-        CTCol col = getOrCreateColumn1Based(index+1, false);
+        CTCol col = getOrCreateColumn1Based(index+1, true);
         col.setHidden(hidden);
     }
 
index 39a1a4d766145626bf5777996ffa474cef1d3a4b..d0110f410bdba1577ec9163a8f250370b17b057d 100644 (file)
@@ -717,4 +717,130 @@ public class TestXSSFSheet extends BaseTestSheet {
         assertEquals(33.0, col.getWidth(), 0.0);
         assertTrue(col.getCustomWidth());
     }
+
+    /**
+     * Setting width of a column included in a column span
+     */
+    public void test47862() {
+        XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("47862.xlsx");
+        XSSFSheet sheet = wb.getSheetAt(0);
+        CTCols cols = sheet.getCTWorksheet().getColsArray(0);
+        //<cols>
+        //  <col min="1" max="5" width="15.77734375" customWidth="1"/>
+        //</cols>
+
+        //a span of columns [1,5]
+        assertEquals(1, cols.sizeOfColArray());
+        CTCol col = cols.getColArray(0);
+        assertEquals(1, col.getMin());
+        assertEquals(5, col.getMax());
+        double swidth = 15.77734375; //width of columns in the span
+        assertEquals(swidth, col.getWidth());
+
+        for (int i = 0; i < 5; i++) {
+            assertEquals((int)(swidth*256), sheet.getColumnWidth(i));
+        }
+
+        int[] cw = new int[]{10, 15, 20, 25, 30};
+        for (int i = 0; i < 5; i++) {
+            sheet.setColumnWidth(i, cw[i]*256);
+        }
+
+        //the check below failed prior to fix of Bug #47862
+        ColumnHelper.sortColumns(cols);
+        //<cols>
+        //  <col min="1" max="1" customWidth="true" width="10.0" />
+        //  <col min="2" max="2" customWidth="true" width="15.0" />
+        //  <col min="3" max="3" customWidth="true" width="20.0" />
+        //  <col min="4" max="4" customWidth="true" width="25.0" />
+        //  <col min="5" max="5" customWidth="true" width="30.0" />
+        //</cols>
+
+        //now the span is splitted into 5 individual columns
+        assertEquals(5, cols.sizeOfColArray());
+        for (int i = 0; i < 5; i++) {
+            assertEquals(cw[i]*256, sheet.getColumnWidth(i));
+            assertEquals((double)cw[i], cols.getColArray(i).getWidth());
+        }
+
+        //serialize and check again 
+        wb = getTestDataProvider().writeOutAndReadBack(wb);
+        sheet = wb.getSheetAt(0);
+        cols = sheet.getCTWorksheet().getColsArray(0);
+        assertEquals(5, cols.sizeOfColArray());
+        for (int i = 0; i < 5; i++) {
+            assertEquals(cw[i]*256, sheet.getColumnWidth(i));
+            assertEquals((double)cw[i], cols.getColArray(i).getWidth());
+        }
+    }
+
+    /**
+     * Hiding a column included in a column span
+     */
+    public void test47804() {
+        XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("47804.xlsx");
+        XSSFSheet sheet = wb.getSheetAt(0);
+        CTCols cols = sheet.getCTWorksheet().getColsArray(0);
+        assertEquals(2, cols.sizeOfColArray());
+        CTCol col;
+        //<cols>
+        //  <col min="2" max="4" width="12" customWidth="1"/>
+        //  <col min="7" max="7" width="10.85546875" customWidth="1"/>
+        //</cols>
+
+        //a span of columns [2,4]
+        col = cols.getColArray(0);
+        assertEquals(2, col.getMin());
+        assertEquals(4, col.getMax());
+        //individual column
+        col = cols.getColArray(1);
+        assertEquals(7, col.getMin());
+        assertEquals(7, col.getMax());
+
+        sheet.setColumnHidden(2, true); // Column C
+        sheet.setColumnHidden(6, true); // Column G
+
+        assertTrue(sheet.isColumnHidden(2));
+        assertTrue(sheet.isColumnHidden(6));
+
+        //other columns but C and G are not hidden
+        assertFalse(sheet.isColumnHidden(1));
+        assertFalse(sheet.isColumnHidden(3));
+        assertFalse(sheet.isColumnHidden(4));
+        assertFalse(sheet.isColumnHidden(5));
+
+        //the check below failed prior to fix of Bug #47804
+        ColumnHelper.sortColumns(cols);
+        //the span is now splitted into three parts
+        //<cols>
+        //  <col min="2" max="2" customWidth="true" width="12.0" />
+        //  <col min="3" max="3" customWidth="true" width="12.0" hidden="true"/>
+        //  <col min="4" max="4" customWidth="true" width="12.0"/>
+        //  <col min="7" max="7" customWidth="true" width="10.85546875" hidden="true"/>
+        //</cols>
+
+        assertEquals(4, cols.sizeOfColArray());
+        col = cols.getColArray(0);
+        assertEquals(2, col.getMin());
+        assertEquals(2, col.getMax());
+        col = cols.getColArray(1);
+        assertEquals(3, col.getMin());
+        assertEquals(3, col.getMax());
+        col = cols.getColArray(2);
+        assertEquals(4, col.getMin());
+        assertEquals(4, col.getMax());
+        col = cols.getColArray(3);
+        assertEquals(7, col.getMin());
+        assertEquals(7, col.getMax());
+
+        //serialize and check again
+        wb = getTestDataProvider().writeOutAndReadBack(wb);
+        sheet = wb.getSheetAt(0);
+        assertTrue(sheet.isColumnHidden(2));
+        assertTrue(sheet.isColumnHidden(6));
+        assertFalse(sheet.isColumnHidden(1));
+        assertFalse(sheet.isColumnHidden(3));
+        assertFalse(sheet.isColumnHidden(4));
+        assertFalse(sheet.isColumnHidden(5));
+    }
 }
diff --git a/test-data/spreadsheet/47804.xlsx b/test-data/spreadsheet/47804.xlsx
new file mode 100644 (file)
index 0000000..7e14346
Binary files /dev/null and b/test-data/spreadsheet/47804.xlsx differ
diff --git a/test-data/spreadsheet/47862.xlsx b/test-data/spreadsheet/47862.xlsx
new file mode 100644 (file)
index 0000000..d5d19c8
Binary files /dev/null and b/test-data/spreadsheet/47862.xlsx differ