From 609ce1aebabd7ac35323213390d51bc0e0d40ead Mon Sep 17 00:00:00 2001 From: Paulo Ferro Date: Tue, 31 Oct 2023 12:13:31 +0000 Subject: [PATCH 1/3] Preserve return null when cellType == Blank behavior --- Npoi.Mapper/src/Npoi.Mapper/MapHelper.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Npoi.Mapper/src/Npoi.Mapper/MapHelper.cs b/Npoi.Mapper/src/Npoi.Mapper/MapHelper.cs index 5e4cc3f..8094109 100644 --- a/Npoi.Mapper/src/Npoi.Mapper/MapHelper.cs +++ b/Npoi.Mapper/src/Npoi.Mapper/MapHelper.cs @@ -336,7 +336,9 @@ public static bool TryGetCellValue(ICell cell, Type targetType, TrimSpacesType t value = null; if (cell == null) return true; - if (targetType == StringType) + var cellType = GetCellType(cell); + + if (targetType == StringType && cellType != CellType.Blank) { value = TrimString(CellDataFormatter.FormatCellValue(cell, evaluator)); return true; @@ -361,7 +363,7 @@ string TrimString(string raw) } } - switch (GetCellType(cell)) + switch (cellType) { case CellType.String: From ee03e0db9d9768538e217e71976bbc7380792ce7 Mon Sep 17 00:00:00 2001 From: Paulo Ferro Date: Tue, 31 Oct 2023 12:17:35 +0000 Subject: [PATCH 2/3] Preserve return null when cellType == Blank behavior on formula cells --- Npoi.Mapper/src/Npoi.Mapper/MapHelper.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Npoi.Mapper/src/Npoi.Mapper/MapHelper.cs b/Npoi.Mapper/src/Npoi.Mapper/MapHelper.cs index 8094109..6b5b99b 100644 --- a/Npoi.Mapper/src/Npoi.Mapper/MapHelper.cs +++ b/Npoi.Mapper/src/Npoi.Mapper/MapHelper.cs @@ -340,7 +340,9 @@ public static bool TryGetCellValue(ICell cell, Type targetType, TrimSpacesType t if (targetType == StringType && cellType != CellType.Blank) { - value = TrimString(CellDataFormatter.FormatCellValue(cell, evaluator)); + string trimmedValue = TrimString(CellDataFormatter.FormatCellValue(cell, evaluator)); + value = trimmedValue?.Length == 0 ? null : trimmedValue; + return true; } From 36e09f29b8777174228891972a7ccb1e1d6ed445 Mon Sep 17 00:00:00 2001 From: Paulo Ferro Date: Tue, 31 Oct 2023 18:51:31 +0000 Subject: [PATCH 3/3] Unit tests correction to keep previous behavior --- Npoi.Mapper/test/NestedPropertyTests.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Npoi.Mapper/test/NestedPropertyTests.cs b/Npoi.Mapper/test/NestedPropertyTests.cs index a851ef0..2c27292 100644 --- a/Npoi.Mapper/test/NestedPropertyTests.cs +++ b/Npoi.Mapper/test/NestedPropertyTests.cs @@ -89,9 +89,9 @@ public void ImportWithNestedPropertiesTest() Assert.AreEqual(addressId, objs[0].Value.Billing.Address.AddressId); Assert.AreEqual(date.Date, objs[0].Value.Billing.Dates.Dto.Date); - Assert.AreEqual("", objs[1].Value.Name); + Assert.AreEqual(null, objs[1].Value.Name); Assert.AreEqual(customerAge, objs[1].Value.Age); - Assert.AreEqual("", objs[1].Value.Billing.Contact.Name); + Assert.AreEqual(null, objs[1].Value.Billing.Contact.Name); Assert.AreEqual(null, objs[1].Value.Billing.Address); Assert.AreEqual(DateTime.MinValue, objs[1].Value.Billing.Dates.Dto.Date); }