示例#1
0
        /**
         * Shift merged regions
         *
         * @param startRow the row to start Shifting
         * @param endRow   the row to end Shifting
         * @param n        the number of rows to shift
         * @return an array of affected cell regions
         */
        public List <CellRangeAddress> ShiftMerged(int startRow, int endRow, int n)
        {
            List <CellRangeAddress> ShiftedRegions = new List <CellRangeAddress>();

            NPOI.Util.Collections.HashSet <int> removedIndices = new NPOI.Util.Collections.HashSet <int>();
            //move merged regions completely if they fall within the new region boundaries when they are Shifted
            int size = sheet.NumMergedRegions;

            for (int i = 0; i < size; i++)
            {
                CellRangeAddress merged = sheet.GetMergedRegion(i);

                if (merged == null)
                {
                    continue;
                }

                bool inStart = (merged.FirstRow >= startRow || merged.LastRow >= startRow);
                bool inEnd   = (merged.FirstRow <= endRow || merged.LastRow <= endRow);

                //don't check if it's not within the Shifted area
                if (!inStart || !inEnd)
                {
                    continue;
                }

                //only shift if the region outside the Shifted rows is not merged too
                if (!ContainsCell(merged, startRow - 1, 0) && !ContainsCell(merged, endRow + 1, 0))
                {
                    merged.FirstRow = (merged.FirstRow + n);
                    merged.LastRow  = (merged.LastRow + n);
                    //have to Remove/add it back
                    ShiftedRegions.Add(merged);
                    removedIndices.Add(i);
                }
            }
            if (removedIndices.Count > 0)
            {
                sheet.RemoveMergedRegions(removedIndices);
            }
            //read so it doesn't get Shifted again
            foreach (CellRangeAddress region in ShiftedRegions)
            {
                sheet.AddMergedRegion(region);
            }
            return(ShiftedRegions);
        }
示例#2
0
        /**
         * Shift merged regions
         *
         * @param startRow the row to start Shifting
         * @param endRow   the row to end Shifting
         * @param n        the number of rows to shift
         * @return an array of affected cell regions
         */
        public List<CellRangeAddress> ShiftMerged(int startRow, int endRow, int n)
        {
            List<CellRangeAddress> ShiftedRegions = new List<CellRangeAddress>();
            NPOI.Util.Collections.HashSet<int> removedIndices = new NPOI.Util.Collections.HashSet<int>();
            //move merged regions completely if they fall within the new region boundaries when they are Shifted
            int size = sheet.NumMergedRegions;
            for (int i = 0; i < size; i++) 
            {
                CellRangeAddress merged = sheet.GetMergedRegion(i);

                if (merged == null) { continue; }

                bool inStart = (merged.FirstRow >= startRow || merged.LastRow >= startRow);
                bool inEnd = (merged.FirstRow <= endRow || merged.LastRow <= endRow);

                //don't check if it's not within the Shifted area
                if (!inStart || !inEnd)
                {
                    continue;
                }

                //only shift if the region outside the Shifted rows is not merged too
                if (!ContainsCell(merged, startRow - 1, 0) && !ContainsCell(merged, endRow + 1, 0))
                {
                    merged.FirstRow = (merged.FirstRow + n);
                    merged.LastRow = (merged.LastRow + n);
                    //have to Remove/add it back
                    ShiftedRegions.Add(merged);
                    removedIndices.Add(i);
                }
            }
            if (removedIndices.Count>0)
            {
                sheet.RemoveMergedRegions(removedIndices);
            }
            //read so it doesn't get Shifted again
            foreach (CellRangeAddress region in ShiftedRegions)
            {
                sheet.AddMergedRegion(region);
            }
            return ShiftedRegions;
        }
示例#3
0
        /**
         * @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);
        }