Beispiel #1
0
  /**
   * 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;
  }
Beispiel #2
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);
  }