Skip to content
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class ExcelConditionalFormattingCollection : IEnumerable<IExcelConditiona
{
List<ExcelConditionalFormattingRule> _rules = new List<ExcelConditionalFormattingRule>();
ExcelWorksheet _ws;
int LastPriority = 1;
int _nextPriority = 1;
//A dict for those conditionalFormattings that are Ext, have been read in locally but not yet read in their ExtLst parts.
internal Dictionary<string, ExcelConditionalFormattingRule> localAndExtDict = new Dictionary<string, ExcelConditionalFormattingRule>();
internal QuadTree<IExcelConditionalFormattingRule> CfIndex { get; set; }
Expand Down Expand Up @@ -98,11 +98,13 @@ internal void ReadRegularConditionalFormattings(XmlReader xr)
{
AddToList(cf);
}
}
_nextPriority = cf.Priority > _nextPriority ? cf.Priority : _nextPriority;
}
while ((xr.LocalName == "conditionalFormatting" || xr.LocalName == "cfRule") && xr.NodeType == XmlNodeType.EndElement) xr.Read();
}
while (xr.LocalName == "conditionalFormatting" || xr.LocalName == "cfRule");
}
_nextPriority += 1;
}

/// <summary>
Expand Down Expand Up @@ -681,7 +683,7 @@ internal IExcelConditionalFormattingRule AddRule(
var cfRule = ExcelConditionalFormattingRuleFactory.Create(
type,
address,
LastPriority++,
_nextPriority++,
_ws);

// Add the newly created rule to the list
Expand Down
86 changes: 86 additions & 0 deletions src/EPPlusTest/Issues/ConditionalFormattingIssues.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OfficeOpenXml;
using OfficeOpenXml.ConditionalFormatting;
using OfficeOpenXml.ConditionalFormatting.Contracts;
using OfficeOpenXml.Style;
using System.Drawing;
using System.Globalization;
using System.Threading;

Expand Down Expand Up @@ -75,5 +77,89 @@ public void Test1_Input_ExpectedOutput()
SaveAndCleanup(package);
Thread.CurrentThread.CurrentCulture = currentCulture;
}

[TestMethod]
public void s1025()
{
using (var package = OpenTemplatePackage("s1025.xlsx"))
{
ExcelWorkbook wb = package.Workbook;
ExcelWorksheet ws = wb.Worksheets[0];

IExcelConditionalFormattingBetween condGreen = ws.ConditionalFormatting.AddBetween(ws.Cells[5, 2, 25, 2]);
condGreen.Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
condGreen.Style.Fill.BackgroundColor.Color = ColorTranslator.FromHtml("#A9D08E");
condGreen.Formula = ws.Cells[6, 7].FullAddressAbsolute.ToString();
condGreen.Formula2 = ws.Cells[6, 8].FullAddressAbsolute.ToString();

IExcelConditionalFormattingBetween condYellow = ws.ConditionalFormatting.AddBetween(ws.Cells[5, 2, 25, 2]);
condYellow.Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
condYellow.Style.Fill.BackgroundColor.Color = ColorTranslator.FromHtml("#FFE699");
condYellow.Formula = ws.Cells[7, 7].FullAddressAbsolute.ToString();
condYellow.Formula2 = ws.Cells[7, 8].FullAddressAbsolute.ToString();

IExcelConditionalFormattingGreaterThan condRed = ws.ConditionalFormatting.AddGreaterThan(ws.Cells[5, 2, 25, 2]);
condRed.Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
condRed.Style.Fill.BackgroundColor.Color = ColorTranslator.FromHtml("#FF7979");
condRed.Formula = ws.Cells[9, 7].FullAddressAbsolute.ToString();

Assert.AreEqual(6, condRed.Priority);

SaveAndCleanup(package);
}
}

[TestMethod]
public void VerifyReadWritePriority()
{
using (var p = OpenPackage("CFReadWritePriority.xlsx", true))
{
var ws = p.Workbook.Worksheets.Add("CFReadWritePriorityWs");

ws.Cells["A1:C10"].Formula = "ROW()+COLUMN()";

ws.Calculate();

var cf1 = ws.Cells["A1:A10"].ConditionalFormatting.AddAboveAverage();
var cf2 = ws.Cells["A1:A10"].ConditionalFormatting.AddAboveAverage();

cf1.Style.Fill.BackgroundColor.SetColor(Color.RosyBrown);

cf2.Style.Fill.BackgroundColor.SetColor(Color.DarkGreen);

var cfB1 = ws.Cells["B1:B10"].ConditionalFormatting.AddBetween();
cfB1.Formula = "4";
cfB1.Formula2 = "14";

var cfB2 = ws.Cells["B1:B10"].ConditionalFormatting.AddBetween();

cfB2.Formula = "14";
cfB2.Formula2 = "35";

cfB1.Style.Fill.BackgroundColor.SetColor(Color.BlueViolet);
cfB2.Style.Fill.BackgroundColor.SetColor(Color.Chartreuse);

cfB1.Priority = 1;

SaveAndCleanup(p);
}

using (var p = OpenPackage("CFReadWritePriority.xlsx", false))
{
var ws = p.Workbook.Worksheets[0];

var cfText = ws.Cells["C3:C5"].ConditionalFormatting.AddContainsText();

cfText.Formula = "";
cfText.Style.Fill.BackgroundColor.Color = Color.Red;

Assert.AreEqual(5, cfText.Priority);

var outFile = GetOutputFile("", "CFSamePriorityResave.xlsx");

p.SaveAs(outFile);
//SaveAndCleanup(p);
}
}
}
}