1
0

10 Commity 32d98b8674 ... 375665831a

Autor SHA1 Správa Dátum
  Piotr Czajkowski 375665831a Added info about 2.5 2 mesiacov pred
  Piotr Czajkowski 783fbf07fa Cosmetics 2 mesiacov pred
  Piotr Czajkowski 77efeb52f2 Cleanup 2 mesiacov pred
  Piotr Czajkowski 37f22e351a Let's be consistent 2 mesiacov pred
  Piotr Czajkowski 5badcc3fa6 Prefer to have Location in Message 2 mesiacov pred
  Piotr Czajkowski e6a97651b5 Added worksheet name 2 mesiacov pred
  Piotr Czajkowski 1f7c85e8a9 Cosmetics 2 mesiacov pred
  Piotr Czajkowski eb2c7d4965 Cosmetics 2 mesiacov pred
  Piotr Czajkowski 821a1619f0 More info in exception 2 mesiacov pred
  Piotr Czajkowski f05d82254d Added BadDateThrows 2 mesiacov pred

+ 7 - 11
ExcelORM/ExcelORM/ExcelDynamicReader.cs

@@ -35,8 +35,7 @@ public class ExcelDynamicReader : IDisposable
                     continue;
                 }
 
-                if (item.Type == null) item.Type = cell.Value.ValueType();
-
+                item.Type ??= cell.Value.ValueType();
                 var cellItem = item with
                 {
                     Value = cell.Value.ToObject()
@@ -98,15 +97,12 @@ public class ExcelDynamicReader : IDisposable
 
     public IEnumerable<DynamicWorksheet> ReadAll(uint startFrom = 1, uint skip = 0)
     {
-        foreach (var worksheet in xlWorkbook.Worksheets)
+        return xlWorkbook.Worksheets.Select(worksheet => new DynamicWorksheet
         {
-            yield return new DynamicWorksheet
-            {
-                Name = worksheet.Name,
-                Position = worksheet.Position,
-                Cells = Read(worksheet, startFrom, skip)
-            };
-        }
+            Name = worksheet.Name,
+            Position = worksheet.Position,
+            Cells = Read(worksheet, startFrom, skip)
+        });
     }
 
     public void Dispose()
@@ -119,7 +115,7 @@ public class ExcelDynamicReader : IDisposable
     {
         if (disposing)
         {
-            xlWorkbook?.Dispose();
+            xlWorkbook.Dispose();
         }
     }
     ~ExcelDynamicReader() => Dispose(false);

+ 1 - 1
ExcelORM/ExcelORM/ExcelDynamicWriter.cs

@@ -86,7 +86,7 @@ public class ExcelDynamicWriter : IDisposable
     {
         if (disposing)
         {
-            xlWorkbook?.Dispose();
+            xlWorkbook.Dispose();
         }
     }
     ~ExcelDynamicWriter() => Dispose(false);

+ 2 - 2
ExcelORM/ExcelORM/ExcelORM.csproj

@@ -4,7 +4,7 @@
         <TargetFramework>net8.0</TargetFramework>
         <ImplicitUsings>enable</ImplicitUsings>
         <Nullable>enable</Nullable>
-        <Version>2.4.1</Version>
+        <Version>2.5.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>Updated ClosedXML to 0.104.2 due to security issue.</PackageReleaseNotes>
+	<PackageReleaseNotes>Added Location to exception for easier troubleshooting.</PackageReleaseNotes>
     </PropertyGroup>
 
     <ItemGroup>

+ 16 - 13
ExcelORM/ExcelORM/ExcelReader.cs

@@ -38,16 +38,23 @@ public class ExcelReader : IDisposable
                 var property = type.GetProperty(item.PropertyName);
                 if (property == null) continue;
 
-                if (property.PropertyType.BaseType == typeof(SpecialBase) &&
-                    RuntimeHelpers.GetUninitializedObject(property.PropertyType) is SpecialBase special)
+                try
                 {
-                    special.GetValueFromCell(cell);
+                    if (property.PropertyType.BaseType == typeof(SpecialBase) &&
+                        RuntimeHelpers.GetUninitializedObject(property.PropertyType) is SpecialBase special)
+                    {
+                        special.GetValueFromCell(cell);
 
-                    property.SetValue(current, special);
-                    continue;
-                }
+                        property.SetValue(current, special);
+                        continue;
+                    }
 
-                current.SetPropertyValue(property, cell.Value);
+                    current.SetPropertyValue(property, cell.Value);
+                }
+                catch (ArgumentException e)
+                {
+                    throw new ArgumentException($"{e.Message}\nLocation: {cell.Address.ColumnLetter}{cell.Address.RowNumber} ({cell.Worksheet.Name})", e);
+                }
             }
 
             yield return current;
