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

Add support for merge limit #521

Merged
merged 29 commits into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
db5e2b9
add support for grouped cells
eynarhaji Feb 13, 2023
aae315c
fix for rows after endgroup
eynarhaji Feb 13, 2023
8b23c5b
minor fix for text after grouping
eynarhaji Feb 14, 2023
0b31fa5
fix unit tests
eynarhaji Feb 16, 2023
f4f950b
fix unit tests and docs
eynarhaji Feb 19, 2023
614a11c
fix unit tests and docs
eynarhaji Feb 19, 2023
eeb7966
minor fix
eynarhaji Feb 19, 2023
8372221
Add support to vertical merge cells
eynarhaji Feb 19, 2023
5f2df40
Merge branch 'master' into main
eynarhaji Feb 20, 2023
7fcd698
Merge branch 'master' into main
eynarhaji Feb 21, 2023
4332026
minor fixes in merge cells
eynarhaji Feb 21, 2023
1cc7dc8
minor fix
eynarhaji Feb 21, 2023
30ffde3
fix complex scenario
eynarhaji Feb 21, 2023
761f614
finalize changes
eynarhaji Feb 21, 2023
f301af4
Merge branch 'mini-software:master' into master
eynarhaji Apr 30, 2023
9734dc5
Add support for if/elseif/else statements
eynarhaji Apr 30, 2023
198fca3
Merge branch 'mini-master'
eynarhaji Aug 6, 2023
37496c7
add merge tag
eynarhaji Aug 6, 2023
578feb4
add merge-tag
eynarhaji Aug 7, 2023
0910f30
update readme
eynarhaji Aug 7, 2023
d1f1022
Merge branch 'feature/merge-tag'
eynarhaji Aug 7, 2023
466bd01
minor fix
eynarhaji Aug 7, 2023
6e52528
minor fix
eynarhaji Aug 7, 2023
e096069
minor fix
eynarhaji Aug 7, 2023
89aeadc
Update MiniExcelLibs.csproj
shps951023 Aug 7, 2023
c1154ee
Merge branch 'mini-master'
eynarhaji Aug 8, 2023
2308fe0
Add support for merge limit
eynarhaji Aug 8, 2023
1c2a851
update test cases
eynarhaji Aug 8, 2023
c0912ea
rollback
eynarhaji Aug 8, 2023
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
Prev Previous commit
Next Next commit
fix complex scenario
  • Loading branch information
eynarhaji committed Feb 21, 2023
commit 30ffde34cb88d50c448c6664e02661446cbc5ff2
Binary file modified samples/xlsx/TestMergeSameCells.xlsx
Binary file not shown.
86 changes: 55 additions & 31 deletions src/MiniExcel/OpenXml/ExcelOpenXmlTemplate.Impl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,16 @@ public XMergeCell(XmlElement mergeCell)
X2 = ColumnHelper.GetColumnIndex(StringHelper.GetLetter(refs[1]));
Y2 = StringHelper.GetNumber(xy2);

Width = Math.Abs(X1 - X2) + 1; ;
Width = Math.Abs(X1 - X2) + 1;
Height = Math.Abs(Y1 - Y2) + 1;
}
public XMergeCell(string xy1, string xy2)
public XMergeCell(string x1, int y1, string x2, int y2)
{
X1 = ColumnHelper.GetColumnIndex(StringHelper.GetLetter(xy1));
Y1 = StringHelper.GetNumber(xy1);
X1 = ColumnHelper.GetColumnIndex(x1);
Y1 = y1;

X2 = ColumnHelper.GetColumnIndex(StringHelper.GetLetter(xy2));
Y2 = StringHelper.GetNumber(xy2);
X2 = ColumnHelper.GetColumnIndex(x2);
Y2 = y2;

Width = Math.Abs(X1 - X2) + 1;
Height = Math.Abs(Y1 - Y2) + 1;
Expand Down Expand Up @@ -151,6 +151,13 @@ public MergeCellIndex(int rowStart, int rowEnd)
RowEnd = rowEnd;
}
}

private class XChildNode
{
public string InnerText { get; set; }
public string ColIndex { get; set; }
public int RowIndex { get; set; }
}

