Search in sources :

Example 16 with CTCols

use of org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCols in project poi by apache.

the class ColumnHelper method getColumn1Based.

/**
     * Returns the Column at the given 1 based index.
     * POI default is 0 based, but the file stores
     *  as 1 based.
     */
public CTCol getColumn1Based(long index1, boolean splitColumns) {
    CTCols cols = worksheet.getColsArray(0);
    // Fetching the array is quicker than working on the new style
    //  list, assuming we need to read many of them (which we often do),
    //  and assuming we're not making many changes (which we're not)
    CTCol[] colArray = cols.getColArray();
    for (CTCol col : colArray) {
        long colMin = col.getMin();
        long colMax = col.getMax();
        if (colMin <= index1 && colMax >= index1) {
            if (splitColumns) {
                if (colMin < index1) {
                    insertCol(cols, colMin, (index1 - 1), new CTCol[] { col });
                }
                if (colMax > index1) {
                    insertCol(cols, (index1 + 1), colMax, new CTCol[] { col });
                }
                col.setMin(index1);
                col.setMax(index1);
            }
            return col;
        }
    }
    return null;
}
Also used : CTCols(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCols) CTCol(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCol)

Example 17 with CTCols

use of org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCols in project poi by apache.

the class ColumnHelper method cloneCol.

private CTCol cloneCol(final CTCols cols, final CTCol col, final long[] newRange) {
    CTCol cloneCol = cloneCol(cols, col);
    cloneCol.setMin(newRange[0]);
    cloneCol.setMax(newRange[1]);
    return cloneCol;
}
Also used : CTCol(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCol)

Example 18 with CTCols

use of org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCols in project poi by apache.

the class TestXSSFColGrouping method testMergingOverlappingCols_OVERLAPS_2_MINOR.

/**
     * Test the cols element is correct in case of NumericRanges.OVERLAPS_2_MINOR
     */
@Test
public void testMergingOverlappingCols_OVERLAPS_2_MINOR() throws IOException {
    XSSFWorkbook wb1 = new XSSFWorkbook();
    XSSFSheet sheet = wb1.createSheet("test");
    CTCols cols = sheet.getCTWorksheet().getColsArray(0);
    CTCol col = cols.addNewCol();
    col.setMin(2 + 1);
    col.setMax(4 + 1);
    col.setWidth(20);
    col.setCustomWidth(true);
    sheet.groupColumn((short) 1, (short) 3);
    cols = sheet.getCTWorksheet().getColsArray(0);
    logger.log(POILogger.DEBUG, "testMergingOverlappingCols_OVERLAPS_2_MINOR/cols:" + cols);
    assertEquals(1, cols.getColArray(0).getOutlineLevel());
    // 1 based
    assertEquals(2, cols.getColArray(0).getMin());
    // 1 based
    assertEquals(2, cols.getColArray(0).getMax());
    assertEquals(false, cols.getColArray(0).getCustomWidth());
    assertEquals(1, cols.getColArray(1).getOutlineLevel());
    // 1 based
    assertEquals(3, cols.getColArray(1).getMin());
    // 1 based        
    assertEquals(4, cols.getColArray(1).getMax());
    assertEquals(true, cols.getColArray(1).getCustomWidth());
    assertEquals(0, cols.getColArray(2).getOutlineLevel());
    // 1 based
    assertEquals(5, cols.getColArray(2).getMin());
    // 1 based
    assertEquals(5, cols.getColArray(2).getMax());
    assertEquals(true, cols.getColArray(2).getCustomWidth());
    assertEquals(3, cols.sizeOfColArray());
    XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(wb1, "testMergingOverlappingCols_OVERLAPS_2_MINOR");
    sheet = wb2.getSheet("test");
    for (int i = 2; i <= 4; i++) {
        assertEquals("Unexpected width of column " + i, 20 * 256, sheet.getColumnWidth(i));
    }
    assertEquals("Unexpected width of column " + 1, sheet.getDefaultColumnWidth() * 256, sheet.getColumnWidth(1));
    wb2.close();
    wb1.close();
}
Also used : CTCols(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCols) CTCol(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCol) Test(org.junit.Test)

Example 19 with CTCols

use of org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCols in project poi by apache.

