Skip to content

Commit b157401

Browse files
authored
Fixed bug: CF rule priority after read + add new (#2321)
1 parent 6be995c commit b157401

2 files changed

Lines changed: 91 additions & 3 deletions

File tree

src/EPPlus/ConditionalFormatting/ExcelConditionalFormattingCollection.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public class ExcelConditionalFormattingCollection : IEnumerable<IExcelConditiona
3434
{
3535
List<ExcelConditionalFormattingRule> _rules = new List<ExcelConditionalFormattingRule>();
3636
ExcelWorksheet _ws;
37-
int LastPriority = 1;
37+
int _nextPriority = 1;
3838
//A dict for those conditionalFormattings that are Ext, have been read in locally but not yet read in their ExtLst parts.
3939
internal Dictionary<string, ExcelConditionalFormattingRule> localAndExtDict = new Dictionary<string, ExcelConditionalFormattingRule>();
4040
internal QuadTree<IExcelConditionalFormattingRule> CfIndex { get; set; }
@@ -98,11 +98,13 @@ internal void ReadRegularConditionalFormattings(XmlReader xr)
9898
{
9999
AddToList(cf);
100100
}
101-
}
101+
_nextPriority = cf.Priority > _nextPriority ? cf.Priority : _nextPriority;
102+
}
102103
while ((xr.LocalName == "conditionalFormatting" || xr.LocalName == "cfRule") && xr.NodeType == XmlNodeType.EndElement) xr.Read();
103104
}
104105
while (xr.LocalName == "conditionalFormatting" || xr.LocalName == "cfRule");
105106
}
107+
_nextPriority += 1;
106108
}
107109

108110
/// <summary>
@@ -681,7 +683,7 @@ internal IExcelConditionalFormattingRule AddRule(
681683
var cfRule = ExcelConditionalFormattingRuleFactory.Create(
682684
type,
683685
address,
684-
LastPriority++,
686+
_nextPriority++,
685687
_ws);
686688

687689
// Add the newly created rule to the list

src/EPPlusTest/Issues/ConditionalFormattingIssues.cs

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
using Microsoft.VisualStudio.TestTools.UnitTesting;
22
using OfficeOpenXml;
33
using OfficeOpenXml.ConditionalFormatting;
4+
using OfficeOpenXml.ConditionalFormatting.Contracts;
45
using OfficeOpenXml.Style;
6+
using System.Drawing;
57
using System.Globalization;
68
using System.Threading;
79

@@ -75,5 +77,89 @@ public void Test1_Input_ExpectedOutput()
7577
SaveAndCleanup(package);
7678
Thread.CurrentThread.CurrentCulture = currentCulture;
7779
}
80+
81+
[TestMethod]
82+
public void s1025()
83+
{
84+
using (var package = OpenTemplatePackage("s1025.xlsx"))
85+
{
86+
ExcelWorkbook wb = package.Workbook;
87+
ExcelWorksheet ws = wb.Worksheets[0];
88+
89+
IExcelConditionalFormattingBetween condGreen = ws.ConditionalFormatting.AddBetween(ws.Cells[5, 2, 25, 2]);
90+
condGreen.Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
91+
condGreen.Style.Fill.BackgroundColor.Color = ColorTranslator.FromHtml("#A9D08E");
92+
condGreen.Formula = ws.Cells[6, 7].FullAddressAbsolute.ToString();
93+
condGreen.Formula2 = ws.Cells[6, 8].FullAddressAbsolute.ToString();
94+
95+
IExcelConditionalFormattingBetween condYellow = ws.ConditionalFormatting.AddBetween(ws.Cells[5, 2, 25, 2]);
96+
condYellow.Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
97+
condYellow.Style.Fill.BackgroundColor.Color = ColorTranslator.FromHtml("#FFE699");
98+
condYellow.Formula = ws.Cells[7, 7].FullAddressAbsolute.ToString();
99+
condYellow.Formula2 = ws.Cells[7, 8].FullAddressAbsolute.ToString();
100+
101+
IExcelConditionalFormattingGreaterThan condRed = ws.ConditionalFormatting.AddGreaterThan(ws.Cells[5, 2, 25, 2]);
102+
condRed.Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
103+
condRed.Style.Fill.BackgroundColor.Color = ColorTranslator.FromHtml("#FF7979");
104+
condRed.Formula = ws.Cells[9, 7].FullAddressAbsolute.ToString();
105+
106+
Assert.AreEqual(6, condRed.Priority);
107+
108+
SaveAndCleanup(package);
109+
}
110+
}
111+
112+
[TestMethod]
113+
public void VerifyReadWritePriority()
114+
{
115+
using (var p = OpenPackage("CFReadWritePriority.xlsx", true))
116+
{
117+
var ws = p.Workbook.Worksheets.Add("CFReadWritePriorityWs");
118+
119+
ws.Cells["A1:C10"].Formula = "ROW()+COLUMN()";
120+
121+
ws.Calculate();
122+
123+
var cf1 = ws.Cells["A1:A10"].ConditionalFormatting.AddAboveAverage();
124+
var cf2 = ws.Cells["A1:A10"].ConditionalFormatting.AddAboveAverage();
125+
126+
cf1.Style.Fill.BackgroundColor.SetColor(Color.RosyBrown);
127+
128+
cf2.Style.Fill.BackgroundColor.SetColor(Color.DarkGreen);
129+
130+
var cfB1 = ws.Cells["B1:B10"].ConditionalFormatting.AddBetween();
131+
cfB1.Formula = "4";
132+
cfB1.Formula2 = "14";
133+
134+
var cfB2 = ws.Cells["B1:B10"].ConditionalFormatting.AddBetween();
135+
136+
cfB2.Formula = "14";
137+
cfB2.Formula2 = "35";
138+
139+
cfB1.Style.Fill.BackgroundColor.SetColor(Color.BlueViolet);
140+
cfB2.Style.Fill.BackgroundColor.SetColor(Color.Chartreuse);
141+
142+
cfB1.Priority = 1;
143+
144+
SaveAndCleanup(p);
145+
}
146+
147+
using (var p = OpenPackage("CFReadWritePriority.xlsx", false))
148+
{
149+
var ws = p.Workbook.Worksheets[0];
150+
151+
var cfText = ws.Cells["C3:C5"].ConditionalFormatting.AddContainsText();
152+
153+
cfText.Formula = "";
154+
cfText.Style.Fill.BackgroundColor.Color = Color.Red;
155+
156+
Assert.AreEqual(5, cfText.Priority);
157+
158+
var outFile = GetOutputFile("", "CFSamePriorityResave.xlsx");
159+
160+
p.SaveAs(outFile);
161+
//SaveAndCleanup(p);
162+
}
163+
}
78164
}
79165
}

0 commit comments

Comments
 (0)