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

Databar conditional formatting #394

Merged
merged 2 commits into from
Aug 23, 2017
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 19 additions & 7 deletions ClosedXML/Excel/ConditionalFormats/Save/XLCFDataBarConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,32 @@

namespace ClosedXML.Excel
{
internal class XLCFDataBarConverter:IXLCFConverter
internal class XLCFDataBarConverter : IXLCFConverter
{
public ConditionalFormattingRule Convert(IXLConditionalFormat cf, Int32 priority, XLWorkbook.SaveContext context)
{
var conditionalFormattingRule = new ConditionalFormattingRule { Type = cf.ConditionalFormatType.ToOpenXml(), Priority = priority };

var dataBar = new DataBar {ShowValue = !cf.ShowBarOnly};
var conditionalFormatValueObject1 = new ConditionalFormatValueObject { Type = cf.ContentTypes[1].ToOpenXml()};
if (cf.Values.Count >= 1) conditionalFormatValueObject1.Val = cf.Values[1].Value;
var dataBar = new DataBar { ShowValue = !cf.ShowBarOnly };
var conditionalFormatValueObject1 = new ConditionalFormatValueObject { Type = cf.ContentTypes[1].ToOpenXml() };
if (cf.Values.Any() && cf.Values[1]?.Value != null) conditionalFormatValueObject1.Val = cf.Values[1].Value;

var conditionalFormatValueObject2 = new ConditionalFormatValueObject { Type = cf.ContentTypes[2].ToOpenXml()};
if (cf.Values.Count >= 2) conditionalFormatValueObject2.Val = cf.Values[2].Value;
var conditionalFormatValueObject2 = new ConditionalFormatValueObject { Type = cf.ContentTypes[2].ToOpenXml() };
if (cf.Values.Count >= 2 && cf.Values[2]?.Value != null) conditionalFormatValueObject2.Val = cf.Values[2].Value;

var color = new Color { Rgb = cf.Colors[1].Color.ToHex() };
var color = new Color();
switch (cf.Colors[1].ColorType)
{
case XLColorType.Color:
color.Rgb = cf.Colors[1].Color.ToHex();
break;
case XLColorType.Theme:
color.Theme = System.Convert.ToUInt32(cf.Colors[1].ThemeColor);
break;
case XLColorType.Indexed:
color.Indexed = System.Convert.ToUInt32(cf.Colors[1].Indexed);
break;
}

dataBar.Append(conditionalFormatValueObject1);
dataBar.Append(conditionalFormatValueObject2);
Expand Down
4 changes: 4 additions & 0 deletions ClosedXML/Excel/Tables/XLTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,10 @@ private String GetUniqueName(String originalName)

public Int32 GetFieldIndex(String name)
{
// There is a discrepancy in the way headers with line breaks are stored.
// The entry in the table definition will contain \r\n
// but the shared string value of the actual cell will contain only \n
name = name.Replace("\r\n", "\n");
if (FieldNames.ContainsKey(name))
return FieldNames[name].Index;

Expand Down
1 change: 1 addition & 0 deletions ClosedXML_Tests/ClosedXML_Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@
<EmbeddedResource Include="Resource\Misc\ExcelProducedWorkbookWithImages.xlsx" />
<EmbeddedResource Include="Resource\Misc\EmptyCellValue.xlsx" />
<EmbeddedResource Include="Resource\Misc\AllShapes.xlsx" />
<EmbeddedResource Include="Resource\Misc\TableHeadersWithLineBreaks.xlsx" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Resource\Images\ImageHandling.png" />
Expand Down
3 changes: 2 additions & 1 deletion ClosedXML_Tests/Excel/Loading/LoadingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ public void CanSuccessfullyLoadFiles()
@"Misc\InvalidPrintTitles.xlsx",
@"Misc\ExcelProducedWorkbookWithImages.xlsx",
@"Misc\EmptyCellValue.xlsx",
@"Misc\AllShapes.xlsx"
@"Misc\AllShapes.xlsx",
@"Misc\TableHeadersWithLineBreaks.xlsx"
};

foreach (var file in files)
Expand Down
Binary file not shown.