Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/EPPlus/ExcelRangeBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2404,7 +2404,6 @@ private void DeleteThreadedComments(ExcelAddressBase Range)
/// </summary>
public void Dispose()
{
//_worksheet = null;
}

#endregion
Expand Down
6 changes: 5 additions & 1 deletion src/EPPlus/ExcelWorksheet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3617,7 +3617,11 @@ internal void SetValueInner(int row, int col, object value)
var styleId = -1;

styleId = GetStyleId(row, col);

if(_flags.GetFlagValue(row, col, CellFlags.RichText))
{
var rtc = _values.GetValue(row, col)._value as ExcelRichTextCollection;
rtc?.Dispose();
}
if (FullPrecision)
{
_values.SetValue(row, col, value, styleId);
Expand Down
32 changes: 24 additions & 8 deletions src/EPPlus/Style/RichText/ExcelRichTextCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@ namespace OfficeOpenXml.Style
/// <summary>
/// Collection of Richtext objects
/// </summary>
public class ExcelRichTextCollection : IEnumerable<ExcelRichText>
public class ExcelRichTextCollection : IEnumerable<ExcelRichText>, IDisposable
{
List<ExcelRichText> _list = new List<ExcelRichText>();
internal ExcelRangeBase _cells = null;
internal ExcelWorkbook _wb;
internal bool _isComment=false;
private bool _isDisposed=false;
internal ExcelRichTextCollection(ExcelWorkbook wb, ExcelRangeBase cells)
{
_wb = wb;
Expand All @@ -51,7 +52,6 @@ internal ExcelRichTextCollection(string s, ExcelRangeBase cells)
Add(s);
}
}

internal ExcelRichTextCollection(ExcelRichTextCollection rtc, ExcelRangeBase cells)
{
_wb = cells._workbook;
Expand Down Expand Up @@ -115,10 +115,20 @@ public ExcelRichText this[int Index]
{
get
{
CheckDeleted();
var item = _list[Index];
return item;
}
}

private void CheckDeleted()
{
if(_isDisposed)
{
throw (new ObjectDisposedException(@"This RichText object has been overwritten by another value and has been disposed. Please use the .RichText object to initiaze a new object, if you want to set the cell/object to a rich text value."));
}
}

/// <summary>
/// Items in the list
/// </summary>
Expand All @@ -137,6 +147,7 @@ public int Count
/// <returns></returns>
public ExcelRichText Add(string Text, bool NewParagraph = false)
{
CheckDeleted();
if (NewParagraph) Text += "\n";
return Insert(_list.Count, Text);
}
Expand All @@ -149,6 +160,7 @@ public ExcelRichText Add(string Text, bool NewParagraph = false)
/// <returns></returns>
public ExcelRichText Insert(int index, string text)
{
CheckDeleted();
if (text == null) throw new ArgumentException("Text can't be null", "text");
var rt = new ExcelRichText(text, this);
rt.PreserveSpace = true;
Expand Down Expand Up @@ -195,7 +207,6 @@ public ExcelRichText Insert(int index, string text)
if (_isComment == false)
{
_cells._worksheet._flags.SetFlagValue(_cells._fromRow, _cells._fromCol, true, CellFlags.RichText);
//_cells.SetIsRichTextFlag(true);
}
}
_list.Insert(index, rt);
Expand All @@ -207,19 +218,16 @@ public ExcelRichText Insert(int index, string text)
/// </summary>
public void Clear()
{
CheckDeleted();
_list.Clear();
if (_cells != null && _isComment == false)
{
_cells.DeleteMe(_cells, false, true, true, true, false, true, false, false, false);
_cells.SetIsRichTextFlag(false);
}
}
/// <summary>
/// Removes an item at the specific index
/// </summary>
/// <param name="Index"></param>
public void RemoveAt(int Index)
{
CheckDeleted();
_list.RemoveAt(Index);
if (_cells != null && _list.Count == 0 && _isComment == false) _cells.SetIsRichTextFlag(false);
}
Expand All @@ -229,6 +237,7 @@ public void RemoveAt(int Index)
/// <param name="Item"></param>
public void Remove(ExcelRichText Item)
{
CheckDeleted();
_list.Remove(Item);
if (_cells != null && _list.Count == 0 && _isComment == false) _cells.SetIsRichTextFlag(false);
}
Expand All @@ -248,6 +257,7 @@ public string Text
}
set
{
CheckDeleted();
if (string.IsNullOrEmpty(value))
{
Clear();
Expand Down Expand Up @@ -312,6 +322,12 @@ System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
return _list.GetEnumerator();
}

public void Dispose()
{
_cells.Dispose();
_isDisposed = true;
}

#endregion
}
}
48 changes: 48 additions & 0 deletions src/EPPlusTest/Core/Range/RangeRichTextTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -211,5 +211,53 @@ public void ValidateRichText_TextIsReflectedOnRemove()
Assert.AreEqual(1, range.RichText.Count);
Assert.AreEqual("1", range.Text); // FAILS as "12"
}
[TestMethod]
public void ValidateRichText_ClearShould()
{
var package = new ExcelPackage();
var ws = package.Workbook.Worksheets.Add("Test");
var rtc = ws.Cells[1, 1].RichText;
var rt1 = rtc.Add("A");
var rt2 = rtc.Add("B");

Assert.IsTrue(ws.Cells[1, 1].IsRichText);

Assert.AreEqual("AB", rtc.Text);
Assert.AreEqual("AB", ws.Cells[1, 1].Text);
Assert.AreEqual("AB", ws.Cells[1, 1].RichText.Text);

rtc.Clear();
Assert.AreEqual("", rtc.Text);
Assert.AreEqual("", ws.Cells[1, 1].Text);
Assert.AreEqual("", ws.Cells[1, 1].RichText.Text);
Assert.IsFalse(ws.Cells[1, 1].IsRichText);
}
[TestMethod]
public void ValidateRichText_ShouldThrowWhenCellHasBeenOverwritten()
{
var package = new ExcelPackage();
var ws = package.Workbook.Worksheets.Add("Test");
var rtc = ws.Cells[1, 1].RichText;
var rt1 = rtc.Add("A");
var rt2 = rtc.Add("B");

ws.Cells[1, 1].Value = "SomeValue";

Assert.ThrowsExactly<ObjectDisposedException>(() =>{
rtc.Add("c");
});

Assert.ThrowsExactly<ObjectDisposedException>(() => {
rtc.Insert(0, "d");
});

Assert.ThrowsExactly<ObjectDisposedException>(() => {
rtc.Clear();
});

Assert.ThrowsExactly<ObjectDisposedException>(() => {
rtc.Text = "ABCD";
});
}
}
}
13 changes: 9 additions & 4 deletions src/EPPlusTest/Issues/LegacyTests/Issues.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4293,10 +4293,15 @@ public void s431()
.Select(p => p as ExcelPicture).ToList();

var pic = pics.First(p => p.Name == "Image_ExistingInventoryImg");
var image = File.ReadAllBytes("c:\\temp\\img1.png");
pic.Image.SetImage(image, ePictureType.Png);
image = File.ReadAllBytes("c:\\temp\\img2.png");
pics[1].Image.SetImage(image, ePictureType.Png);
var img1 = "c:\\temp\\img1.png";
if (File.Exists(img1))
{
var image = File.ReadAllBytes("c:\\temp\\img1.png");
pic.Image.SetImage(image, ePictureType.Png);

image = File.ReadAllBytes("c:\\temp\\img2.png");
pics[1].Image.SetImage(image, ePictureType.Png);
}

SaveAndCleanup(package);
}
Expand Down