From e234cd46f5c34aa735ed87aae7c9c2185ae5899a Mon Sep 17 00:00:00 2001 From: MarkPflug Date: Thu, 16 Nov 2023 06:21:08 -0800 Subject: [PATCH] Fix for #143: data skipped when headers disabled and blank lines at start of file. --- Directory.Build.targets | 4 +-- docs/ReleaseNotes.md | 4 +++ .../ExcelDataReaderTests.cs | 33 +++++++++++++++++-- .../Sylvan.Data.Excel.csproj | 2 +- .../Xlsb/XlsbWorkbookReader.cs | 2 +- .../Xlsx/XlsxWorkbookReader.cs | 2 +- 6 files changed, 40 insertions(+), 7 deletions(-) diff --git a/Directory.Build.targets b/Directory.Build.targets index 4e71a20..7ecd89f 100644 --- a/Directory.Build.targets +++ b/Directory.Build.targets @@ -15,8 +15,8 @@ - - + + diff --git a/docs/ReleaseNotes.md b/docs/ReleaseNotes.md index a44e93c..be07595 100644 --- a/docs/ReleaseNotes.md +++ b/docs/ReleaseNotes.md @@ -1,5 +1,9 @@ # Sylvan.Data.Excel Release Notes +_0.4.18_ +- Fix a bug where column filters were applied to one too many columns when writing .xlsb files. +- Fix a bug where data would be skipped when headers were disabled and a worksheet started with blank line(s). + _0.4.17_ - Exclude phonetic component when reading string values. - Allow invalid ref values, which Excel appears to treat as missing. diff --git a/source/Sylvan.Data.Excel.Tests/ExcelDataReaderTests.cs b/source/Sylvan.Data.Excel.Tests/ExcelDataReaderTests.cs index 4abc02d..64d8420 100644 --- a/source/Sylvan.Data.Excel.Tests/ExcelDataReaderTests.cs +++ b/source/Sylvan.Data.Excel.Tests/ExcelDataReaderTests.cs @@ -341,7 +341,7 @@ public void ErrorAsNull() using var edr = ExcelDataReader.Create(file, opts); Assert.True(edr.Read()); Assert.True(edr.IsDBNull(2)); - Assert.True(edr.IsDBNullAsync(2).Result); + //Assert.True(edr.IsDBNullAsync(2)); Assert.Equal("", edr.GetString(2)); } @@ -394,7 +394,7 @@ public void ErrorAsEmptyString() using var edr = ExcelDataReader.Create(file, opts); Assert.True(edr.Read()); Assert.False(edr.IsDBNull(2)); - Assert.False(edr.IsDBNullAsync(2).Result); + //Assert.False(edr.IsDBNullAsync(2).Result); Assert.Equal("", edr.GetString(2)); } @@ -1061,6 +1061,35 @@ public void BlankFirstRow() Assert.False(edr.Read()); } + [Fact] + public void BlankFirstRowNoHeader() + { + var file = GetFile("BlankFirstRow"); + var o = new ExcelDataReaderOptions { Schema = ExcelSchema.NoHeaders }; + using var edr = ExcelDataReader.Create(file, o); + + Assert.Equal(0, edr.FieldCount); + Assert.Equal(0, edr.RowFieldCount); + Assert.True(edr.Read()); + Assert.Equal(0, edr.FieldCount); + Assert.Equal(0, edr.RowFieldCount); + Assert.True(edr.Read()); + Assert.Equal(0, edr.FieldCount); + Assert.Equal(4, edr.RowFieldCount); + Assert.Equal("", edr.GetString(0)); + Assert.Equal("a", edr.GetString(1)); + Assert.Equal("b", edr.GetString(2)); + Assert.Equal("c", edr.GetString(3)); + Assert.True(edr.Read()); + Assert.Equal(0, edr.FieldCount); + Assert.Equal(4, edr.RowFieldCount); + Assert.Equal("", edr.GetString(0)); + Assert.Equal("1", edr.GetString(1)); + Assert.Equal("2", edr.GetString(2)); + Assert.Equal("3", edr.GetString(3)); + Assert.False(edr.Read()); + } + [Theory] [InlineData("Big")] [InlineData("BlankFirstRow")] diff --git a/source/Sylvan.Data.Excel/Sylvan.Data.Excel.csproj b/source/Sylvan.Data.Excel/Sylvan.Data.Excel.csproj index 15bd41b..33dbfce 100644 --- a/source/Sylvan.Data.Excel/Sylvan.Data.Excel.csproj +++ b/source/Sylvan.Data.Excel/Sylvan.Data.Excel.csproj @@ -3,7 +3,7 @@ net6.0;netstandard2.1;netstandard2.0 latest - 0.4.17 + 0.4.18 A cross-platform .NET library for reading Excel data files. excel;xls;xlsx;xlsb;datareader enable diff --git a/source/Sylvan.Data.Excel/Xlsb/XlsbWorkbookReader.cs b/source/Sylvan.Data.Excel/Xlsb/XlsbWorkbookReader.cs index de7311a..a44adf1 100644 --- a/source/Sylvan.Data.Excel/Xlsb/XlsbWorkbookReader.cs +++ b/source/Sylvan.Data.Excel/Xlsb/XlsbWorkbookReader.cs @@ -321,7 +321,7 @@ public override bool Read() if (hasRows) { this.state = State.Open; - if (curFieldCount >= 0) + if (rowIndex == parsedRowIndex && curFieldCount >= 0) { this.rowFieldCount = curFieldCount; this.curFieldCount = -1; diff --git a/source/Sylvan.Data.Excel/Xlsx/XlsxWorkbookReader.cs b/source/Sylvan.Data.Excel/Xlsx/XlsxWorkbookReader.cs index 55cacc5..07e67e2 100644 --- a/source/Sylvan.Data.Excel/Xlsx/XlsxWorkbookReader.cs +++ b/source/Sylvan.Data.Excel/Xlsx/XlsxWorkbookReader.cs @@ -425,7 +425,7 @@ public override bool Read() if (hasRows) { this.state = State.Open; - if (curFieldCount >= 0) + if (rowIndex == parsedRowIndex && curFieldCount >= 0) { this.rowFieldCount = curFieldCount; this.curFieldCount = -1;