Exemple #1
0
        public void ContainsRow()
        {
            CellRangeAddress region = new CellRangeAddress(10, 12, 3, 5);

            Assert.IsFalse(region.ContainsRow(9));
            Assert.IsTrue(region.ContainsRow(10));
            Assert.IsTrue(region.ContainsRow(11));
            Assert.IsTrue(region.ContainsRow(12));
            Assert.IsFalse(region.ContainsRow(13));
        }
Exemple #2
0
        /**
         * Shifts, grows, or shrinks the merged regions due to a row Shift.
         * Merged regions that are completely overlaid by Shifting will be deleted.
         *
         * @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 merged regions, doesn't contain deleted ones
         */
        public List <CellRangeAddress> ShiftMergedRegions(int startRow, int endRow, int n)
        {
            List <CellRangeAddress> ShiftedRegions = new List <CellRangeAddress>();
            ISet <int> removedIndices = new 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);

                // remove merged region that overlaps Shifting
                var lastCol = sheet.GetRow(startRow) != null?sheet.GetRow(startRow).LastCellNum : sheet.GetRow(endRow) != null?sheet.GetRow(endRow).LastCellNum : 0;

                if (removalNeeded(merged, startRow, endRow, n, lastCol))
                {
                    removedIndices.Add(i);
                    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 (!merged.ContainsRow(startRow - 1) && !merged.ContainsRow(endRow + 1))
                {
                    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.ToList());
            }

            //read so it doesn't Get Shifted again
            foreach (CellRangeAddress region in ShiftedRegions)
            {
                sheet.AddMergedRegion(region);
            }
            return(ShiftedRegions);
        }