Skip to content

Commit

Permalink
#1058, #946 - Use built in style for hyperlinks in LoadFromCollection…
Browse files Browse the repository at this point in the history
… - v6
  • Loading branch information
swmal committed Oct 2, 2023
1 parent 10de8a1 commit b65fd75
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 4 deletions.
9 changes: 9 additions & 0 deletions src/EPPlus/Attributes/EpplusTableColumnAttributeBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@ public bool Hidden
set;
}

/// <summary>
/// Indicates whether the Built in (default) hyperlink style should be
/// applied to hyperlinks or not. Default value is true.
/// </summary>
public bool UseBuiltInHyperlinkStyle
{
get; set;
} = true;

/// <summary>
/// If not <see cref="RowFunctions.None"/> the last cell in the column (the totals row) will contain a formula of the specified type.
/// </summary>
Expand Down
20 changes: 17 additions & 3 deletions src/EPPlus/ExcelWorksheet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3490,12 +3490,13 @@ internal void SetValueStyleIdInner(int row, int col, object value, int styleId)
/// <param name="toRow">end row</param>
/// <param name="toColumn">end column</param>
/// <param name="values">set values</param>
/// <param name="addHyperlinkStyles">Will add built in styles for hyperlinks</param>
/// <param name="setHyperLinkFromValue">If the value is of type Uri or ExcelHyperlink the Hyperlink property is set.</param>
internal void SetRangeValueInner(int fromRow, int fromColumn, int toRow, int toColumn, object[,] values, bool setHyperLinkFromValue)
internal void SetRangeValueInner(int fromRow, int fromColumn, int toRow, int toColumn, object[,] values, bool setHyperLinkFromValue, bool addHyperlinkStyles = false)
{
if (setHyperLinkFromValue)
{
SetValuesWithHyperLink(fromRow, fromColumn, values);
SetValuesWithHyperLink(fromRow, fromColumn, values, addHyperlinkStyles);
}
else
{
Expand All @@ -3507,11 +3508,12 @@ internal void SetRangeValueInner(int fromRow, int fromColumn, int toRow, int toC
_metadataStore.Clear(fromRow, fromColumn, values.GetUpperBound(0) + 1, values.GetUpperBound(1) + 1);
}

private void SetValuesWithHyperLink(int fromRow, int fromColumn, object[,] values)
private void SetValuesWithHyperLink(int fromRow, int fromColumn, object[,] values, bool addHyperlinkStyles)
{
var rowBound = values.GetUpperBound(0);
var colBound = values.GetUpperBound(1);

var hyperlinkStylesAdded = false;
for (int r = 0; r <= rowBound; r++)
{
for (int c = 0; c <= colBound; c++)
Expand All @@ -3527,6 +3529,18 @@ private void SetValuesWithHyperLink(int fromRow, int fromColumn, object[,] value
var t = v.GetType();
if (t == typeof(Uri) || t == typeof(ExcelHyperLink))
{
if (!hyperlinkStylesAdded && addHyperlinkStyles)
{
if (!Workbook.Styles.NamedStyles.ExistsKey("Hyperlink"))
{
var hls = Workbook.Styles.CreateNamedStyle("Hyperlink");
hls.BuildInId = 8;
hls.Style.Font.UnderLine = true;
hls.Style.Font.Color.SetColor(System.Drawing.Color.FromArgb(0x0563C1));
}
hyperlinkStylesAdded = true;
}
Cells[row, col].StyleName = "Hyperlink";
_hyperLinks.SetValue(row, col, (Uri)v);
if (v is ExcelHyperLink hl)
{
Expand Down
2 changes: 2 additions & 0 deletions src/EPPlus/LoadFunctions/ColumnInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ public ColumnInfo()

public string NumberFormat { get; set; }

public bool UseBuiltInHyperlinkStyle { get; set; }

public RowFunctions TotalsRowFunction { get; set; }

public string TotalsRowFormula { get; set; }
Expand Down
6 changes: 5 additions & 1 deletion src/EPPlus/LoadFunctions/LoadFunctionBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,12 @@ public LoadFunctionBase(ExcelRangeBase range, LoadFunctionFunctionParamsBase par
PrintHeaders = parameters.PrintHeaders;
TableStyle = parameters.TableStyle;
TableName = parameters.TableName?.Trim();

_useBuiltInStylesForHyperlinks = parameters.UseBuiltInStylesForHyperlinks;
}

private readonly bool _useBuiltInStylesForHyperlinks;

/// <summary>
/// The range to which the data should be loaded
/// </summary>
Expand Down Expand Up @@ -95,7 +99,7 @@ internal ExcelRangeBase Load()
}
else
{
ws.SetRangeValueInner(Range._fromRow, Range._fromCol, Range._fromRow + nRows - 1, Range._fromCol + nCols - 1, values, true);
ws.SetRangeValueInner(Range._fromRow, Range._fromCol, Range._fromRow + nRows - 1, Range._fromCol + nCols - 1, values, true, _useBuiltInStylesForHyperlinks);
}


Expand Down
10 changes: 10 additions & 0 deletions src/EPPlus/LoadFunctions/Params/LoadFunctionFunctionParamsBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,15 @@ public TableStyles? TableStyle
{
get; set;
} = null;

/// <summary>
/// If true, EPPlus will add the built in (default) styles for hyperlinks and apply them on any member
/// that is of the <see cref="Uri"/> or <see cref="ExcelHyperLink"/> types. Default value is true.
/// </summary>
public bool UseBuiltInStylesForHyperlinks
{
get;
set;
} = true;
}
}
38 changes: 38 additions & 0 deletions src/EPPlusTest/LoadFunctions/LoadFromCollectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,30 @@ namespace EPPlusTest.LoadFunctions
[TestClass]
public class LoadFromCollectionTests : TestBase
{
[EpplusTable(AutofitColumns = true, PrintHeaders = true, TableStyle = TableStyles.Light10)]
internal class Company
{
public Company(int id, string name, Uri url)
{
Id = id;
Name = name;
Url = url;
}

[EpplusTableColumn(Header = "Id", Order = 1)]
public int Id
{
get; set;
}

[EpplusTableColumn(Header = "Name", Order = 2)]
public string Name { get; set; }

[EpplusTableColumn(Header = "Homepage", Order = 3)]
public Uri Url { get; set; }

}

internal abstract class BaseClass
{
public string Id { get; set; }
Expand Down Expand Up @@ -504,5 +528,19 @@ public void LoadListOfClassWithEnumWithDescription()
}
}

[TestMethod]
public void LoadWithAttributesTest()
{
var l = new List<Company>();
l.Add(new Company(1, "EPPlus Software AB", new Uri("https://epplussoftware.com")));

using (var package = OpenPackage("LoadFromCollectionAttr.xlsx", true))
{
var sheet = package.Workbook.Worksheets.Add("test");
sheet.Cells["A1"].LoadFromCollection(l, x => x.UseBuiltInStylesForHyperlinks = true);

SaveAndCleanup(package);
}
}
}
}

0 comments on commit b65fd75

Please sign in to comment.