diff --git a/README.md b/README.md index 9f829465..eea61471 100644 --- a/README.md +++ b/README.md @@ -457,6 +457,25 @@ Cells covered by a shared formula report `HasFormula` as `true` but leave `Formu Font colors can also be theme colors (`Theme` and `Tint`), indexed colors (`Indexed`) or automatic (`Auto`). `NumberFormat` is null for cells in the General format. +#### 14. Limit on empty cells + +Rows that are missing from the worksheet are returned as empty rows. To protect against worksheets that declare a huge range with little data, a query throws an `InvalidDataException` once the empty cells it would have to fill in exceed `MaxSynthesizedCells` (100,000 by default). +Each empty row counts as the number of columns being read, so a `QueryRange` with a wide `endColumnIndex` reaches the limit after a few empty rows (e.g. 148 empty rows × 1000 columns). + +To read such worksheets raise the limit, or set it to `null` to disable it: + +```csharp +var config = new OpenXmlConfiguration +{ + MaxSynthesizedCells = 10_000_000 // or null to disable the limit +}; + +var importer = MiniExcelV2.Importers.GetOpenXmlImporter(); +var rows = importer.QueryRange(path, startColumnIndex: 1, endColumnIndex: 1000, configuration: config); +``` + +The limit does not apply when `IgnoreEmptyRows` is enabled, since empty rows are then skipped. + ### Create/Export Excel diff --git a/V2-Upgrade-Notes.md b/V2-Upgrade-Notes.md index 8e75f655..f90f7003 100644 --- a/V2-Upgrade-Notes.md +++ b/V2-Upgrade-Notes.md @@ -18,3 +18,4 @@ so the return type for `OpenXmlImporter.QueryAsync` is `IAsyncEnumerable` ins - A `leaveOpen` parameter has been added to most methods that take a stream as input in both `OpenXmlImporter` and `CsvImporter` to configure whether the stream must be disposed after the operation performed is completed. - The `useHeaderRow` parameter in multiple `OpenXmlImporter` and `CsvImporter` methods has been renamed to `hasHeaderRow` for making its usage clearer. - `CsvExporter.Export` API methods, not being required to return the same type of `OpenXmlExporter.Export`, now return `int` instead of `int[]`. +- Queries now throw an `InvalidDataException` when the empty rows they have to fill in exceed `OpenXmlConfiguration.MaxSynthesizedCells` (100,000 cells by default). Each empty row counts as the number of columns being read, so a `QueryRange` with a wide `endColumnIndex` can hit the limit on a small worksheet. Raise the value or set it to `null` to read such worksheets as in 1.x; see [Limit on empty cells](README.md#14-limit-on-empty-cells). diff --git a/src/MiniExcel.OpenXml/OpenXmlConfiguration.cs b/src/MiniExcel.OpenXml/OpenXmlConfiguration.cs index a5007bba..c3b7da87 100644 --- a/src/MiniExcel.OpenXml/OpenXmlConfiguration.cs +++ b/src/MiniExcel.OpenXml/OpenXmlConfiguration.cs @@ -22,7 +22,10 @@ public class OpenXmlConfiguration : MiniExcelBaseConfiguration /// /// Maximum number of cells that may be synthesized for implicit empty rows during a query. - /// Each synthesized row counts as at least one cell. Set to to disable the limit. + /// Each synthesized row counts as one cell per column being read: the header columns when a header row is used, + /// otherwise every column up to the last column of the sheet or the requested endColumnIndex, so a wide + /// QueryRange reaches the limit after a few empty rows. + /// The limit does not apply when is enabled. Set to to disable the limit. /// public long? MaxSynthesizedCells { get; set; } = 100_000; diff --git a/src/MiniExcel.OpenXml/Reader/OpenXmlReader.cs b/src/MiniExcel.OpenXml/Reader/OpenXmlReader.cs index 57a96b3d..1a753cd7 100644 --- a/src/MiniExcel.OpenXml/Reader/OpenXmlReader.cs +++ b/src/MiniExcel.OpenXml/Reader/OpenXmlReader.cs @@ -270,7 +270,9 @@ internal static async Task CreateAsync(Stream stream, IMiniExcelC synthesizedCellCount += (long)(emptyRowCount + selfClosingRowCount) * columnCount; if (synthesizedCellCount > maxSynthesizedCells) - throw new InvalidDataException($"The worksheet exceeds the configured limit of {maxSynthesizedCells} synthesized empty cells."); + throw new InvalidDataException( + $"The worksheet exceeds the configured limit of {maxSynthesizedCells} synthesized empty cells. " + + $"Increase {nameof(OpenXmlConfiguration)}.{nameof(OpenXmlConfiguration.MaxSynthesizedCells)} or set it to null to disable the limit."); } var query = QueryRowAsync(reader, isFirstRow, startRowIndex, nextRowIndex, rowIndex, diff --git a/tests/MiniExcel.OpenXml.Tests/Issues/MiniExcelGithubIssuesTests.cs b/tests/MiniExcel.OpenXml.Tests/Issues/MiniExcelGithubIssuesTests.cs index a4fb933f..5808099a 100644 --- a/tests/MiniExcel.OpenXml.Tests/Issues/MiniExcelGithubIssuesTests.cs +++ b/tests/MiniExcel.OpenXml.Tests/Issues/MiniExcelGithubIssuesTests.cs @@ -158,6 +158,39 @@ public void QueryRejectsWorksheetExceedingSynthesizedCellLimit() Assert.Contains("synthesized empty cells", exception.Message); } + /// + /// https://github.com/mini-software/MiniExcel/issues/1075 + /// + [Fact] + public void QueryRangeSynthesizedCellLimitNamesTheSettingAndCanBeDisabled() + { + using var path = AutoDeletingPath.Create(); + _excelExporter.Export(path.FilePath, new[] { new { Value = "control" } }); + SheetHelper.ReplaceFirstSheetXml(path.FilePath, """ + + + + top + bottom + + + """); + + // Each of the 148 empty rows counts as the 1000 requested columns, which exceeds the default limit + var exception = Assert.Throws(() => + _excelImporter.QueryRange(path.FilePath, startColumnIndex: 1, endColumnIndex: 1000).ToList()); + Assert.Contains(nameof(OpenXmlConfiguration.MaxSynthesizedCells), exception.Message); + + var config = new OpenXmlConfiguration { MaxSynthesizedCells = null }; + var rows = _excelImporter.QueryRange(path.FilePath, startColumnIndex: 1, endColumnIndex: 1000, configuration: config).ToList(); + Assert.Equal(150, rows.Count); + Assert.Equal("bottom", (string)rows[149].A); + + var ignoreEmptyRowsConfig = new OpenXmlConfiguration { IgnoreEmptyRows = true }; + var nonEmptyRows = _excelImporter.QueryRange(path.FilePath, startColumnIndex: 1, endColumnIndex: 1000, configuration: ignoreEmptyRowsConfig).ToList(); + Assert.Equal(2, nonEmptyRows.Count); + } + [Theory] [InlineData("1048577")] [InlineData("2000000000")]