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
add merge-tag
  • Loading branch information
eynarhaji committed Aug 7, 2023
commit 578feb48e880aa6315fac526d649f94480359522
Binary file removed samples/xlsx/3B60BF00
Binary file not shown.
98 changes: 54 additions & 44 deletions src/MiniExcel/OpenXml/ExcelOpenXmlTemplate.Impl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -200,14 +200,17 @@ private void WriteSheetXml(Stream stream, XmlDocument doc, XmlNode sheetData, bo
RowIndex = StringHelper.GetNumber(att)
};
}).ToList();

var mergeColumns = columns.Where(s => s.InnerText.Contains("@merge")).OrderBy(s => s.RowIndex).ToList();
var endMergeColumns = columns.Where(s => s.InnerText.Contains("@endmerge")).OrderBy(s => s.RowIndex).ToList();


var mergeColumns = columns.Where(s => s.InnerText.Contains("@merge")).OrderBy(s => s.RowIndex)
.ToList();
var endMergeColumns = columns.Where(s => s.InnerText.Contains("@endmerge")).OrderBy(s => s.RowIndex)
.ToList();

foreach (var mergeColumn in mergeColumns)
{
var endMergeColumn = endMergeColumns.FirstOrDefault(s => s.ColIndex == mergeColumn.ColIndex && s.RowIndex > mergeColumn.RowIndex);

var endMergeColumn = endMergeColumns.FirstOrDefault(s =>
s.ColIndex == mergeColumn.ColIndex && s.RowIndex > mergeColumn.RowIndex);

if (endMergeColumn != null)
{
mergeTaggedColumns[mergeColumn] = endMergeColumn;
Expand All @@ -220,57 +223,64 @@ private void WriteSheetXml(Stream stream, XmlDocument doc, XmlNode sheetData, bo
{
foreach (var taggedColumn in mergeTaggedColumns)
{
calculatedColumns.AddRange(columns.Where(x=>x.ColIndex == taggedColumn.Key.ColIndex && x.RowIndex > taggedColumn.Key.RowIndex && x.RowIndex < taggedColumn.Value.RowIndex));
calculatedColumns.AddRange(columns.Where(x =>
x.ColIndex == taggedColumn.Key.ColIndex && x.RowIndex > taggedColumn.Key.RowIndex &&
x.RowIndex < taggedColumn.Value.RowIndex));
}
}
else
{
calculatedColumns = columns;
}

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<XmlElement>()
.Where(s => !string.IsNullOrEmpty(s.InnerText)).ToList();
Dictionary<int, MergeCellIndex>
lastMergeCellIndexes = new Dictionary<int, MergeCellIndex>();

foreach (var childNode in childNodes)
for (int rowNo = 0; rowNo < XRowInfos.Count; rowNo++)
{
var childNodeAtt = StringHelper.GetLetter(childNode.GetAttribute("r"));

var xmlNodes = calculatedColumns
.Where(j => j.InnerText == childNode.InnerText && j.ColIndex == childNodeAtt)
.OrderBy(s => s.RowIndex).ToList();
var rowInfo = XRowInfos[rowNo];
var row = rowInfo.Row;
var childNodes = row.ChildNodes.Cast<XmlElement>().ToList();

if (xmlNodes.Count > 1)
foreach (var childNode in childNodes)
{
var firstRow = xmlNodes.FirstOrDefault();
var lastRow = xmlNodes.LastOrDefault(s => s.RowIndex <= firstRow?.RowIndex + xmlNodes.Count && s.RowIndex != firstRow?.RowIndex);
var childNodeAtt = StringHelper.GetLetter(childNode.GetAttribute("r"));

if (firstRow != null && lastRow != null)
if(!string.IsNullOrEmpty(childNode.InnerText))
{
var mergeCell = new XMergeCell(firstRow.ColIndex, firstRow.RowIndex, lastRow.ColIndex, lastRow.RowIndex);
var xmlNodes = calculatedColumns
.Where(j => j.InnerText == childNode.InnerText && j.ColIndex == childNodeAtt)
.OrderBy(s => s.RowIndex).ToList();

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

if (mergeIndexResult && mergeCell.Y1 >= mergeIndex.RowStart &&
mergeCell.Y2 <= mergeIndex.RowEnd)
if (xmlNodes.Count > 1)
{
continue;
}
var firstRow = xmlNodes.FirstOrDefault();
var lastRow = xmlNodes.LastOrDefault(s =>
s.RowIndex <= firstRow?.RowIndex + xmlNodes.Count &&
s.RowIndex != firstRow?.RowIndex);

lastMergeCellIndexes[mergeCell.X1] = new MergeCellIndex(mergeCell.Y1, mergeCell.Y2);
if (firstRow != null && lastRow != null)
{
var mergeCell = new XMergeCell(firstRow.ColIndex, firstRow.RowIndex,
lastRow.ColIndex, lastRow.RowIndex);

if (rowInfo.RowMercells == null)
{
rowInfo.RowMercells = new List<XMergeCell>();
}
var mergeIndexResult =
lastMergeCellIndexes.TryGetValue(mergeCell.X1, out var mergeIndex);

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

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

rowInfo.RowMercells.Add(mergeCell);
rowInfo.RowMercells.Add(mergeCell);
}
}
}
}

childNode.SetAttribute("r", $"{childNodeAtt}{{{{$rowindex}}}}"); //TODO:
}
}
}
Expand Down Expand Up @@ -656,11 +666,11 @@ private void WriteSheetXml(Stream stream, XmlDocument doc, XmlNode sheetData, bo
// note: only first time need add diff ![image](https://user-images.githubusercontent.com/12729184/114494728-6bceda80-9c4f-11eb-9685-8b5ed054eabe.png)
if (!first)
//rowIndexDiff++;
rowIndexDiff = rowIndexDiff + (rowInfo.IEnumerableMercell == null ? 1 : rowInfo.IEnumerableMercell.Height); //TODO:base on the merge size
rowIndexDiff += (rowInfo.IEnumerableMercell?.Height ?? 1); //TODO:base on the merge size
first = false;

var mergeBaseRowIndex = newRowIndex;
newRowIndex = newRowIndex + (rowInfo.IEnumerableMercell == null ? 1 : rowInfo.IEnumerableMercell.Height);
newRowIndex += rowInfo.IEnumerableMercell?.Height ?? 1;
writer.Write(CleanXml(rowXml, endPrefix)); // pass StringBuilder for netcoreapp3.0 or above

//mergecells
Expand Down
29 changes: 2 additions & 27 deletions tests/MiniExcelTests/MiniExcelTemplateTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -841,27 +841,6 @@ public void TemplateTest()

}

[Fact]
public void MergeSameCellsTest()
{
var mergedFilePath = Path.Combine(Path.GetTempPath(), $"{Guid.NewGuid().ToString()}.xlsx");

var path = @"../../../../../samples/xlsx/TestMergeSameCells.xlsx";

MiniExcel.MergeSameCells(mergedFilePath, path);
{
var mergedCells = Helpers.GetFirstSheetMergedCells(mergedFilePath);

Assert.Equal("A2:A4", mergedCells[0]);
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]);
}
}

[Fact]
public void MergeSameCellsWithTagTest()
{
Expand All @@ -874,12 +853,8 @@ public void MergeSameCellsWithTagTest()
var mergedCells = Helpers.GetFirstSheetMergedCells(mergedFilePath);

Assert.Equal("A2:A4", mergedCells[0]);
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]);
Assert.Equal("C3:C4", mergedCells[1]);
Assert.Equal("A7:A8", mergedCells[2]);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/MiniExcelTests/MiniExcelTests.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net5.0</TargetFrameworks>
<TargetFrameworks>net6.0;</TargetFrameworks>

<IsPackable>false</IsPackable>
<AssemblyOriginatorKeyFile>miniexcel.snk</AssemblyOriginatorKeyFile>
Expand Down