Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

don't brake meged cells when shifting rows #836

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 28 additions & 32 deletions main/SS/UserModel/Helpers/RowShifter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,52 +48,48 @@ public RowShifter(ISheet sh)
*/
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
if (startRow + n <= merged.FirstRow && endRow + n >= merged.LastRow)
{
removedIndices.Add(i);
continue;
}

bool inStart = (merged.FirstRow >= startRow || merged.LastRow >= startRow);
bool inEnd = (merged.FirstRow <= endRow || merged.LastRow <= endRow);
var rangesToShift = new List<CellRangeAddress>();
var rangesToExpand = new List<CellRangeAddress>();
var size = sheet.NumMergedRegions;

//don't check if it's not within the Shifted area
if (!inStart || !inEnd)
for (var i = 0; i < size; i++)
{
var merged = sheet.GetMergedRegion(i);
if (MergedInsideMovingRange(startRow, endRow, n, merged))
{
rangesToShift.Add(merged);
continue;
}

//only shift if the region outside the Shifted rows is not merged too
if (!merged.ContainsRow(startRow - 1) && !merged.ContainsRow(endRow + 1))
if (MovingRangeCrossesMerged(startRow, endRow, n, merged))
{
merged.FirstRow = (/*setter*/merged.FirstRow + n);
merged.LastRow = (/*setter*/merged.LastRow + n);
//have to Remove/add it back
ShiftedRegions.Add(merged);
removedIndices.Add(i);
rangesToExpand.Add(merged);
}
}

if (!(removedIndices.Count==0)/*.IsEmpty()*/)
foreach (var range in rangesToShift)
{
sheet.RemoveMergedRegions(removedIndices.ToList());
range.FirstRow = range.FirstRow + n;
range.LastRow = range.LastRow + n;
}

//read so it doesn't Get Shifted again
foreach (CellRangeAddress region in ShiftedRegions)
foreach (var range in rangesToExpand)
{
sheet.AddMergedRegion(region);
range.LastRow = range.LastRow + n;

}
return ShiftedRegions;

return rangesToShift;
}

private bool MergedInsideMovingRange(int startRow, int endRow, int n, CellRangeAddress merged)
{
return startRow <= merged.FirstRow && endRow >= merged.LastRow;
}

private bool MovingRangeCrossesMerged(int startRow, int endRow, int n, CellRangeAddress merged)
{
return merged.FirstRow <= startRow && startRow <= merged.LastRow;
}

/**
Expand Down