Skip to content
Merged
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
4 changes: 2 additions & 2 deletions OpenXmlFormats/Spreadsheet/SharedString/CT_Rst.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ public string XmlText
if (r.t != null)
{
sw.Write("<t");
if(r.t.Contains(' '))
if(r.t.Contains(' ')||r.t.Contains('\n'))
sw.Write(" xml:space=\"preserve\"");
sw.Write(">");
sw.Write(XmlHelper.EncodeXml(r.t));
Expand All @@ -181,7 +181,7 @@ public string XmlText
if (this.t != null)
{
sw.Write("<t");
if (t.Contains(' '))
if (t.Contains(' ') || t.Contains('\n'))
sw.Write(" xml:space=\"preserve\"");
sw.Write(">");
sw.Write(XmlHelper.EncodeXml(this.t));
Expand Down
6 changes: 4 additions & 2 deletions main/HSSF/UserModel/HSSFCell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ namespace NPOI.HSSF.UserModel
using System.Globalization;
using System.Collections.Generic;
using NPOI.SS.Formula.Eval;
using NPOI.Util;

/// <summary>
/// High level representation of a cell in a row of a spReadsheet.
Expand Down Expand Up @@ -100,7 +101,7 @@ public HSSFCell(HSSFWorkbook book, HSSFSheet sheet, int row, short col,
CellType type)
{
HSSFCell.CheckBounds(col);
cellType = CellType.Unknown; // Force 'SetCellType' to Create a first Record
cellType = CellType._None; // Force 'SetCellType' to Create a first Record
stringValue = null;
this.book = book;
this._sheet = sheet;
Expand Down Expand Up @@ -403,7 +404,7 @@ private void SetCellType(CellType cellType, bool setValue, int row, int col, sho
throw new InvalidOperationException("Invalid cell type: " + cellType);
}
if (cellType != this.cellType &&
this.cellType != CellType.Unknown) // Special Value to indicate an Uninitialized Cell
this.cellType != CellType._None) // Special Value to indicate an Uninitialized Cell
{
_sheet.Sheet.ReplaceValueRecord(_record);
}
Expand Down Expand Up @@ -1458,6 +1459,7 @@ internal void NotifyArrayFormulaChanging()
}

[Obsolete("Will be removed at NPOI 2.8, Use CachedFormulaResultType instead.")]
[Removal(Version = "4.2")]
public CellType GetCachedFormulaResultTypeEnum()
{
throw new NotImplementedException();
Expand Down
54 changes: 33 additions & 21 deletions main/SS/Formula/BaseFormulaEvaluator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public CellValue Evaluate(ICell cell)
* Be aware that your cell value will be Changed to hold the
* result of the formula. If you simply want the formula
* value computed for you, use {@link #EvaluateFormulaCellEnum(Cell)}}
* @param cell
* @param cell The {@code Cell} to evaluate and modify.
* @return the {@code cell} that was passed in, allowing for chained calls
*/

Expand All @@ -144,30 +144,47 @@ public virtual ICell EvaluateInCell(ICell cell)
CellValue cv = EvaluateFormulaCellValue(cell);
SetCellValue(cell, cv);
SetCellType(cell, cv); // cell will no longer be a formula cell

// Due to bug 46479 we should call setCellValue() before setCellType(),
// but bug 61148 showed a case where it would be better the other
// way around, so for now we call setCellValue() a second time to
// handle both cases correctly. There is surely a better way to do this, though...
SetCellValue(cell, cv);
}
return result;
}

protected abstract CellValue EvaluateFormulaCellValue(ICell cell);

/**
* If cell Contains formula, it Evaluates the formula, and saves the result of the formula. The
* cell remains as a formula cell. If the cell does not contain formula, this method returns -1
* and leaves the cell unChanged.
*
* Note that the type of the <em>formula result</em> is returned, so you know what kind of
* cached formula result is also stored with the formula.
* If cell contains formula, it evaluates the formula,
* and saves the result of the formula. The cell
* remains as a formula cell.
* Else if cell does not contain formula, this method leaves
* the cell unchanged.
* Note that the type of the formula result is returned,
* so you know what kind of value is also stored with
* the formula.
* <pre>
* int EvaluatedCellType = Evaluator.EvaluateFormulaCell(cell);
* CellType evaluatedCellType = evaluator.evaluateFormulaCellEnum(cell);
* </pre>
* Be aware that your cell will hold both the formula, and the result. If you want the cell
* Replaced with the result of the formula, use {@link #EvaluateInCell(NPOI.SS.UserModel.Cell)}
* @param cell The cell to Evaluate
* @return -1 for non-formula cells, or the type of the <em>formula result</em>
* Be aware that your cell will hold both the formula,
* and the result. If you want the cell replaced with
* the result of the formula, use {@link #evaluate(org.apache.poi.ss.usermodel.Cell)} }
* @param cell The cell to evaluate
* @return The type of the formula result (the cell's type remains as CellType.FORMULA however)
* If cell is not a formula cell, returns {@link CellType#_NONE} rather than throwing an exception.
*/
public CellType EvaluateFormulaCell(ICell cell)
{
return EvaluateFormulaCellEnum(cell);
if (cell == null || cell.CellType != CellType.Formula)
{
return CellType._None;
}
CellValue cv = EvaluateFormulaCellValue(cell);
// cell remains a formula cell, but the cached value is Changed
SetCellValue(cell, cv);
return cv.CellType;
}

/**
Expand All @@ -190,16 +207,11 @@ public CellType EvaluateFormulaCell(ICell cell)
* If cell is not a formula cell, returns {@link CellType#_NONE} rather than throwing an exception.
* @since POI 3.15 beta 3
*/
[Obsolete("use <c>evaluateFormulaCell(cell)</c> instead")]
[Removal(Version = "4.2")]
public virtual CellType EvaluateFormulaCellEnum(ICell cell)
{
if (cell == null || cell.CellType != CellType.Formula)
{
return CellType.Unknown;
}
CellValue cv = EvaluateFormulaCellValue(cell);
// cell remains a formula cell, but the cached value is Changed
SetCellValue(cell, cv);
return cv.CellType;
return EvaluateFormulaCell(cell);
}

protected static void SetCellType(ICell cell, CellValue cv)
Expand Down
9 changes: 8 additions & 1 deletion main/SS/UserModel/Cell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,16 @@ namespace NPOI.SS.UserModel
{
using System;
using NPOI.SS.Util;
using NPOI.Util;

public enum CellType : int
{
Unknown = -1,
/**
* Unknown type, used to represent a state prior to initialization or the
* lack of a concrete type.
* For internal use only.
*/
_None = -1, /*Internal*/
Numeric = 0,
String = 1,
Formula = 2,
Expand Down Expand Up @@ -296,6 +302,7 @@ CellType CellType
/// Will be renamed to <c>getCachedFormulaResultType()</c> when we make the CellType enum transition in POI 4.0. See bug 59791.
/// </remarks>
[Obsolete("Will be removed at NPOI 2.8, Use CachedFormulaResultType instead.")]
[Removal(Version = "4.2")]
CellType GetCachedFormulaResultTypeEnum();
}
}
Expand Down
37 changes: 29 additions & 8 deletions ooxml/XSSF/Streaming/SXSSFCell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public CellType CachedFormulaResultType
{
get
{
if(_value.GetType() != CellType.Formula)
if(!IsFormulaCell())
{
throw new InvalidOperationException("Only formula cells have cached results");
}
Expand All @@ -97,6 +97,7 @@ public CellType CachedFormulaResultType
}

[Obsolete("Will be removed at NPOI 2.8, Use CachedFormulaResultType instead.")]
[Removal(Version = "4.2")]
public CellType GetCachedFormulaResultTypeEnum()
{
return CachedFormulaResultType;
Expand Down Expand Up @@ -188,12 +189,23 @@ public ICellStyle CellStyle
_style = value;
}
}

private bool IsFormulaCell()
{
return _value is FormulaValue;
}
public CellType CellType
{
get { return _value.GetType(); }
get
{
if(IsFormulaCell())
{
return CellType.Formula;
}
return _value.GetType();
}
}


public int ColumnIndex
{
get
Expand Down Expand Up @@ -448,12 +460,21 @@ public void SetAsActiveCell()

public ICell SetCellErrorValue(byte value)
{
//ensure type garuntees that the type is error so the if condition is never true.
EnsureType(CellType.Error);
if (_value.GetType() == CellType.Formula)
((ErrorFormulaValue)_value).PreEvaluatedValue = value;
// for formulas, we want to keep the type and only have an ERROR as formula value
if(_value.GetType()==CellType.Formula)
{
// ensure that the type is "ERROR"
SetFormulaType(CellType.Error);

// populate the value
((ErrorFormulaValue) _value).PreEvaluatedValue = value;
}
else
((ErrorValue)_value).Value = value;
{
EnsureType(CellType.Error);
((ErrorValue) _value).Value = value;
}

return this;
}

Expand Down
5 changes: 4 additions & 1 deletion ooxml/XSSF/Streaming/SXSSFEvaluationCell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ limitations under the License.
using NPOI.SS.Formula;
using NPOI.SS.UserModel;
using NPOI.SS.Util;
using NPOI.Util;
using NPOI.XSSF.UserModel;

namespace NPOI.XSSF.Streaming
Expand Down Expand Up @@ -80,7 +81,8 @@ public CellType CellType
* @deprecated POI 3.15 beta 3.
* Will be deleted when we make the CellType enum transition. See bug 59791.
*/

[Obsolete("use CellType instead")]
[Removal(Version = "4.2")]
public CellType CellTypeEnum
{
get
Expand Down Expand Up @@ -165,6 +167,7 @@ public CellType CachedFormulaResultType
}

[Obsolete("Will be removed at NPOI 2.8, Use CachedFormulaResultType instead.")]
[Removal(Version = "4.2")]
public CellType GetCachedFormulaResultTypeEnum()
{
return _cell.GetCachedFormulaResultTypeEnum();
Expand Down
12 changes: 12 additions & 0 deletions ooxml/XSSF/Streaming/SXSSFFormulaEvaluator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,18 @@ public static SXSSFFormulaEvaluator Create(SXSSFWorkbook workbook, IStabilityCla
return new SXSSFFormulaEvaluator(workbook, stabilityClassifier, udfFinder);
}

public override void NotifySetFormula(ICell cell)
{
_bookEvaluator.NotifyUpdateCell(new SXSSFEvaluationCell((SXSSFCell)cell));
}
public override void NotifyDeleteCell(ICell cell)
{
_bookEvaluator.NotifyDeleteCell(new SXSSFEvaluationCell((SXSSFCell)cell));
}
public override void NotifyUpdateCell(ICell cell)
{
_bookEvaluator.NotifyUpdateCell(new SXSSFEvaluationCell((SXSSFCell)cell));
}
protected override IEvaluationCell ToEvaluationCell(ICell cell)
{
if (cell is not SXSSFCell sxssfCell)
Expand Down
12 changes: 0 additions & 12 deletions ooxml/XSSF/UserModel/BaseXSSFFormulaEvaluator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,6 @@ protected override IRichTextString CreateRichTextString(String str)
{
return new XSSFRichTextString(str);
}
public override void NotifySetFormula(ICell cell)
{
_bookEvaluator.NotifyUpdateCell(new XSSFEvaluationCell((XSSFCell)cell));
}
public override void NotifyDeleteCell(ICell cell)
{
_bookEvaluator.NotifyDeleteCell(new XSSFEvaluationCell((XSSFCell)cell));
}
public override void NotifyUpdateCell(ICell cell)
{
_bookEvaluator.NotifyUpdateCell(new XSSFEvaluationCell((XSSFCell)cell));
}

/**
* Turns a XSSFCell / SXSSFCell into a XSSFEvaluationCell
Expand Down
1 change: 1 addition & 0 deletions ooxml/XSSF/UserModel/XSSFCell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1415,6 +1415,7 @@ public ICell CopyCellTo(int targetIndex)
}

[Obsolete("Will be removed at NPOI 2.8, Use CachedFormulaResultType instead.")]
[Removal(Version = "4.2")]
public CellType GetCachedFormulaResultTypeEnum()
{
if(!IsFormulaCell)
Expand Down
12 changes: 12 additions & 0 deletions ooxml/XSSF/UserModel/XSSFFormulaEvaluator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,18 @@ public static XSSFFormulaEvaluator Create(XSSFWorkbook workbook, IStabilityClass
return new XSSFFormulaEvaluator(workbook, stabilityClassifier, udfFinder);
}

public override void NotifySetFormula(ICell cell)
{
_bookEvaluator.NotifyUpdateCell(new XSSFEvaluationCell((XSSFCell)cell));
}
public override void NotifyDeleteCell(ICell cell)
{
_bookEvaluator.NotifyDeleteCell(new XSSFEvaluationCell((XSSFCell)cell));
}
public override void NotifyUpdateCell(ICell cell)
{
_bookEvaluator.NotifyUpdateCell(new XSSFEvaluationCell((XSSFCell)cell));
}
/**
* Loops over all cells in all sheets of the supplied
* workbook.
Expand Down
10 changes: 5 additions & 5 deletions testcases/main/HSSF/UserModel/TestHSSFCell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public void TestCachedTypeChange()
if (recs.Length == 28 && recs[23] is StringRecord)
{
wb.Close();
throw new AssertionException("Identified bug - leftover StringRecord");
Assert.Fail("Identified bug - leftover StringRecord");
}
ConfirmStringRecord(sheet, false);

Expand Down Expand Up @@ -388,13 +388,13 @@ public void TestCellStyleWorkbookMatch()
try
{
styA.VerifyBelongsToWorkbook(wbB);
Assert.Fail();
Assert.Fail("expected ArgumentException");
}
catch (ArgumentException) { }
try
{
styB.VerifyBelongsToWorkbook(wbA);
Assert.Fail();
Assert.Fail("expected ArgumentException");
}
catch (ArgumentException) { }

Expand All @@ -406,13 +406,13 @@ public void TestCellStyleWorkbookMatch()
try
{
cellA.CellStyle = (styB);
Assert.Fail();
Assert.Fail("expected ArgumentException");
}
catch (ArgumentException) { }
try
{
cellB.CellStyle = (styA);
Assert.Fail();
Assert.Fail("expected ArgumentException");
}
catch (ArgumentException) { }

Expand Down
Loading
Loading