private CT_Col insertCol(CT_Cols cols, long min, long max, CT_Col[] colsWithAttributes, bool ignoreExistsCheck, CT_Col overrideColumn) { if (ignoreExistsCheck || !columnExists(cols, min, max)) { CT_Col newCol = cols.InsertNewCol(0); newCol.min = (uint)(min); newCol.max = (uint)(max); foreach (CT_Col col in colsWithAttributes) { SetColumnAttributes(col, newCol); } if (overrideColumn != null) { SetColumnAttributes(overrideColumn, newCol); } return(newCol); } return(null); }
public void TestNoColsWithoutWidthWhenGrouping() { XSSFWorkbook wb = new XSSFWorkbook(); XSSFSheet sheet = (XSSFSheet)wb.CreateSheet("test"); sheet.SetColumnWidth(4, 5000); sheet.SetColumnWidth(5, 5000); sheet.GroupColumn((short)4, (short)7); sheet.GroupColumn((short)9, (short)12); wb = XSSFTestDataSamples.WriteOutAndReadBack(wb, "testNoColsWithoutWidthWhenGrouping"); sheet = (XSSFSheet)wb.GetSheet("test"); CT_Cols cols = sheet.GetCTWorksheet().GetColsArray(0); //logger.log(POILogger.DEBUG, "test52186/cols:" + cols); foreach (CT_Col col in cols.GetColList()) { Assert.IsTrue(col.IsSetWidth(), "Col width attribute is unset: " + col.ToString()); } }
public void TestCleanColumns() { CT_Worksheet worksheet = new CT_Worksheet(); CT_Cols cols1 = worksheet.AddNewCols(); CT_Col col1 = cols1.AddNewCol(); col1.min = (1); col1.max = (1); col1.width = (88); col1.hidden = (true); CT_Col col2 = cols1.AddNewCol(); col2.min = (2); col2.max = (3); CT_Cols cols2 = worksheet.AddNewCols(); CT_Col col4 = cols2.AddNewCol(); col4.min = (13); col4.max = (16384); // Test cleaning cols Assert.AreEqual(2, worksheet.sizeOfColsArray()); int count = countColumns(worksheet); Assert.AreEqual(16375, count); // Clean columns and Test a clean worksheet ColumnHelper helper = new ColumnHelper(worksheet); Assert.AreEqual(1, worksheet.sizeOfColsArray()); count = countColumns(worksheet); Assert.AreEqual(16375, count); // Remember - POI column 0 == OOXML column 1 Assert.AreEqual(88.0, helper.GetColumn(0, false).width, 0.0); Assert.IsTrue(helper.GetColumn(0, false).hidden); Assert.AreEqual(0.0, helper.GetColumn(1, false).width, 0.0); Assert.IsFalse(helper.GetColumn(1, false).hidden); }
public void CleanColumns() { this.newCols = new CT_Cols(); List <CT_Cols> colsArray = this.worksheet.GetColsArray(); if (colsArray != null) { int index1; for (index1 = 0; index1 < colsArray.Count; ++index1) { List <CT_Col> colArray = colsArray[index1].GetColArray(); for (int index2 = 0; index2 < colArray.Count; ++index2) { this.newCols = this.AddCleanColIntoCols(this.newCols, colArray[index2]); } } for (int index2 = index1 - 1; index2 >= 0; --index2) { this.worksheet.RemoveCols(index2); } } this.worksheet.AddNewCols(); this.worksheet.SetColsArray(0, this.newCols); }
/** * Does the column at the given 0 based index exist * in the supplied list of column defInitions? */ public bool columnExists(CT_Cols cols, long index) { return(columnExists1Based(cols, index + 1)); }
//public CT_Cols AddCleanColIntoCols(CT_Cols cols, CT_Col col) //{ // bool colOverlaps = false; // // a Map to remember overlapping columns // Dictionary<long, bool> overlappingCols = new Dictionary<long, bool>(); // int sizeOfColArray = cols.sizeOfColArray(); // for (int i = 0; i < sizeOfColArray; i++) // { // CT_Col ithCol = cols.GetColArray(i); // long[] range1 = { ithCol.min, ithCol.max }; // long[] range2 = { col.min, col.max }; // 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) // { // // move the max border of the ithCol // // and insert a new column within the overlappingRange with merged column attributes // ithCol.max = (uint)(overlappingRange[0] - 1); // insertCol(cols, overlappingRange[0], // overlappingRange[1], new CT_Col[] { ithCol, col }); // i++; // //CT_Col newCol = insertCol(cols, (overlappingRange[1] + 1), col // // .max, new CT_Col[] { col }); // //i++; // } // else if (overlappingType == NumericRanges.OVERLAPS_2_MINOR) // { // // move the min border of the ithCol // // and insert a new column within the overlappingRange with merged column attributes // ithCol.min = (uint)(overlappingRange[1] + 1); // insertCol(cols, overlappingRange[0], // overlappingRange[1], new CT_Col[] { ithCol, col }); // i++; // //CT_Col newCol = insertCol(cols, col.min, // // (overlappingRange[0] - 1), new CT_Col[] { col }); // //i++; // } // else if (overlappingType == NumericRanges.OVERLAPS_2_WRAPS) // { // // merge column attributes, no new column is needed // SetColumnAttributes(col, ithCol); // } // else if (overlappingType == NumericRanges.OVERLAPS_1_WRAPS) // { // // split the ithCol in three columns: before the overlappingRange, overlappingRange, and after the overlappingRange // // before overlappingRange // if (col.min != ithCol.min) // { // insertCol(cols, ithCol.min, (col // .min - 1), new CT_Col[] { ithCol }); // i++; // } // // after the overlappingRange // if (col.max != ithCol.max) // { // insertCol(cols, (col.max + 1), // ithCol.max, new CT_Col[] { ithCol }); // i++; // } // // within the overlappingRange // ithCol.min = (uint)(overlappingRange[0]); // ithCol.max = (uint)(overlappingRange[1]); // SetColumnAttributes(col, ithCol); // } // if (overlappingType != NumericRanges.NO_OVERLAPS) // { // colOverlaps = true; // // remember overlapped columns // for (long j = overlappingRange[0]; j <= overlappingRange[1]; j++) // { // overlappingCols.Add(j, true); // } // } // } // if (!colOverlaps) // { // CloneCol(cols, col); // } // else // { // // insert new columns for ranges without overlaps // long colMin = -1; // for (long j = col.min; j <= col.max; j++) // { // if (overlappingCols.ContainsKey(j) && !overlappingCols[j]) // { // if (colMin < 0) // { // colMin = j; // } // if ((j + 1) > col.max || overlappingCols[(j + 1)]) // { // insertCol(cols, colMin, j, new CT_Col[] { col }); // colMin = -1; // } // } // } // } // SortColumns(cols); // return cols; //} /* * Insert a new CT_Col at position 0 into cols, Setting min=min, max=max and * copying all the colsWithAttributes array cols attributes into newCol */ private CT_Col insertCol(CT_Cols cols, long min, long max, CT_Col[] colsWithAttributes) { return(insertCol(cols, min, max, colsWithAttributes, false, null)); }
/** * @see <a href="http://en.wikipedia.org/wiki/Sweep_line_algorithm">Sweep line algorithm</a> */ private void SweepCleanColumns(CT_Cols cols, CT_Col[] flattenedColsArray, CT_Col overrideColumn) { List <CT_Col> flattenedCols = new List <CT_Col>(flattenedColsArray); TreeSet <CT_Col> currentElements = new TreeSet <CT_Col>(CTColComparator.BY_MAX); IEnumerator <CT_Col> flIter = flattenedCols.GetEnumerator(); CT_Col haveOverrideColumn = null; long lastMaxIndex = 0; long currentMax = 0; IList <CT_Col> toRemove = new List <CT_Col>(); int pos = -1; //while (flIter.hasNext()) while ((pos + 1) < flattenedCols.Count) { //CTCol col = flIter.next(); pos++; CT_Col col = flattenedCols[pos]; long currentIndex = col.min; long colMax = col.max; long nextIndex = (colMax > currentMax) ? colMax : currentMax; //if (flIter.hasNext()) { if ((pos + 1) < flattenedCols.Count) { //nextIndex = flIter.next().getMin(); nextIndex = flattenedCols[pos + 1].min; //flIter.previous(); } IEnumerator <CT_Col> iter = currentElements.GetEnumerator(); toRemove.Clear(); while (iter.MoveNext()) { CT_Col elem = iter.Current; if (currentIndex <= elem.max) { break; // all passed elements have been purged } toRemove.Add(elem); } foreach (CT_Col rc in toRemove) { currentElements.Remove(rc); } if (!(currentElements.Count == 0) && lastMaxIndex < currentIndex) { // we need to process previous elements first CT_Col[] copyCols = new CT_Col[currentElements.Count]; currentElements.CopyTo(copyCols); insertCol(cols, lastMaxIndex, currentIndex - 1, copyCols, true, haveOverrideColumn); } currentElements.Add(col); if (colMax > currentMax) { currentMax = colMax; } if (col.Equals(overrideColumn)) { haveOverrideColumn = overrideColumn; } while (currentIndex <= nextIndex && !(currentElements.Count == 0)) { NPOI.Util.Collections.HashSet <CT_Col> currentIndexElements = new NPOI.Util.Collections.HashSet <CT_Col>(); long currentElemIndex; { // narrow scope of currentElem CT_Col currentElem = currentElements.First(); currentElemIndex = currentElem.max; currentIndexElements.Add(currentElem); while (true) { CT_Col higherElem = currentElements.Higher(currentElem); if (higherElem == null || higherElem.max != currentElemIndex) { break; } currentElem = higherElem; currentIndexElements.Add(currentElem); if (colMax > currentMax) { currentMax = colMax; } if (col.Equals(overrideColumn)) { haveOverrideColumn = overrideColumn; } } } //if (currentElemIndex < nextIndex || !flIter.hasNext()) { if (currentElemIndex < nextIndex || !((pos + 1) < flattenedCols.Count)) { CT_Col[] copyCols = new CT_Col[currentElements.Count]; currentElements.CopyTo(copyCols); insertCol(cols, currentIndex, currentElemIndex, copyCols, true, haveOverrideColumn); //if (flIter.hasNext()) { if ((pos + 1) < flattenedCols.Count) { if (nextIndex > currentElemIndex) { //currentElements.removeAll(currentIndexElements); foreach (CT_Col rc in currentIndexElements) { currentElements.Remove(rc); } if (currentIndexElements.Contains(overrideColumn)) { haveOverrideColumn = null; } } } else { //currentElements.removeAll(currentIndexElements); foreach (CT_Col rc in currentIndexElements) { currentElements.Remove(rc); } if (currentIndexElements.Contains(overrideColumn)) { haveOverrideColumn = null; } } lastMaxIndex = currentIndex = currentElemIndex + 1; } else { lastMaxIndex = currentIndex; currentIndex = nextIndex + 1; } } } SortColumns(cols); }
public void TestAddCleanColIntoCols() { CT_Worksheet worksheet = new CT_Worksheet(); ColumnHelper helper = new ColumnHelper(worksheet); CT_Cols cols1 = new CT_Cols(); CT_Col col1 = cols1.AddNewCol(); col1.min = (1); col1.max = (1); col1.width = (88); col1.hidden = (true); CT_Col col2 = cols1.AddNewCol(); col2.min = (2); col2.max = (3); CT_Col col3 = cols1.AddNewCol(); col3.min = (13); col3.max = (16750); Assert.AreEqual(3, cols1.sizeOfColArray()); CT_Col col4 = cols1.AddNewCol(); col4.min = (8); col4.max = (9); Assert.AreEqual(4, cols1.sizeOfColArray()); CT_Col col5 = new CT_Col(); col5.min = (4); col5.max = (5); helper.AddCleanColIntoCols(cols1, col5); Assert.AreEqual(5, cols1.sizeOfColArray()); CT_Col col6 = new CT_Col(); col6.min = (8); col6.max = (11); col6.hidden = (true); helper.AddCleanColIntoCols(cols1, col6); Assert.AreEqual(6, cols1.sizeOfColArray()); CT_Col col7 = new CT_Col(); col7.min = (6); col7.max = (8); col7.width = (17.0); helper.AddCleanColIntoCols(cols1, col7); Assert.AreEqual(8, cols1.sizeOfColArray()); CT_Col col8 = new CT_Col(); col8.min = (20); col8.max = (30); helper.AddCleanColIntoCols(cols1, col8); Assert.AreEqual(10, cols1.sizeOfColArray()); CT_Col col9 = new CT_Col(); col9.min = (25); col9.max = (27); helper.AddCleanColIntoCols(cols1, col9); // TODO - assert something interesting Assert.AreEqual(12, cols1.col.Count); Assert.AreEqual(1u, cols1.GetColArray(0).min); Assert.AreEqual(16750u, cols1.GetColArray(11).max); }
public CT_Cols AddCleanColIntoCols(CT_Cols cols, CT_Col col) { bool colOverlaps = false; for (int i = 0; i < cols.sizeOfColArray(); i++) { CT_Col ithCol = cols.GetColArray(i); long[] range1 = { ithCol.min, ithCol.max }; long[] range2 = { col.min, col.max }; 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.max = (uint)(overlappingRange[0] - 1); CT_Col rangeCol = insertCol(cols, overlappingRange[0], overlappingRange[1], new CT_Col[] { ithCol, col }); i++; CT_Col newCol = insertCol(cols, (overlappingRange[1] + 1), col .max, new CT_Col[] { col }); i++; } else if (overlappingType == NumericRanges.OVERLAPS_2_MINOR) { ithCol.min = (uint)(overlappingRange[1] + 1); CT_Col rangeCol = insertCol(cols, overlappingRange[0], overlappingRange[1], new CT_Col[] { ithCol, col }); i++; CT_Col newCol = insertCol(cols, col.min, (overlappingRange[0] - 1), new CT_Col[] { col }); i++; } else if (overlappingType == NumericRanges.OVERLAPS_2_WRAPS) { SetColumnAttributes(col, ithCol); if (col.min != ithCol.min) { CT_Col newColBefore = insertCol(cols, col.min, (ithCol .min - 1), new CT_Col[] { col }); i++; } if (col.max != ithCol.max) { CT_Col newColAfter = insertCol(cols, (ithCol.max + 1), col.max, new CT_Col[] { col }); i++; } } else if (overlappingType == NumericRanges.OVERLAPS_1_WRAPS) { if (col.min != ithCol.min) { CT_Col newColBefore = insertCol(cols, ithCol.min, (col .min - 1), new CT_Col[] { ithCol }); i++; } if (col.max != ithCol.max) { CT_Col newColAfter = insertCol(cols, (col.max + 1), ithCol.max, new CT_Col[] { ithCol }); i++; } ithCol.min = (uint)(overlappingRange[0]); ithCol.max = (uint)(overlappingRange[1]); SetColumnAttributes(col, ithCol); } if (overlappingType != NumericRanges.NO_OVERLAPS) { colOverlaps = true; } } if (!colOverlaps) { CT_Col newCol = CloneCol(cols, col); } SortColumns(cols); return(cols); }
//YK: GetXYZArray() array accessors are deprecated in xmlbeans with JDK 1.5 support public static void SortColumns(CT_Cols newCols) { List<CT_Col> colArray = newCols.GetColList(); colArray.Sort(new CTColComparator()); newCols.SetColArray(colArray); }
public CT_Cols AddCleanColIntoCols(CT_Cols cols, CT_Col col) { bool flag = false; for (int index = 0; index < cols.sizeOfColArray(); ++index) { CT_Col colArray = cols.GetColArray(index); long[] range1 = new long[2] { (long)colArray.min, (long)colArray.max }; long[] range2 = new long[2] { (long)col.min, (long)col.max }; long[] overlappingRange = NumericRanges.GetOverlappingRange(range1, range2); int overlappingType = NumericRanges.GetOverlappingType(range1, range2); if (overlappingType == 0) { colArray.max = (uint)((ulong)overlappingRange[0] - 1UL); this.insertCol(cols, overlappingRange[0], overlappingRange[1], new CT_Col[2] { colArray, col }); int num = index + 1; this.insertCol(cols, overlappingRange[1] + 1L, (long)col.max, new CT_Col[1] { col }); index = num + 1; } else if (overlappingType == 1) { colArray.min = (uint)((ulong)overlappingRange[1] + 1UL); this.insertCol(cols, overlappingRange[0], overlappingRange[1], new CT_Col[2] { colArray, col }); int num = index + 1; this.insertCol(cols, (long)col.min, overlappingRange[0] - 1L, new CT_Col[1] { col }); index = num + 1; } else if (overlappingType == 3) { this.SetColumnAttributes(col, colArray); if ((int)col.min != (int)colArray.min) { this.insertCol(cols, (long)col.min, (long)(colArray.min - 1U), new CT_Col[1] { col }); ++index; } if ((int)col.max != (int)colArray.max) { this.insertCol(cols, (long)(colArray.max + 1U), (long)col.max, new CT_Col[1] { col }); ++index; } } else if (overlappingType == 2) { if ((int)col.min != (int)colArray.min) { this.insertCol(cols, (long)colArray.min, (long)(col.min - 1U), new CT_Col[1] { colArray }); ++index; } if ((int)col.max != (int)colArray.max) { this.insertCol(cols, (long)(col.max + 1U), (long)colArray.max, new CT_Col[1] { colArray }); ++index; } colArray.min = (uint)overlappingRange[0]; colArray.max = (uint)overlappingRange[1]; this.SetColumnAttributes(col, colArray); } if (overlappingType != -1) { flag = true; } } if (!flag) { this.CloneCol(cols, col); } ColumnHelper.SortColumns(cols); return(cols); }