Skip to content

Commit

Permalink
Added fix for empty worksheet (#1463)
Browse files Browse the repository at this point in the history
* Added fix for empty worksheet

* Fixed short minValue to int MinValue
  • Loading branch information
OssianEPPlus authored May 27, 2024
1 parent d3ac107 commit 306eb90
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/EPPlus/FormulaParsing/FormulaExpressions/RangeExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
}

0 comments on commit 306eb90

Please sign in to comment.