Browse Source

Added WriteWithHyperlink

Piotr Czajkowski 5 months ago
parent
commit
1f7a017d4a

+ 2 - 2
ExcelORM/ExcelORM/ExcelORM.csproj

@@ -4,7 +4,7 @@
         <TargetFramework>net8.0</TargetFramework>
         <ImplicitUsings>enable</ImplicitUsings>
         <Nullable>enable</Nullable>
-        <Version>2.2.0</Version>
+        <Version>2.3.0</Version>
         <PackageProjectUrl>https://git.liox.eu/pczajkowski/ExcelORM</PackageProjectUrl>
         <RepositoryUrl>https://github.com/pczajkowski/ExcelORM</RepositoryUrl>
         <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
@@ -14,7 +14,7 @@
 	<Authors>Piotr Czajkowski</Authors>
 	<Description>Simple library to read/write C# objects from/to Excel files. </Description>
 	<RepositoryType>GitHub</RepositoryType>
-	<PackageReleaseNotes>Using .NET 8 now. Added support for formulas. Got rid of new constraint.</PackageReleaseNotes>
+	<PackageReleaseNotes>Added Hyperlink and improved appending.</PackageReleaseNotes>
     </PropertyGroup>
 
     <ItemGroup>

+ 1 - 1
ExcelORM/ExcelORM/Models/Formula.cs

@@ -10,7 +10,7 @@ namespace ExcelORM.Models
         public override void GetValueFromCell(IXLCell cell)
         {
             Value = cell.Value.ToObject();
-            FormulaA1 = cell.FormulaA1;
+            if (cell.HasFormula) FormulaA1 = cell.FormulaA1;
         }
     }
 }

+ 21 - 0
ExcelORM/ExcelORM/Models/Hyperlink.cs

@@ -0,0 +1,21 @@
+using ClosedXML.Excel;
+
+namespace ExcelORM.Models
+{
+    public class Hyperlink : SpecialBase
+    {
+        public object? Value { get; set; }
+        public XLHyperlink? Link { get; set; }
+        public override void SetCellValue(IXLCell cell)
+        {
+            cell.Value = XLCellValue.FromObject(Value);
+            cell.SetHyperlink(Link);
+        }
+
+        public override void GetValueFromCell(IXLCell cell)
+        {
+            Value = cell.Value.ToObject();
+            if (cell.HasHyperlink) Link = cell.GetHyperlink();
+        }
+    }
+}

+ 9 - 0
ExcelORM/ExcelORMTests/TestWithHyperlink.cs

@@ -0,0 +1,9 @@
+using ExcelORM.Models;
+
+namespace ExcelORMTests
+{
+    public record TestWithHyperlink : Test
+    {
+        public Hyperlink? Link { get; set; }
+    }
+}

+ 33 - 0
ExcelORM/ExcelORMTests/WriterTests.cs

@@ -1,5 +1,6 @@
 using ExcelORM;
 using ExcelORM.Models;
+using ClosedXML.Excel;
 
 namespace ExcelORMTests;
 
@@ -243,4 +244,36 @@ public class WriterTests
 
         File.Delete(testFile);
     }
+
+    private static readonly TestWithHyperlink[] arrayWithHyperlinks =
+    {
+        new() { Name = "Bilbo", Surname = "Baggins", Job = "Eater", Link = new Hyperlink{ Value = "Wiki", Link = new XLHyperlink("https://en.wikipedia.org/wiki/Bilbo_Baggins") } },
+        new() { Name = "John", Job = "Policeman", Link = new Hyperlink{ Value = "CNN", Link = new XLHyperlink("https://edition.cnn.com/2023/12/10/us/john-okeefe-boston-police-death-cec/index.html") } },
+        new() { Name = "Bruce", Surname = "Lee", Job = "Fighter", Link = new Hyperlink{ Value = "IMDb", Link = new XLHyperlink("https://www.imdb.com/name/nm0000045/") } }
+    };
+
+    [Fact]
+    public void WriteWithHyperlink()
+    {
+        var testFile = Path.GetRandomFileName();
+        testFile = Path.ChangeExtension(testFile, "xlsx");
+
+        var writer = new ExcelWriter(testFile);
+        writer.Write(arrayWithHyperlinks);
+        writer.SaveAs(testFile);
+
+        var reader = new ExcelReader(testFile);
+        var readArray = reader.Read<TestWithHyperlink>().ToArray();
+        Assert.Equal(arrayWithFormulas.Length, readArray.Length);
+
+        for (var i = 0; i < readArray.Length; i++)
+        {
+            Assert.NotNull(arrayWithHyperlinks[i].Link);
+            Assert.NotNull(readArray[i].Link);
+            Assert.Equal(arrayWithHyperlinks[i].Link?.Value, readArray[i].Link?.Value);
+            Assert.Equal(arrayWithHyperlinks[i].Link?.Link?.ToString(), readArray[i].Link?.Link?.ToString());
+        }
+        
+        File.Delete(testFile);
+    }
 }