ソースを参照

Got rid of warnings

Piotr Czajkowski 3 ヶ月 前
コミット
b48482c223

+ 4 - 2
ExcelORM/ExcelORM/ExcelDynamicWriter.cs

@@ -30,17 +30,19 @@ public class ExcelDynamicWriter
     {
         var rowIndex = append switch
         {
-            true => worksheet.LastRowUsed().RowNumber() + 1,
+            true => worksheet.LastRowUsed()?.RowNumber() + 1,
             false => GenerateHeader(worksheet, values.First()),
         };
 
+        if (rowIndex == null) throw new NullReferenceException(nameof(rowIndex));
+
         foreach (var row in values)
         {
             foreach (var cell in row)
             {
                 if (cell.Value == null) continue;
 
-                worksheet.Cell(rowIndex, cell.Position).Value = XLCellValue.FromObject(cell.Value);
+                worksheet.Cell(rowIndex.Value, cell.Position).Value = XLCellValue.FromObject(cell.Value);
             }
 
             rowIndex++;

+ 7 - 5
ExcelORM/ExcelORM/ExcelWriter.cs

@@ -79,23 +79,25 @@ public class ExcelWriter
     {
         if (!values.Any()) return;
 
-        int rowIndex;
+        int? rowIndex;
         var properties = typeof(T).GetProperties();
         List<Mapping>? mapping = [];
 
         if (append)
         {
-            rowIndex = worksheet.LastRowUsed().RowNumber() + 1;
-            var headerCells = headerRowIndex != null ? worksheet.Row((int)headerRowIndex).CellsUsed() : worksheet.FirstRowUsed().CellsUsed();
+            rowIndex = worksheet.LastRowUsed()?.RowNumber() + 1;
+            var headerCells = headerRowIndex != null ? worksheet.Row((int)headerRowIndex).CellsUsed() : worksheet.FirstRowUsed()?.CellsUsed();
             mapping = Mapping.MapProperties<T>(headerCells);
             if (mapping == null || mapping.Count == 0) return;
         } else
             rowIndex = GenerateHeader<T>(worksheet, properties);
 
+        if (rowIndex == null) throw new NullReferenceException(nameof(rowIndex));
+
         foreach (var value in values)
         {
-            if (append) WriteRowAppend(value, worksheet, properties, rowIndex, mapping);
-            else WriteRow(value, worksheet, properties, rowIndex);
+            if (append) WriteRowAppend(value, worksheet, properties, rowIndex.Value, mapping);
+            else WriteRow(value, worksheet, properties, rowIndex.Value);
             
             rowIndex++;
         }