Skip to content

Commit d442d37

Browse files
committed
LoadFromCollection will now set the Hyperlink property if a member is Uri or ExcelHyperLink.
1 parent 29c8e63 commit d442d37

File tree

4 files changed

+109
-7
lines changed

4 files changed

+109
-7
lines changed

src/EPPlus/ExcelRangeBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ private void SetValueAddress(ExcelAddress address, _setValue valueMethod, object
191191
if (value is object[,] && (valueMethod == Set_Value || valueMethod == Set_StyleID))
192192
{
193193
// only simple set value is supported for bulk copy
194-
_worksheet.SetRangeValueInner(address.Start.Row, address.Start.Column, address.End.Row, address.End.Column, (object[,])value);
194+
_worksheet.SetRangeValueInner(address.Start.Row, address.Start.Column, address.End.Row, address.End.Column, (object[,])value, false);
195195
}
196196
else
197197
{

src/EPPlus/ExcelWorksheet.cs

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3944,16 +3944,65 @@ internal void SetValueStyleIdInner(int row, int col, object value, int styleId)
39443944
/// <param name="toRow">end row</param>
39453945
/// <param name="toColumn">end column</param>
39463946
/// <param name="values">set values</param>
3947-
internal void SetRangeValueInner(int fromRow, int fromColumn, int toRow, int toColumn, object[,] values)
3947+
/// <param name="setHyperLinkFromValue">If the value is of type Uri or ExcelHyperlink the Hyperlink property is set.</param>
3948+
internal void SetRangeValueInner(int fromRow, int fromColumn, int toRow, int toColumn, object[,] values, bool setHyperLinkFromValue)
39483949
{
3949-
3950-
_values.SetValueRange_Value(fromRow, fromColumn, values);
3950+
if (setHyperLinkFromValue)
3951+
{
3952+
SetValuesWithHyperLink(fromRow, fromColumn, values);
3953+
}
3954+
else
3955+
{
3956+
_values.SetValueRange_Value(fromRow, fromColumn, values);
3957+
}
39513958
//Clearout formulas and flags, for example the rich text flag.
39523959
_formulas.Clear(fromRow, fromColumn, values.GetUpperBound(0) + 1, values.GetUpperBound(1) + 1);
39533960
_flags.Clear(fromRow, fromColumn, values.GetUpperBound(0) + 1, values.GetUpperBound(1)+1);
39543961
_metadataStore.Clear(fromRow, fromColumn, values.GetUpperBound(0) + 1, values.GetUpperBound(1) + 1);
39553962
}
39563963

3964+
private void SetValuesWithHyperLink(int fromRow, int fromColumn, object[,] values)
3965+
{
3966+
var rowBound = values.GetUpperBound(0);
3967+
var colBound = values.GetUpperBound(1);
3968+
3969+
for (int r = 0; r <= rowBound; r++)
3970+
{
3971+
for (int c = 0; c <= colBound; c++)
3972+
{
3973+
var v = values[r, c];
3974+
var row = fromRow + r;
3975+
var col = fromColumn + c;
3976+
if (v==null)
3977+
{
3978+
_values.SetValue_Value(row, col, v);
3979+
continue;
3980+
}
3981+
var t = v.GetType();
3982+
if (t == typeof(Uri) || t == typeof(ExcelHyperLink))
3983+
{
3984+
_hyperLinks.SetValue(row, col, (Uri)v);
3985+
if (v is ExcelHyperLink hl)
3986+
{
3987+
SetValueInner(row, col, hl.Display);
3988+
}
3989+
else
3990+
{
3991+
var cv = GetValueInner(row, col);
3992+
if (cv == null || cv.ToString() == "")
3993+
{
3994+
SetValueInner(row, col, ((Uri)v).OriginalString);
3995+
}
3996+
}
3997+
}
3998+
else
3999+
{
4000+
_values.SetValue_Value(row, col, v);
4001+
}
4002+
}
4003+
}
4004+
}
4005+
39574006
/// <summary>
39584007
/// Existance check of sheet value
39594008
/// </summary>

src/EPPlus/LoadFunctions/LoadFunctionBase.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ internal ExcelRangeBase Load()
8787
}
8888
else
8989
{
90-
ws.SetRangeValueInner(Range._fromRow, Range._fromCol, Range._fromRow + nRows - 1, Range._fromCol + nCols - 1, values);
90+
ws.SetRangeValueInner(Range._fromRow, Range._fromCol, Range._fromRow + nRows - 1, Range._fromCol + nCols - 1, values, true);
9191
}
9292

9393

@@ -161,7 +161,7 @@ private void SetValuesAndFormulas(int nRows, int nCols, object[,] values, Dictio
161161
var fromRow = Range._fromRow;
162162
var rangeCol = Range._fromCol + col;
163163
var toRow = Range._fromRow + nRows - 1;
164-
ws.SetRangeValueInner(fromRow, rangeCol, toRow, rangeCol, columnValues);
164+
ws.SetRangeValueInner(fromRow, rangeCol, toRow, rangeCol, columnValues, true);
165165
}
166166

167167
}

