Skip to content

Commit

Permalink
Fixed an issue where ExcelRangeBase.Value returned the RichText objec…
Browse files Browse the repository at this point in the history
…t instead of an empty string if the RichText collection was empty.
  • Loading branch information
JanKallman committed Apr 8, 2024
1 parent 9dffadd commit 70c22fc
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 9 deletions.
8 changes: 7 additions & 1 deletion src/EPPlus/ExcelRangeBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1279,7 +1279,13 @@ public bool IsRichText
get
{
IsRangeValid("richtext");
return _worksheet._flags.GetFlagValue(_fromRow, _fromCol, CellFlags.RichText);
var isRt = _worksheet._flags.GetFlagValue(_fromRow, _fromCol, CellFlags.RichText);
if (isRt)
{
_rtc = _worksheet.GetRichText(_fromRow, _fromCol, this);
return _rtc.Count>0;
}
return isRt;
}
set
{
Expand Down
8 changes: 3 additions & 5 deletions src/EPPlus/ExcelWorksheet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1443,15 +1443,14 @@ private void LoadHyperLinks(XmlReader xr)
internal ExcelRichTextCollection GetRichText(int row, int col, ExcelRangeBase r = null)
{
var v = GetCoreValueInner(row, col);
//var isRt = _flags.GetFlagValue(row, col, CellFlags.RichText);
//if (isRt && v._value is ExcelRichTextCollection rtc)
if (v._value is ExcelRichTextCollection rtc)
var isRt = _flags.GetFlagValue(row, col, CellFlags.RichText);
if (isRt && v._value is ExcelRichTextCollection rtc)
{
return rtc;
}
else
{
var text = ValueToTextHandler.GetFormattedText(v._value, Workbook, v._styleId, false);
var text = ValueToTextHandler.GetFormattedText(v._value, Workbook, v._styleId, false);
if (string.IsNullOrEmpty(text))
{
var item = new ExcelRichTextCollection(Workbook, r);
Expand Down Expand Up @@ -2230,7 +2229,6 @@ public object GetValue(int Row, int Column)
var v = GetValueInner(Row, Column);
if (v!=null)
{
//var cell = ((ExcelCell)_cells[cellID]);
if (_flags.GetFlagValue(Row, Column, CellFlags.RichText))
{
return (object)Cells[Row, Column].RichText.Text;
Expand Down
5 changes: 3 additions & 2 deletions src/EPPlus/Style/RichText/ExcelRichTextCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ internal ExcelRichTextCollection(ExcelWorkbook wb, ExcelRangeBase cells)
{
_wb = wb;
_cells = cells;
}
_cells._worksheet._flags.SetFlagValue(_cells._fromRow, _cells._fromCol, true, CellFlags.RichText);
}

internal ExcelRichTextCollection(string s, ExcelRangeBase cells)
internal ExcelRichTextCollection(string s, ExcelRangeBase cells)
{
_wb = cells._workbook;
_cells = cells;
Expand Down
12 changes: 11 additions & 1 deletion src/EPPlusTest/Core/Range/RangeRichTextTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,18 @@ public void IsRichTextShouldKeepValues()

Thread.CurrentThread.CurrentCulture = ci;
}

[TestMethod]
public void VerifyRichTextIsBlankIfAccess()
{
using(var p=new ExcelPackage())
{
var ws = p.Workbook.Worksheets.Add("Sheet1");
var t = ws.Cells["A1"].RichText.Text;

Assert.AreEqual(string.Empty, ws.Cells["A1"].Value);
}
}
[TestMethod]
public void ValidateRichText_TextIsReflectedOnRemove()
{
var package = new OfficeOpenXml.ExcelPackage();
Expand Down

0 comments on commit 70c22fc

Please sign in to comment.