@SuppressWarnings("deprecation") public int getIndexOfColumn(CTCols cols, CTCol searchCol) { int i = 0; for (CTCol col : cols.getColArray()) { if (col.getMin() == searchCol.getMin() && col.getMax() == searchCol.getMax()) { return i; } i++; } return -1; }
/** * generated <code><col><code> tags. * @param sheet * @param table container. */ private void generateColumns(XSSFSheet sheet, Element table) { List<CTCols> colsList = sheet.getCTWorksheet().getColsList(); MathContext mc = new MathContext(3); for (CTCols cols : colsList) { long oldLevel = 1; for (CTCol col : cols.getColArray()) { while (true) { if (oldLevel == col.getMin()) { break; } Element column = htmlDocumentFacade.createTableColumn(); // htmlDocumentFacade.addStyleClass(column, "col", "width:2cm;"); column.setAttribute("style", "width:2cm;"); table.appendChild(column); oldLevel++; } Element column = htmlDocumentFacade.createTableColumn(); String width = new BigDecimal(sheet.getColumnWidth(Long.bitCount(oldLevel)) / 1440.0, mc).toString(); column.setAttribute("style", "width:".concat(width).concat("cm;")); table.appendChild(column); oldLevel++; } } }
/** * 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) @SuppressWarnings("deprecation") 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; }
public CTCol cloneCol(CTCols cols, CTCol col) { CTCol newCol = cols.addNewCol(); newCol.setMin(col.getMin()); newCol.setMax(col.getMax()); setColumnAttributes(col, newCol); return newCol; }
@SuppressWarnings("deprecation") private boolean columnExists(CTCols cols, long min, long max) { for (CTCol col : cols.getColArray()) { if (col.getMin() == min && col.getMax() == max) { return true; } } return false; }
@SuppressWarnings("deprecation") private boolean columnExists1Based(CTCols cols, long index1) { for (CTCol col : cols.getColArray()) { if (col.getMin() == index1) { return true; } } return false; }
public int getIndexOfColumn(CTCols cols, CTCol col) { for (int i = 0; i < cols.sizeOfColArray(); i++) { if (cols.getColArray(i).getMin() == col.getMin() && cols.getColArray(i).getMax() == col.getMax()) { return i; } } return -1; }
/** * 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 colsArray = worksheet.getColsArray(0); for (int i = 0; i < colsArray.sizeOfColArray(); i++) { CTCol colArray = colsArray.getColArray(i); if (colArray.getMin() <= index1 && colArray.getMax() >= index1) { if (splitColumns) { if (colArray.getMin() < index1) { insertCol(colsArray, colArray.getMin(), (index1 - 1), new CTCol[] {colArray}); } if (colArray.getMax() > index1) { insertCol(colsArray, (index1 + 1), colArray.getMax(), new CTCol[] {colArray}); } colArray.setMin(index1); colArray.setMax(index1); } return colArray; } } return null; }
public void testCloneCol() { CTWorksheet worksheet = CTWorksheet.Factory.newInstance(); ColumnHelper helper = new ColumnHelper(worksheet); CTCols cols = CTCols.Factory.newInstance(); CTCol col = CTCol.Factory.newInstance(); col.setMin(2); col.setMax(8); col.setHidden(true); col.setWidth(13.4); CTCol newCol = helper.cloneCol(cols, col); assertEquals(2, newCol.getMin()); assertEquals(8, newCol.getMax()); assertTrue(newCol.getHidden()); assertEquals(13.4, newCol.getWidth(), 0.0); }
/** @see <a href="http://en.wikipedia.org/wiki/Sweep_line_algorithm">Sweep line algorithm</a> */ private void sweepCleanColumns(CTCols cols, CTCol[] flattenedColsArray, CTCol overrideColumn) { List<CTCol> flattenedCols = new ArrayList<CTCol>(Arrays.asList(flattenedColsArray)); TreeSet<CTCol> currentElements = new TreeSet<CTCol>(CTColComparator.BY_MAX); ListIterator<CTCol> flIter = flattenedCols.listIterator(); CTCol haveOverrideColumn = null; long lastMaxIndex = 0; long currentMax = 0; while (flIter.hasNext()) { CTCol col = flIter.next(); long currentIndex = col.getMin(); long colMax = col.getMax(); long nextIndex = (colMax > currentMax) ? colMax : currentMax; if (flIter.hasNext()) { nextIndex = flIter.next().getMin(); flIter.previous(); } Iterator<CTCol> iter = currentElements.iterator(); while (iter.hasNext()) { CTCol elem = iter.next(); if (currentIndex <= elem.getMax()) break; // all passed elements have been purged iter.remove(); } if (!currentElements.isEmpty() && lastMaxIndex < currentIndex) { // we need to process previous elements first insertCol( cols, lastMaxIndex, currentIndex - 1, currentElements.toArray(new CTCol[currentElements.size()]), true, haveOverrideColumn); } currentElements.add(col); if (colMax > currentMax) currentMax = colMax; if (col.equals(overrideColumn)) haveOverrideColumn = overrideColumn; while (currentIndex <= nextIndex && !currentElements.isEmpty()) { Set<CTCol> currentIndexElements = new HashSet<CTCol>(); long currentElemIndex; { // narrow scope of currentElem CTCol currentElem = currentElements.first(); currentElemIndex = currentElem.getMax(); currentIndexElements.add(currentElem); while (true) { CTCol higherElem = currentElements.higher(currentElem); if (higherElem == null || higherElem.getMax() != currentElemIndex) break; currentElem = higherElem; currentIndexElements.add(currentElem); if (colMax > currentMax) currentMax = colMax; if (col.equals(overrideColumn)) haveOverrideColumn = overrideColumn; } } if (currentElemIndex < nextIndex || !flIter.hasNext()) { insertCol( cols, currentIndex, currentElemIndex, currentElements.toArray(new CTCol[currentElements.size()]), true, haveOverrideColumn); if (flIter.hasNext()) { if (nextIndex > currentElemIndex) { currentElements.removeAll(currentIndexElements); if (currentIndexElements.contains(overrideColumn)) haveOverrideColumn = null; } } else { currentElements.removeAll(currentIndexElements); if (currentIndexElements.contains(overrideColumn)) haveOverrideColumn = null; } lastMaxIndex = currentIndex = currentElemIndex + 1; } else { lastMaxIndex = currentIndex; currentIndex = nextIndex + 1; } } } sortColumns(cols); }
public CTCols addCleanColIntoCols(CTCols cols, CTCol col) { boolean colOverlaps = false; for (int i = 0; i < cols.sizeOfColArray(); i++) { CTCol ithCol = cols.getColArray(i); long[] range1 = {ithCol.getMin(), ithCol.getMax()}; long[] range2 = {col.getMin(), col.getMax()}; long[] overlappingRange = NumericRanges.getOverlappingRange(range1, range2); int overlappingType = NumericRanges.getOverlappingType(range1, range2); // different behavior required for each of the 4 different // overlapping types if (overlappingType == NumericRanges.OVERLAPS_1_MINOR) { ithCol.setMax(overlappingRange[0] - 1); CTCol rangeCol = insertCol(cols, overlappingRange[0], overlappingRange[1], new CTCol[] {ithCol, col}); i++; CTCol newCol = insertCol(cols, (overlappingRange[1] + 1), col.getMax(), new CTCol[] {col}); i++; } else if (overlappingType == NumericRanges.OVERLAPS_2_MINOR) { ithCol.setMin(overlappingRange[1] + 1); CTCol rangeCol = insertCol(cols, overlappingRange[0], overlappingRange[1], new CTCol[] {ithCol, col}); i++; CTCol newCol = insertCol(cols, col.getMin(), (overlappingRange[0] - 1), new CTCol[] {col}); i++; } else if (overlappingType == NumericRanges.OVERLAPS_2_WRAPS) { setColumnAttributes(col, ithCol); if (col.getMin() != ithCol.getMin()) { CTCol newColBefore = insertCol(cols, col.getMin(), (ithCol.getMin() - 1), new CTCol[] {col}); i++; } if (col.getMax() != ithCol.getMax()) { CTCol newColAfter = insertCol(cols, (ithCol.getMax() + 1), col.getMax(), new CTCol[] {col}); i++; } } else if (overlappingType == NumericRanges.OVERLAPS_1_WRAPS) { if (col.getMin() != ithCol.getMin()) { CTCol newColBefore = insertCol(cols, ithCol.getMin(), (col.getMin() - 1), new CTCol[] {ithCol}); i++; } if (col.getMax() != ithCol.getMax()) { CTCol newColAfter = insertCol(cols, (col.getMax() + 1), ithCol.getMax(), new CTCol[] {ithCol}); i++; } ithCol.setMin(overlappingRange[0]); ithCol.setMax(overlappingRange[1]); setColumnAttributes(col, ithCol); } if (overlappingType != NumericRanges.NO_OVERLAPS) { colOverlaps = true; } } if (!colOverlaps) { CTCol newCol = cloneCol(cols, col); } sortColumns(cols); return cols; }