src/EPPlusTest/LoadFunctions/LoadFromCollectionTests.cs

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ Date Author Change
4343
namespace EPPlusTest.LoadFunctions
4444
{
4545
[TestClass]
46-
public class LoadFromCollectionTests
46+
public class LoadFromCollectionTests : TestBase
4747
{
4848
internal abstract class BaseClass
4949
{
@@ -69,6 +69,7 @@ internal class BClass
6969
public string Id { get; set; }
7070
[System.ComponentModel.Description("MyName")]
7171
public string Name { get; set; }
72+
[EpplusTableColumn(Order = 3)]
7273
public int Number { get; set; }
7374
}
7475

@@ -79,6 +80,28 @@ internal class CamelCasedClass
7980
public string CamelCased_And_Underscored { get; set; }
8081
}
8182

83+
internal class UrlClass : BClass
84+
{
85+
[EpplusIgnore]
86+
public string EMailAddress { get; set; }
87+
[EpplusTableColumn(Order = 5, Header = "My Mail To")]
88+
public ExcelHyperLink MailTo
89+
{
90+
get
91+
{
92+
var url = new ExcelHyperLink("mailto:" + EMailAddress);
93+
url.Display = Name;
94+
return url;
95+
}
96+
}
97+
[EpplusTableColumn(Order = 4)]
98+
public Uri Url
99+
{
100+
get;
101+
set;
102+
}
103+
}
104+
82105
[TestMethod]
83106
public void ShouldNotIncludeHeadersWhenPrintHeadersIsOmitted()
84107
{
@@ -366,5 +389,35 @@ public void ShouldLoadExpandoObjects()
366389
Assert.AreEqual("TestName 2", sheet.Cells["B3"].Value);
367390
}
368391
}
392+
[TestMethod]
393+
public void ShouldSetHyperlinkForURIs()
394+
{
395+
var items = new List<UrlClass>()
396+
{
397+
new UrlClass{Id="1", Name="Person 1", EMailAddress="[email protected]"},
398+
new UrlClass{Id="2", Name="Person 2", EMailAddress="[email protected]"},
399+
new UrlClass{Id="2", Name="Person with Url", EMailAddress="[email protected]", Url=new Uri("https://epplussoftware.com")},
400+
};
401+
402+
using (var package = OpenPackage("LoadFromCollectionUrls.xlsx", true))
403+
{
404+
var sheet = package.Workbook.Worksheets.Add("test");
405+
var r = sheet.Cells["A1"].LoadFromCollection(items, true, TableStyles.Medium1);
406+
407+
Assert.AreEqual("MyId", sheet.Cells["A1"].Value);
408+
Assert.AreEqual("MyName", sheet.Cells["B1"].Value);
409+
Assert.AreEqual("Number", sheet.Cells["C1"].Value);
410+
Assert.AreEqual("Url", sheet.Cells["D1"].Value);
411+
Assert.AreEqual("My Mail To", sheet.Cells["E1"].Value);
412+
413+
Assert.AreEqual("1", sheet.Cells["A2"].Value);
414+
Assert.AreEqual("Person 2", sheet.Cells["B3"].Value);
415+
Assert.IsInstanceOfType(sheet.Cells["E3"].Hyperlink, typeof(ExcelHyperLink));
416+
Assert.AreEqual("Person 2", sheet.Cells["E3"].Value);
417+
418+
SaveAndCleanup(package);
419+
}
420+
}
421+
369422
}
370423
}

0 commit comments

Comments
 (0)