@@ -103,11 +110,7 @@ public class ExcelReader : IDisposable
 
     public IEnumerable<T> ReadAll<T>(uint startFrom = 1, uint skip = 0) where T : class
     {
-        foreach (var worksheet in xlWorkbook.Worksheets)
-        {
-            foreach (var item in Read<T>(worksheet, startFrom, skip))
-                yield return item;
-        }
+        return xlWorkbook.Worksheets.SelectMany(worksheet => Read<T>(worksheet, startFrom, skip));
     }
 
     public void Dispose()
@@ -120,7 +123,7 @@ public class ExcelReader : IDisposable
     {
         if (disposing)
         {
-            xlWorkbook?.Dispose();
+            xlWorkbook.Dispose();
         }
     }
     ~ExcelReader() => Dispose(false);

+ 4 - 4
ExcelORM/ExcelORM/ExcelWriter.cs

@@ -19,7 +19,7 @@ public class ExcelWriter : IDisposable
         xlWorkbook = workbook ?? throw new ArgumentNullException(nameof(workbook));
     }
 
-    private static int GenerateHeader<T>(IXLWorksheet worksheet, PropertyInfo[] properties) where T : class
+    private static int GenerateHeader(IXLWorksheet worksheet, PropertyInfo[] properties)
     {
         var rowIndex = 1;
         var cellIndex = 1;
@@ -56,7 +56,7 @@ public class ExcelWriter : IDisposable
             if (property.Skip()) continue;
 
             var mapped = mapping.FirstOrDefault(x => x.PropertyName != null && x.PropertyName.Equals(property.Name));
-            if (mapped == null || mapped.Position == null) continue;
+            if (mapped?.Position == null) continue;
 
             WriteCell(value, property, worksheet.Cell(rowIndex, mapped.Position.Value));
         }
@@ -90,7 +90,7 @@ public class ExcelWriter : IDisposable
             mapping = Mapping.MapProperties<T>(headerCells);
             if (mapping == null || mapping.Count == 0) return;
         } else
-            rowIndex = GenerateHeader<T>(worksheet, properties);
+            rowIndex = GenerateHeader(worksheet, properties);
 
         if (rowIndex == null) throw new NullReferenceException(nameof(rowIndex));
 
@@ -130,7 +130,7 @@ public class ExcelWriter : IDisposable
     {
         if (disposing)
         {
-            xlWorkbook?.Dispose();
+            xlWorkbook.Dispose();
         }
     }
     ~ExcelWriter() => Dispose(false);

+ 3 - 0
ExcelORM/ExcelORMTests/ExcelORMTests.csproj

@@ -53,6 +53,9 @@
       <None Update="testFiles\withFormula.xlsx">
         <CopyToOutputDirectory>Always</CopyToOutputDirectory>
       </None>
+      <None Update="testFiles\badDate.xlsx">
+        <CopyToOutputDirectory>Always</CopyToOutputDirectory>
+      </None>
     </ItemGroup>
 
     <ItemGroup>

+ 9 - 0
ExcelORM/ExcelORMTests/ReaderTests.cs

@@ -11,6 +11,7 @@ public class ReaderTests
     private const string MultipleSheetsFile = "testFiles/multipleSheets.xlsx";
     private const string DifferentTypesFile = "testFiles/differentTypes.xlsx";
     private const string WithFormulaFile = "testFiles/withFormula.xlsx";
+    private const string BadDate = "testFiles/badDate.xlsx";
     
     [Fact]
     public void Read()
@@ -109,4 +110,12 @@ public class ReaderTests
         var results = reader.Read<TestWithFormula>().ToArray();
         Assert.NotEmpty(results);
     }
+    
+    [Fact]
+    public void BadDateThrows()
+    {
+        using var reader = new ExcelReader(BadDate);
+        var exception = Assert.Throws<ArgumentException>(() => reader.Read<TestTypes>().ToArray());
+        Assert.Contains("Location", exception.Message);
+    }
 }

BIN
ExcelORM/ExcelORMTests/testFiles/badDate.xlsx


+ 2 - 0
README.md

@@ -20,6 +20,8 @@ And their nullable variants.
 As always, feel free to use it however you desire. But I provide you with no guarantee whatsoever. Enjoy!
 
 ## Version history
+In version 2.5 I've added Location to ArgumentException Message in ExcelReader. It'll show address of affected cell and the worksheet's name.
+
 In version 2 I've added ability to read data dynamically without a need to create a special type. Useful when you need to read/write some not so organized data.
 
 In version 2.2 I've added support for formulas, but as this library is based on ClosedXML it has its limitations, as per [Evaluating Formulas](https://github.com/closedxml/closedxml/wiki/Evaluating-Formulas):