the class TestXSSFColGrouping method testNoColsWithoutWidthWhenGroupingAndCollapsing.

/**
     * Tests that POI doesn't produce "col" elements without "width" attribute. 
     * POI-52186
     */
@Test
public void testNoColsWithoutWidthWhenGroupingAndCollapsing() throws IOException {
    XSSFWorkbook wb1 = new XSSFWorkbook();
    XSSFSheet sheet = wb1.createSheet("test");
    sheet.setColumnWidth(4, 5000);
    sheet.setColumnWidth(5, 5000);
    sheet.groupColumn((short) 4, (short) 5);
    sheet.setColumnGroupCollapsed(4, true);
    CTCols cols = sheet.getCTWorksheet().getColsArray(0);
    logger.log(POILogger.DEBUG, "test52186_2/cols:" + cols);
    XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(wb1, "testNoColsWithoutWidthWhenGroupingAndCollapsing");
    sheet = wb2.getSheet("test");
    for (int i = 4; i <= 5; i++) {
        assertEquals("Unexpected width of column " + i, 5000, sheet.getColumnWidth(i));
    }
    cols = sheet.getCTWorksheet().getColsArray(0);
    for (CTCol col : cols.getColArray()) {
        assertTrue("Col width attribute is unset: " + col, col.isSetWidth());
    }
    wb2.close();
    wb1.close();
}
Also used : CTCols(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCols) CTCol(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCol) Test(org.junit.Test)

Example 20 with CTCols

use of org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCols in project poi by apache.

the class TestXSSFColGrouping method testMergingOverlappingCols_OVERLAPS_1_MINOR.

/**
     * Test the cols element is correct in case of NumericRanges.OVERLAPS_1_MINOR
     */
@Test
public void testMergingOverlappingCols_OVERLAPS_1_MINOR() throws IOException {
    XSSFWorkbook wb1 = new XSSFWorkbook();
    XSSFSheet sheet = wb1.createSheet("test");
    CTCols cols = sheet.getCTWorksheet().getColsArray(0);
    CTCol col = cols.addNewCol();
    col.setMin(2 + 1);
    col.setMax(4 + 1);
    col.setWidth(20);
    col.setCustomWidth(true);
    sheet.groupColumn((short) 3, (short) 5);
    cols = sheet.getCTWorksheet().getColsArray(0);
    logger.log(POILogger.DEBUG, "testMergingOverlappingCols_OVERLAPS_1_MINOR/cols:" + cols);
    assertEquals(0, cols.getColArray(0).getOutlineLevel());
    // 1 based
    assertEquals(3, cols.getColArray(0).getMin());
    // 1 based
    assertEquals(3, cols.getColArray(0).getMax());
    assertEquals(true, cols.getColArray(0).getCustomWidth());
    assertEquals(1, cols.getColArray(1).getOutlineLevel());
    // 1 based
    assertEquals(4, cols.getColArray(1).getMin());
    // 1 based        
    assertEquals(5, cols.getColArray(1).getMax());
    assertEquals(true, cols.getColArray(1).getCustomWidth());
    assertEquals(1, cols.getColArray(2).getOutlineLevel());
    // 1 based
    assertEquals(6, cols.getColArray(2).getMin());
    // 1 based
    assertEquals(6, cols.getColArray(2).getMax());
    assertEquals(false, cols.getColArray(2).getCustomWidth());
    assertEquals(3, cols.sizeOfColArray());
    XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(wb1, "testMergingOverlappingCols_OVERLAPS_1_MINOR");
    sheet = wb2.getSheet("test");
    for (int i = 2; i <= 4; i++) {
        assertEquals("Unexpected width of column " + i, 20 * 256, sheet.getColumnWidth(i));
    }
    assertEquals("Unexpected width of column " + 5, sheet.getDefaultColumnWidth() * 256, sheet.getColumnWidth(5));
    wb2.close();
    wb1.close();
}
Also used : CTCols(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCols) CTCol(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCol) Test(org.junit.Test)

Aggregations

CTCols (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCols)27 Test (org.junit.Test)24 CTCol (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCol)23 SXSSFWorkbook (org.apache.poi.xssf.streaming.SXSSFWorkbook)6 CTWorksheet (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet)6 TreeSet (java.util.TreeSet)1