private void WriteSheetXml(Stream stream, XmlDocument doc, XmlNode sheetData, bool mergeCells = false)
{
Expand All @@ -170,43 +177,60 @@ private void WriteSheetXml(Stream stream, XmlDocument doc, XmlNode sheetData, bo

if(mergeCells)
{
var rows = XRowInfos.SelectMany(s => s.Row.Cast<XmlElement>()).ToList();
var columns = XRowInfos.SelectMany(s => s.Row.Cast<XmlElement>()).Select(s=>
{
var att = s.GetAttribute("r");
return new XChildNode()
{
InnerText = s.InnerText,
ColIndex = StringHelper.GetLetter(att),
RowIndex = StringHelper.GetNumber(att)
};
}).ToList();

Dictionary<int, MergeCellIndex> lastMergeCellIndexes = new Dictionary<int, MergeCellIndex>();

for (int rowNo = 0; rowNo < XRowInfos.Count; rowNo++)
{
var rowInfo = XRowInfos[rowNo];
var row = rowInfo.Row;
var childNodes = row.ChildNodes.Cast<XmlNode>().ToList();

foreach (var mergeCell in from cNode in childNodes
select rows.Where(j =>
j.ChildNodes.Cast<XmlNode>().Any(m => m.InnerText == cNode.InnerText)).ToList()
into xmlNodes
where xmlNodes.Count > 1
let firstRow = xmlNodes.FirstOrDefault()
let lastRow = xmlNodes.LastOrDefault()
where firstRow != null && lastRow != null
let firstRowRAtt = firstRow.GetAttribute("r")
let lastRowRAtt = lastRow.GetAttribute("r")
select new XMergeCell(firstRowRAtt, lastRowRAtt))
var childNodes = row.ChildNodes.Cast<XmlElement>().ToList();

foreach (var childNode in childNodes)
{
var mergeIndexResult = lastMergeCellIndexes.TryGetValue(mergeCell.X1, out var mergeIndex);
var childNodeAtt = StringHelper.GetLetter(childNode.GetAttribute("r"));

if (mergeIndexResult && mergeCell.Y1 >= mergeIndex.RowStart &&
mergeCell.Y2 <= mergeIndex.RowEnd)
var xmlNodes = columns
.Where(j => j.InnerText == childNode.InnerText && j.ColIndex == childNodeAtt)
.OrderBy(s => s.RowIndex).ToList();

if (xmlNodes.Count > 1)
{
continue;
}
var firstRow = xmlNodes.FirstOrDefault();
var lastRow = xmlNodes.LastOrDefault(s => s.RowIndex <= firstRow?.RowIndex + xmlNodes.Count && s.RowIndex != firstRow?.RowIndex);

if (firstRow != null && lastRow != null)
{
var mergeCell = new XMergeCell(firstRow.ColIndex, firstRow.RowIndex, lastRow.ColIndex, lastRow.RowIndex);

var mergeIndexResult = lastMergeCellIndexes.TryGetValue(mergeCell.X1, out var mergeIndex);

lastMergeCellIndexes[mergeCell.X1] = new MergeCellIndex(mergeCell.Y1, mergeCell.Y2);
if (mergeIndexResult && mergeCell.Y1 >= mergeIndex.RowStart &&
mergeCell.Y2 <= mergeIndex.RowEnd)
{
continue;
}

if (rowInfo.RowMercells == null)
{
rowInfo.RowMercells = new List<XMergeCell>();
}
lastMergeCellIndexes[mergeCell.X1] = new MergeCellIndex(mergeCell.Y1, mergeCell.Y2);

rowInfo.RowMercells.Add(mergeCell);
if (rowInfo.RowMercells == null)
{
rowInfo.RowMercells = new List<XMergeCell>();
}

rowInfo.RowMercells.Add(mergeCell);
}
}
}
}
}
Expand Down
7 changes: 5 additions & 2 deletions tests/MiniExcelTests/MiniExcelTemplateTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -853,9 +853,12 @@ public void MergeSameCellsTest()
var mergedCells = Helpers.GetFirstSheetMergedCells(mergedFilePath);

Assert.Equal("A2:A4", mergedCells[0]);
Assert.Equal("B3:B4", mergedCells[1]);
Assert.Equal("A5:A7", mergedCells[2]);
Assert.Equal("D2:D3", mergedCells[1]);
Assert.Equal("C3:C4", mergedCells[2]);
Assert.Equal("B5:B6", mergedCells[3]);
Assert.Equal("C5:C6", mergedCells[4]);
Assert.Equal("D5:D7", mergedCells[5]);
Assert.Equal("A6:A7", mergedCells[6]);
}
}
}
Expand Down