From 306eb9029618b81f45dd098a62b759020aa038af Mon Sep 17 00:00:00 2001 From: OssianEPPlus <122265629+OssianEPPlus@users.noreply.github.com> Date: Mon, 27 May 2024 15:08:03 +0200 Subject: [PATCH] Added fix for empty worksheet (#1463) * Added fix for empty worksheet * Fixed short minValue to int MinValue --- .../FormulaExpressions/RangeExpression.cs | 18 ++++++++++++------ .../IntegrationTests/FormulaParserTests.cs | 15 +++++++++++++++ 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/src/EPPlus/FormulaParsing/FormulaExpressions/RangeExpression.cs b/src/EPPlus/FormulaParsing/FormulaExpressions/RangeExpression.cs index e9987790f..9f1f29008 100644 --- a/src/EPPlus/FormulaParsing/FormulaExpressions/RangeExpression.cs +++ b/src/EPPlus/FormulaParsing/FormulaExpressions/RangeExpression.cs @@ -22,7 +22,7 @@ internal RangeExpression(FormulaRangeAddress address) : base(address._context) } public RangeExpression(string address, ParsingContext ctx, short externalReferenceIx, int worksheetIx) : base(ctx) { - _addressInfo = new FormulaRangeAddress(ctx) { ExternalReferenceIx= externalReferenceIx, WorksheetIx = worksheetIx < 0 ? ctx.CurrentCell.WorksheetIx : worksheetIx }; + _addressInfo = new FormulaRangeAddress(ctx) { ExternalReferenceIx= externalReferenceIx, WorksheetIx = worksheetIx == int.MinValue ? ctx.CurrentCell.WorksheetIx : worksheetIx }; ExcelCellBase.GetRowColFromAddress(address, out int fromRow, out int fromCol, out int toRow, out int toCol, out bool fixedFromRow, out bool fixedFromCol, out bool fixedToRow, out bool fixedToCol); _addressInfo.FromRow = fromRow==0 ? 1 : fromRow; _addressInfo.ToRow = toRow == 0 ? ExcelPackage.MaxRows : toRow; @@ -39,11 +39,17 @@ public override CompileResult Compile() { if (_addressInfo.IsSingleCell) { - - var ws = Context.Package.Workbook.GetWorksheetByIndexInList(_addressInfo.WorksheetIx); - var v = ws.GetValue(_addressInfo.FromRow, _addressInfo.FromCol); //Use GetValue to get richtext values. - _cachedCompileResult = CompileResultFactory.Create(v, _addressInfo); - _cachedCompileResult.IsHiddenCell = ws.IsRowHidden(_addressInfo.FromRow); + if (_addressInfo.WorksheetIx == -1) + { + _cachedCompileResult = CompileResult.GetErrorResult(eErrorType.Ref); + } + else + { + var ws = Context.Package.Workbook.GetWorksheetByIndexInList(_addressInfo.WorksheetIx); + var v = ws.GetValue(_addressInfo.FromRow, _addressInfo.FromCol); //Use GetValue to get richtext values. + _cachedCompileResult = CompileResultFactory.Create(v, _addressInfo); + _cachedCompileResult.IsHiddenCell = ws.IsRowHidden(_addressInfo.FromRow); + } } else { diff --git a/src/EPPlusTest/FormulaParsing/IntegrationTests/FormulaParserTests.cs b/src/EPPlusTest/FormulaParsing/IntegrationTests/FormulaParserTests.cs index d26eb0b90..2e9b5692a 100644 --- a/src/EPPlusTest/FormulaParsing/IntegrationTests/FormulaParserTests.cs +++ b/src/EPPlusTest/FormulaParsing/IntegrationTests/FormulaParserTests.cs @@ -73,5 +73,20 @@ string QStr(string s) char quotechar = '\"'; return $"{quotechar}{s}{quotechar}"; } + [TestMethod] + public void ReferencingWorksheetThatDoesNotExistShouldReturnRef() + { + using (ExcelPackage p = new ExcelPackage()) + { + ExcelWorksheet ws = p.Workbook.Worksheets.Add("sheet1"); + + ws.Cells["A1"].Formula = "Sheet2!A1"; + + ws.Calculate(); + + var value = ws.Cells["A1"].Value; + Assert.AreEqual(ErrorValues.RefError, value); + } + } } }