diff --git a/OpenXmlFormats/Spreadsheet/SharedString/CT_Rst.cs b/OpenXmlFormats/Spreadsheet/SharedString/CT_Rst.cs index a02b5232b..a7b4da035 100644 --- a/OpenXmlFormats/Spreadsheet/SharedString/CT_Rst.cs +++ b/OpenXmlFormats/Spreadsheet/SharedString/CT_Rst.cs @@ -168,7 +168,7 @@ public string XmlText if (r.t != null) { sw.Write(""); sw.Write(XmlHelper.EncodeXml(r.t)); @@ -181,7 +181,7 @@ public string XmlText if (this.t != null) { sw.Write(""); sw.Write(XmlHelper.EncodeXml(this.t)); diff --git a/main/HSSF/UserModel/HSSFCell.cs b/main/HSSF/UserModel/HSSFCell.cs index 428d4297e..79847db8e 100644 --- a/main/HSSF/UserModel/HSSFCell.cs +++ b/main/HSSF/UserModel/HSSFCell.cs @@ -31,6 +31,7 @@ namespace NPOI.HSSF.UserModel using System.Globalization; using System.Collections.Generic; using NPOI.SS.Formula.Eval; + using NPOI.Util; /// /// High level representation of a cell in a row of a spReadsheet. @@ -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; @@ -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); } @@ -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(); diff --git a/main/SS/Formula/BaseFormulaEvaluator.cs b/main/SS/Formula/BaseFormulaEvaluator.cs index e7d56c0e9..63bb02579 100644 --- a/main/SS/Formula/BaseFormulaEvaluator.cs +++ b/main/SS/Formula/BaseFormulaEvaluator.cs @@ -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 */ @@ -144,6 +144,12 @@ 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; } @@ -151,23 +157,34 @@ public virtual ICell EvaluateInCell(ICell cell) 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 formula result 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. *
-         * int EvaluatedCellType = Evaluator.EvaluateFormulaCell(cell);
+         * CellType evaluatedCellType = evaluator.evaluateFormulaCellEnum(cell);
          * 
- * 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 formula result + * 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; } /** @@ -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 evaluateFormulaCell(cell) 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) diff --git a/main/SS/UserModel/Cell.cs b/main/SS/UserModel/Cell.cs index 298a385df..d9ac904c6 100644 --- a/main/SS/UserModel/Cell.cs +++ b/main/SS/UserModel/Cell.cs @@ -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, @@ -296,6 +302,7 @@ CellType CellType /// Will be renamed to getCachedFormulaResultType() when we make the CellType enum transition in POI 4.0. See bug 59791. /// [Obsolete("Will be removed at NPOI 2.8, Use CachedFormulaResultType instead.")] + [Removal(Version = "4.2")] CellType GetCachedFormulaResultTypeEnum(); } } diff --git a/ooxml/XSSF/Streaming/SXSSFCell.cs b/ooxml/XSSF/Streaming/SXSSFCell.cs index 3f0d4ff2c..14e0fc575 100644 --- a/ooxml/XSSF/Streaming/SXSSFCell.cs +++ b/ooxml/XSSF/Streaming/SXSSFCell.cs @@ -87,7 +87,7 @@ public CellType CachedFormulaResultType { get { - if(_value.GetType() != CellType.Formula) + if(!IsFormulaCell()) { throw new InvalidOperationException("Only formula cells have cached results"); } @@ -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; @@ -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 @@ -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; } diff --git a/ooxml/XSSF/Streaming/SXSSFEvaluationCell.cs b/ooxml/XSSF/Streaming/SXSSFEvaluationCell.cs index 69835e7b9..06013d98d 100644 --- a/ooxml/XSSF/Streaming/SXSSFEvaluationCell.cs +++ b/ooxml/XSSF/Streaming/SXSSFEvaluationCell.cs @@ -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 @@ -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 @@ -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(); diff --git a/ooxml/XSSF/Streaming/SXSSFFormulaEvaluator.cs b/ooxml/XSSF/Streaming/SXSSFFormulaEvaluator.cs index 9f1158275..2264082a0 100644 --- a/ooxml/XSSF/Streaming/SXSSFFormulaEvaluator.cs +++ b/ooxml/XSSF/Streaming/SXSSFFormulaEvaluator.cs @@ -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) diff --git a/ooxml/XSSF/UserModel/BaseXSSFFormulaEvaluator.cs b/ooxml/XSSF/UserModel/BaseXSSFFormulaEvaluator.cs index 37171bdaf..bbaa560c7 100644 --- a/ooxml/XSSF/UserModel/BaseXSSFFormulaEvaluator.cs +++ b/ooxml/XSSF/UserModel/BaseXSSFFormulaEvaluator.cs @@ -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 diff --git a/ooxml/XSSF/UserModel/XSSFCell.cs b/ooxml/XSSF/UserModel/XSSFCell.cs index 4fb5dbe14..646e87411 100644 --- a/ooxml/XSSF/UserModel/XSSFCell.cs +++ b/ooxml/XSSF/UserModel/XSSFCell.cs @@ -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) diff --git a/ooxml/XSSF/UserModel/XSSFFormulaEvaluator.cs b/ooxml/XSSF/UserModel/XSSFFormulaEvaluator.cs index 7fdaf4b4f..e970eeff0 100644 --- a/ooxml/XSSF/UserModel/XSSFFormulaEvaluator.cs +++ b/ooxml/XSSF/UserModel/XSSFFormulaEvaluator.cs @@ -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. diff --git a/testcases/main/HSSF/UserModel/TestHSSFCell.cs b/testcases/main/HSSF/UserModel/TestHSSFCell.cs index d391d1e61..72e325f86 100644 --- a/testcases/main/HSSF/UserModel/TestHSSFCell.cs +++ b/testcases/main/HSSF/UserModel/TestHSSFCell.cs @@ -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); @@ -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) { } @@ -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) { } diff --git a/testcases/main/HSSF/UserModel/TestHSSFFormulaEvaluator.cs b/testcases/main/HSSF/UserModel/TestHSSFFormulaEvaluator.cs index 7ecc60e32..41c166883 100644 --- a/testcases/main/HSSF/UserModel/TestHSSFFormulaEvaluator.cs +++ b/testcases/main/HSSF/UserModel/TestHSSFFormulaEvaluator.cs @@ -65,42 +65,6 @@ public void TestEvaluateSimple() wb.Close(); } - /** - * Test for bug due to attempt to convert a cached formula error result to a boolean - */ - [Test] - public override void TestUpdateCachedFormulaResultFromErrorToNumber_bug46479() - { - HSSFWorkbook wb = new HSSFWorkbook(); - HSSFSheet sheet = wb.CreateSheet("Sheet1") as HSSFSheet; - HSSFRow row = sheet.CreateRow(0) as HSSFRow; - HSSFCell cellA1 = row.CreateCell(0) as HSSFCell; - HSSFCell cellB1 = row.CreateCell(1) as HSSFCell; - cellB1.CellFormula = "A1+1"; - HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb); - cellA1.SetCellErrorValue(FormulaError.NAME); - fe.EvaluateFormulaCell(cellB1); - cellA1.SetCellValue(2.5); - fe.NotifyUpdateCell(cellA1); - try - { - fe.EvaluateInCell(cellB1); - } - catch (InvalidOperationException e) - { - if (e.Message.Equals("Cannot get a numeric value from a error formula cell")) - { - Assert.Fail("Identified bug 46479a"); - } - } - ClassicAssert.AreEqual(3.5, cellB1.NumericCellValue, 0.0); - - wb.Close(); - } - - - - /** * When evaluating defined names, POI has to decide whether it is capable. Currently * (May2009) POI only supports simple cell and area refs.
diff --git a/testcases/main/HSSF/Util/TestAreaReference.cs b/testcases/main/HSSF/Util/TestAreaReference.cs index 278c4cbb9..47769720c 100644 --- a/testcases/main/HSSF/Util/TestAreaReference.cs +++ b/testcases/main/HSSF/Util/TestAreaReference.cs @@ -128,19 +128,19 @@ public void TestContiguousReferences() try { new AreaReference(refDCSimple, SpreadsheetVersion.EXCEL97); - Assert.Fail(); + Assert.Fail("expected ArgumentException"); } catch (ArgumentException) { } try { new AreaReference(refDC2D, SpreadsheetVersion.EXCEL97); - Assert.Fail(); + Assert.Fail("expected ArgumentException"); } catch (ArgumentException) { } try { new AreaReference(refDC3D, SpreadsheetVersion.EXCEL97); - Assert.Fail(); + Assert.Fail("expected ArgumentException"); } catch (ArgumentException) { } diff --git a/testcases/main/SS/UserModel/BaseTestBugzillaIssues.cs b/testcases/main/SS/UserModel/BaseTestBugzillaIssues.cs index 641a11c82..3676d0d5c 100644 --- a/testcases/main/SS/UserModel/BaseTestBugzillaIssues.cs +++ b/testcases/main/SS/UserModel/BaseTestBugzillaIssues.cs @@ -1559,27 +1559,25 @@ public virtual void Bug57798() // First cell of array formula, OK int rowId = 0; int cellId = 1; - Console.WriteLine("Reading row " + rowId + ", col " + cellId); + IRow row = sheet.GetRow(rowId); ICell cell = row.GetCell(cellId); - Console.WriteLine("Formula:" + cell.CellFormula); + ClassicAssert.AreEqual("A1", cell.CellFormula); if (CellType.Formula == cell.CellType) { - int formulaResultType = (int)cell.CachedFormulaResultType; - Console.WriteLine("Formual Result Type:" + formulaResultType); + ClassicAssert.AreEqual(CellType.String, cell.CachedFormulaResultType); } // ******************************* // Second cell of array formula, NOT OK for xlsx files rowId = 1; cellId = 1; - Console.WriteLine("Reading row " + rowId + ", col " + cellId); + row = sheet.GetRow(rowId); cell = row.GetCell(cellId); - Console.WriteLine("Formula:" + cell.CellFormula); + ClassicAssert.AreEqual("A1", cell.CellFormula); if (CellType.Formula == cell.CellType) { - int formulaResultType = (int)cell.CachedFormulaResultType; - Console.WriteLine("Formual Result Type:" + formulaResultType); + ClassicAssert.AreEqual(CellType.String, cell.CachedFormulaResultType); } workbook.Close(); } diff --git a/testcases/main/SS/UserModel/BaseTestCell.cs b/testcases/main/SS/UserModel/BaseTestCell.cs index 0f67cafec..df366d625 100644 --- a/testcases/main/SS/UserModel/BaseTestCell.cs +++ b/testcases/main/SS/UserModel/BaseTestCell.cs @@ -742,6 +742,8 @@ public void TestDefaultStyleProperties() style = cell.CellStyle; ClassicAssert.IsFalse(style2.IsLocked); ClassicAssert.IsTrue(style2.IsHidden); + ClassicAssert.IsTrue(style.IsLocked); + ClassicAssert.IsFalse(style.IsHidden); style2.IsLocked = (/*setter*/true); style2.IsHidden = (/*setter*/false); @@ -998,6 +1000,52 @@ public void GetCellComment() wb.Close(); } + [Test] + public void TestSetErrorValue() + { + IWorkbook wb = _testDataProvider.CreateWorkbook(); + ISheet sheet = wb.CreateSheet(); + IRow row = sheet.CreateRow(0); + ICell cell = row.CreateCell(0); + + cell.SetCellFormula("A2"); + cell.SetCellErrorValue(FormulaError.NAME.Code); + + ClassicAssert.AreEqual(CellType.Formula, cell.CellType, + "Should still be a formula even after we set an error value"); + ClassicAssert.AreEqual(CellType.Error, cell.CachedFormulaResultType, + "Should still be a formula even after we set an error value"); + ClassicAssert.AreEqual("A2", cell.CellFormula); + try { + _ = cell.NumericCellValue; + Assert.Fail("Should catch exception here"); + } catch (InvalidOperationException) { + // expected here + } + try { + _ = cell.StringCellValue; + Assert.Fail("Should catch exception here"); + } catch (InvalidOperationException) { + // expected here + } + try { + _ = cell.RichStringCellValue; + Assert.Fail("Should catch exception here"); + } catch (InvalidOperationException) { + // expected here + } + try { + _ = cell.DateCellValue; + Assert.Fail("Should catch exception here"); + } catch (InvalidOperationException) { + // expected here + } + ClassicAssert.AreEqual(FormulaError.NAME.Code, cell.ErrorCellValue); + ClassicAssert.IsNull(cell.Hyperlink); + + wb.Close(); + } + [Test] public void PrimitiveToEnumReplacementDoesNotBreakBackwardsCompatibility() { @@ -1009,11 +1057,11 @@ public void PrimitiveToEnumReplacementDoesNotBreakBackwardsCompatibility() ISheet sheet = wb.CreateSheet(); IRow row = sheet.CreateRow(0); ICell cell = row.CreateCell(0); - // CellType.* -> CellType.* + cell.SetCellValue(5.0); ClassicAssert.AreEqual(CellType.Numeric, cell.CellType); ClassicAssert.AreEqual(0, (int)cell.CellType); - //ClassicAssert.AreEqual(CellType.NUMERIC, cell.GetCellTypeEnum()); // make sure old way and new way are compatible + // make sure switch(int|Enum) still works. Cases must be statically resolvable in1 Java 6 ("constant expression required") switch (cell.CellType) { diff --git a/testcases/main/SS/UserModel/BaseTestFormulaEvaluator.cs b/testcases/main/SS/UserModel/BaseTestFormulaEvaluator.cs index 9e8cadb13..25032bd85 100644 --- a/testcases/main/SS/UserModel/BaseTestFormulaEvaluator.cs +++ b/testcases/main/SS/UserModel/BaseTestFormulaEvaluator.cs @@ -19,6 +19,7 @@ namespace TestCases.SS.UserModel { using NPOI.SS.UserModel; using NUnit.Framework;using NUnit.Framework.Legacy; + using SixLabors.Fonts; using System; using System.Collections.Generic; @@ -288,7 +289,6 @@ public void TestEvaluateBlank() [Test] public virtual void TestUpdateCachedFormulaResultFromErrorToNumber_bug46479() { - IWorkbook wb = _testDataProvider.CreateWorkbook(); ISheet sheet = wb.CreateSheet("Sheet1"); IRow row = sheet.CreateRow(0); @@ -298,6 +298,8 @@ public virtual void TestUpdateCachedFormulaResultFromErrorToNumber_bug46479() IFormulaEvaluator fe = wb.GetCreationHelper().CreateFormulaEvaluator(); cellA1.SetCellErrorValue(FormulaError.NAME.Code); + ClassicAssert.AreEqual(CellType.Error, fe.EvaluateFormulaCell(cellB1)); + ClassicAssert.AreEqual(CellType.Formula, cellB1.CellType); fe.EvaluateFormulaCell(cellB1); cellA1.SetCellValue(2.5); @@ -308,7 +310,8 @@ public virtual void TestUpdateCachedFormulaResultFromErrorToNumber_bug46479() } catch (InvalidOperationException e) { - if (e.Message.Equals("Cannot get a numeric value from a error formula cell")) + if (e.Message.Equals("Cannot get a numeric value from a error formula cell", + StringComparison.OrdinalIgnoreCase)) { Assert.Fail("Identified bug 46479a"); } @@ -354,6 +357,388 @@ public void EvaluateInCellReturnsSameCell() wb.Close(); } + [Test] + public void TestBug61148() + { + IWorkbook wb = _testDataProvider.CreateWorkbook(); + ICell cell = wb.CreateSheet().CreateRow(0).CreateCell(0); + cell.SetCellFormula("1+2"); + + ClassicAssert.AreEqual(0, (int)cell.NumericCellValue); + ClassicAssert.AreEqual("1+2", cell.ToString()); + + IFormulaEvaluator eval = wb.GetCreationHelper().CreateFormulaEvaluator(); + + eval.EvaluateInCell(cell); + + ClassicAssert.AreEqual("3", cell.ToString()); + } + + [Test] + public void TestMultisheetFormulaEval() + { + IWorkbook wb = _testDataProvider.CreateWorkbook(); + try + { + ISheet sheet1 = wb.CreateSheet("Sheet1"); + ISheet sheet2 = wb.CreateSheet("Sheet2"); + ISheet sheet3 = wb.CreateSheet("Sheet3"); + + // sheet1 A1 + ICell cell = sheet1.CreateRow(0).CreateCell(0); + cell.SetCellType(CellType.Numeric); + cell.SetCellValue(1.0); + + // sheet2 A1 + cell = sheet2.CreateRow(0).CreateCell(0); + cell.SetCellType(CellType.Numeric); + cell.SetCellValue(1.0); + + // sheet2 B1 + cell = sheet2.GetRow(0).CreateCell(1); + cell.SetCellType(CellType.Numeric); + cell.SetCellValue(1.0); + + // sheet3 A1 + cell = sheet3.CreateRow(0).CreateCell(0); + cell.SetCellType(CellType.Numeric); + cell.SetCellValue(1.0); + + // sheet1 A2 formulae + cell = sheet1.CreateRow(1).CreateCell(0); + cell.SetCellType(CellType.Formula); + cell.CellFormula = (/*setter*/"SUM(Sheet1:Sheet3!A1)"); + + // sheet1 A3 formulae + cell = sheet1.CreateRow(2).CreateCell(0); + cell.SetCellType(CellType.Formula); + cell.CellFormula = (/*setter*/"SUM(Sheet1:Sheet3!A1:B1)"); + + wb.GetCreationHelper().CreateFormulaEvaluator().EvaluateAll(); + + cell = sheet1.GetRow(1).GetCell(0); + ClassicAssert.AreEqual(3.0, cell.NumericCellValue, 0); + + cell = sheet1.GetRow(2).GetCell(0); + ClassicAssert.AreEqual(4.0, cell.NumericCellValue, 0); + } + finally + { + wb.Close(); + } + } + + [Test] + public void TestBug55843() + { + IWorkbook wb = _testDataProvider.CreateWorkbook(); + try + { + ISheet sheet = wb.CreateSheet("test"); + IRow row = sheet.CreateRow(0); + IRow row2 = sheet.CreateRow(1); + ICell cellA2 = row2.CreateCell(0, CellType.Formula); + ICell cellB1 = row.CreateCell(1, CellType.Numeric); + cellB1.SetCellValue(10); + IFormulaEvaluator formulaEvaluator = wb.GetCreationHelper().CreateFormulaEvaluator(); + cellA2.SetCellFormula("IF(B1=0,\"\",((ROW()-ROW(A$1))*12))"); + CellValue Evaluate = formulaEvaluator.Evaluate(cellA2); + System.Console.WriteLine(Evaluate); + ClassicAssert.AreEqual("12", Evaluate.FormatAsString()); + + cellA2.CellFormula = (/*setter*/"IF(NOT(B1=0),((ROW()-ROW(A$1))*12),\"\")"); + CellValue EvaluateN = formulaEvaluator.Evaluate(cellA2); + System.Console.WriteLine(EvaluateN); + + ClassicAssert.AreEqual(Evaluate.ToString(), EvaluateN.ToString()); + ClassicAssert.AreEqual("12", EvaluateN.FormatAsString()); + } + finally + { + wb.Close(); + } + } + + [Test] + public void TestBug55843a() + { + IWorkbook wb = _testDataProvider.CreateWorkbook(); + try + { + ISheet sheet = wb.CreateSheet("test"); + IRow row = sheet.CreateRow(0); + IRow row2 = sheet.CreateRow(1); + ICell cellA2 = row2.CreateCell(0, CellType.Formula); + ICell cellB1 = row.CreateCell(1, CellType.Numeric); + cellB1.SetCellValue(10); + IFormulaEvaluator formulaEvaluator = wb.GetCreationHelper().CreateFormulaEvaluator(); + cellA2.SetCellFormula("IF(B1=0,\"\",((ROW(A$1))))"); + CellValue Evaluate = formulaEvaluator.Evaluate(cellA2); + System.Console.WriteLine(Evaluate); + ClassicAssert.AreEqual("1", Evaluate.FormatAsString()); + + cellA2.CellFormula = (/*setter*/"IF(NOT(B1=0),((ROW(A$1))),\"\")"); + CellValue EvaluateN = formulaEvaluator.Evaluate(cellA2); + System.Console.WriteLine(EvaluateN); + + ClassicAssert.AreEqual(Evaluate.ToString(), EvaluateN.ToString()); + ClassicAssert.AreEqual("1", EvaluateN.FormatAsString()); + } + finally + { + wb.Close(); + } + } + + [Test] + public void TestBug55843b() + { + IWorkbook wb = _testDataProvider.CreateWorkbook(); + try + { + ISheet sheet = wb.CreateSheet("test"); + IRow row = sheet.CreateRow(0); + IRow row2 = sheet.CreateRow(1); + ICell cellA2 = row2.CreateCell(0, CellType.Formula); + ICell cellB1 = row.CreateCell(1, CellType.Numeric); + cellB1.SetCellValue(10); + IFormulaEvaluator formulaEvaluator = wb.GetCreationHelper().CreateFormulaEvaluator(); + + cellA2.SetCellFormula("IF(B1=0,\"\",((ROW())))"); + CellValue Evaluate = formulaEvaluator.Evaluate(cellA2); + System.Console.WriteLine(Evaluate); + ClassicAssert.AreEqual("2", Evaluate.FormatAsString()); + + cellA2.CellFormula = (/*setter*/"IF(NOT(B1=0),((ROW())),\"\")"); + CellValue EvaluateN = formulaEvaluator.Evaluate(cellA2); + System.Console.WriteLine(EvaluateN); + + ClassicAssert.AreEqual(Evaluate.ToString(), EvaluateN.ToString()); + ClassicAssert.AreEqual("2", EvaluateN.FormatAsString()); + } + finally + { + wb.Close(); + } + } + + [Test] + public void TestBug55843c() + { + IWorkbook wb = _testDataProvider.CreateWorkbook(); + try + { + ISheet sheet = wb.CreateSheet("test"); + IRow row = sheet.CreateRow(0); + IRow row2 = sheet.CreateRow(1); + ICell cellA2 = row2.CreateCell(0, CellType.Formula); + ICell cellB1 = row.CreateCell(1, CellType.Numeric); + cellB1.SetCellValue(10); + IFormulaEvaluator formulaEvaluator = wb.GetCreationHelper().CreateFormulaEvaluator(); + + cellA2.CellFormula = (/*setter*/"IF(NOT(B1=0),((ROW())))"); + CellValue EvaluateN = formulaEvaluator.Evaluate(cellA2); + System.Console.WriteLine(EvaluateN); + ClassicAssert.AreEqual("2", EvaluateN.FormatAsString()); + } + finally + { + wb.Close(); + } + } + + [Test] + public void TestBug55843d() + { + IWorkbook wb = _testDataProvider.CreateWorkbook(); + try + { + ISheet sheet = wb.CreateSheet("test"); + IRow row = sheet.CreateRow(0); + IRow row2 = sheet.CreateRow(1); + ICell cellA2 = row2.CreateCell(0, CellType.Formula); + ICell cellB1 = row.CreateCell(1, CellType.Numeric); + cellB1.SetCellValue(10); + IFormulaEvaluator formulaEvaluator = wb.GetCreationHelper().CreateFormulaEvaluator(); + + cellA2.CellFormula = (/*setter*/"IF(NOT(B1=0),((ROW())),\"\")"); + CellValue EvaluateN = formulaEvaluator.Evaluate(cellA2); + System.Console.WriteLine(EvaluateN); + ClassicAssert.AreEqual("2", EvaluateN.FormatAsString()); + } + finally + { + wb.Close(); + } + } + + [Test] + public void TestBug55843e() + { + IWorkbook wb = _testDataProvider.CreateWorkbook(); + try + { + ISheet sheet = wb.CreateSheet("test"); + IRow row = sheet.CreateRow(0); + IRow row2 = sheet.CreateRow(1); + ICell cellA2 = row2.CreateCell(0, CellType.Formula); + ICell cellB1 = row.CreateCell(1, CellType.Numeric); + cellB1.SetCellValue(10); + IFormulaEvaluator formulaEvaluator = wb.GetCreationHelper().CreateFormulaEvaluator(); + + cellA2.SetCellFormula("IF(B1=0,\"\",((ROW())))"); + CellValue Evaluate = formulaEvaluator.Evaluate(cellA2); + System.Console.WriteLine(Evaluate); + ClassicAssert.AreEqual("2", Evaluate.FormatAsString()); + } + finally + { + wb.Close(); + } + } + + + [Test] + public void TestBug55843f() + { + IWorkbook wb = _testDataProvider.CreateWorkbook(); + try + { + ISheet sheet = wb.CreateSheet("test"); + IRow row = sheet.CreateRow(0); + IRow row2 = sheet.CreateRow(1); + ICell cellA2 = row2.CreateCell(0, CellType.Formula); + ICell cellB1 = row.CreateCell(1, CellType.Numeric); + cellB1.SetCellValue(10); + IFormulaEvaluator formulaEvaluator = wb.GetCreationHelper().CreateFormulaEvaluator(); + + cellA2.SetCellFormula("IF(B1=0,\"\",IF(B1=10,3,4))"); + CellValue Evaluate = formulaEvaluator.Evaluate(cellA2); + System.Console.WriteLine(Evaluate); + ClassicAssert.AreEqual("3", Evaluate.FormatAsString()); + } + finally + { + wb.Close(); + } + } + + [Test] + public void TestBug56655() + { + IWorkbook wb = _testDataProvider.CreateWorkbook(); + try + { + ISheet sheet = wb.CreateSheet(); + + SetCellFormula(sheet, 0, 0, "#VALUE!"); + SetCellFormula(sheet, 0, 1, "SUMIFS(A:A,A:A,#VALUE!)"); + + wb.GetCreationHelper().CreateFormulaEvaluator().EvaluateAll(); + + ClassicAssert.AreEqual(CellType.Error, GetCell(sheet, 0, 0).CachedFormulaResultType); + ClassicAssert.AreEqual(FormulaError.VALUE.Code, GetCell(sheet, 0, 0).ErrorCellValue); + ClassicAssert.AreEqual(CellType.Error, GetCell(sheet, 0, 1).CachedFormulaResultType); + ClassicAssert.AreEqual(FormulaError.VALUE.Code, GetCell(sheet, 0, 1).ErrorCellValue); + } + catch(Exception) + { + wb.Close(); + } + } + + [Test] + public void TestBug56655a() + { + IWorkbook wb = _testDataProvider.CreateWorkbook(); + try + { + ISheet sheet = wb.CreateSheet(); + + SetCellFormula(sheet, 0, 0, "B1*C1"); + sheet.GetRow(0).CreateCell(1).SetCellValue("A"); + SetCellFormula(sheet, 1, 0, "B1*C1"); + sheet.GetRow(1).CreateCell(1).SetCellValue("A"); + SetCellFormula(sheet, 0, 3, "SUMIFS(A:A,A:A,A2)"); + + wb.GetCreationHelper().CreateFormulaEvaluator().EvaluateAll(); + + ClassicAssert.AreEqual(CellType.Error, GetCell(sheet, 0, 0).CachedFormulaResultType); + ClassicAssert.AreEqual(FormulaError.VALUE.Code, GetCell(sheet, 0, 0).ErrorCellValue); + ClassicAssert.AreEqual(CellType.Error, GetCell(sheet, 1, 0).CachedFormulaResultType); + ClassicAssert.AreEqual(FormulaError.VALUE.Code, GetCell(sheet, 1, 0).ErrorCellValue); + ClassicAssert.AreEqual(CellType.Error, GetCell(sheet, 0, 3).CachedFormulaResultType); + ClassicAssert.AreEqual(FormulaError.VALUE.Code, GetCell(sheet, 0, 3).ErrorCellValue); + } + catch(Exception) + { + wb.Close(); + } + } + + /// + /// + /// 0-based + /// 0-based + private void SetCellFormula(ISheet sheet, int row, int column, string formula) + { + IRow r = sheet.GetRow(row); + if (r == null) + { + r = sheet.CreateRow(row); + } + ICell cell = r.GetCell(column); + if (cell == null) + { + cell = r.CreateCell(column); + } + cell.SetCellType(CellType.Formula); + cell.SetCellFormula(formula); + } + + /// + /// + /// 0-based + /// 0-based + private ICell GetCell(ISheet sheet, int rowNo, int column) + { + return sheet.GetRow(rowNo).GetCell(column); + } + + [Test] + public void TestBug61532() + { + IWorkbook wb = _testDataProvider.CreateWorkbook(); + try + { + ICell cell = wb.CreateSheet().CreateRow(0).CreateCell(0); + cell.SetCellFormula("1+2"); + + ClassicAssert.AreEqual(0, (int)cell.NumericCellValue); + ClassicAssert.AreEqual("1+2", cell.ToString()); + + IFormulaEvaluator eval = wb.GetCreationHelper().CreateFormulaEvaluator(); + + CellValue value = eval.Evaluate(cell); + + ClassicAssert.AreEqual(CellType.Numeric, value.CellType); + ClassicAssert.AreEqual(3.0, value.NumberValue, 0.01); + ClassicAssert.AreEqual(CellType.Formula, cell.CellType); + ClassicAssert.AreEqual("1+2", cell.CellFormula); + ClassicAssert.AreEqual("1+2", cell.ToString()); + + ClassicAssert.IsNotNull(eval.EvaluateInCell(cell)); + + ClassicAssert.AreEqual("3", cell.ToString()); + ClassicAssert.AreEqual(CellType.Numeric, cell.CellType); + ClassicAssert.AreEqual(3.0, cell.NumericCellValue, 0.01); + } + catch(Exception) + { + wb.Close(); + } + } + public void BaseTestNPOIIssue_1057(string paramsFile, string installFile) { DataFormatter formatter = new DataFormatter(); diff --git a/testcases/ooxml/SS/Formula/Functions/TestProper.cs b/testcases/ooxml/SS/Formula/Functions/TestProper.cs index 3e3ae6d80..8ee13dc30 100644 --- a/testcases/ooxml/SS/Formula/Functions/TestProper.cs +++ b/testcases/ooxml/SS/Formula/Functions/TestProper.cs @@ -132,8 +132,8 @@ public void TestMicroBenchmark() ValueEval ret = TextFunction.PROPER.Evaluate(new ValueEval[] { strArg }, 0, 0); ClassicAssert.AreEqual("Some Longer Text That Needs A Number Of Replacements To Check For Runtime Of Different Implementations", ((StringEval)ret).StringValue); } - // Took aprox. 600ms on a decent Laptop in July 2016 - Console.WriteLine("Took: " + (TimeUtil.CurrentMillis() - start) + "ms"); + // Took approx. 600ms on a decent Laptop in July 2016 + //System.out.println("Took: " + (System.currentTimeMillis() - start) + "ms"); } private void checkProper(String input, String expected) { diff --git a/testcases/ooxml/XSSF/Model/TestCommentsTable.cs b/testcases/ooxml/XSSF/Model/TestCommentsTable.cs index e0061dff1..6ed500865 100644 --- a/testcases/ooxml/XSSF/Model/TestCommentsTable.cs +++ b/testcases/ooxml/XSSF/Model/TestCommentsTable.cs @@ -264,7 +264,6 @@ public void Bug54920() // private static void setComment(ISheet sheet, ICell cell, IDrawing drawing, String commentText, ICreationHelper helper, IClientAnchor anchor) { - //System.out.println("Setting col: " + cell.getColumnIndex() + " and row " + cell.getRowIndex()); anchor.Col1 = (cell.ColumnIndex); anchor.Col2 = (cell.ColumnIndex); anchor.Row1 = (cell.RowIndex); diff --git a/testcases/ooxml/XSSF/Streaming/TestAutoSizeColumnTracker.cs b/testcases/ooxml/XSSF/Streaming/TestAutoSizeColumnTracker.cs index 4de91feea..6f87fc968 100644 --- a/testcases/ooxml/XSSF/Streaming/TestAutoSizeColumnTracker.cs +++ b/testcases/ooxml/XSSF/Streaming/TestAutoSizeColumnTracker.cs @@ -196,8 +196,6 @@ private void CheckColumnWidth(int expectedWidth, int column, bool useMergedCells private static void assumeRequiredFontsAreInstalled(IWorkbook workbook, ICell cell) { // autoSize will fail if required fonts are not installed, skip this test then IFont font = workbook.GetFontAt(cell.CellStyle.FontIndex); - //System.out.Println(font.FontHeightInPoints); - //System.out.Println(font.FontName); Assume.That(SheetUtil.CanComputeColumnWidth(font), "Cannot verify autoSizeColumn() because the necessary Fonts are not installed on this machine: " + font); } diff --git a/testcases/ooxml/XSSF/Streaming/TestSXSSFCell.cs b/testcases/ooxml/XSSF/Streaming/TestSXSSFCell.cs index 35421ab2f..3053313c9 100644 --- a/testcases/ooxml/XSSF/Streaming/TestSXSSFCell.cs +++ b/testcases/ooxml/XSSF/Streaming/TestSXSSFCell.cs @@ -31,7 +31,7 @@ namespace TestCases.XSSF.Streaming * Tests various functionality having to do with {@link SXSSFCell}. For instance support for * particular datatypes, etc. */ - [Ignore("This may cause file access denied")] + //[Ignore("This may cause file access denied")] public class TestSXSSFCell : BaseTestXCell { @@ -47,14 +47,15 @@ public static void TearDown() { } [Test] - public void TestPreserveSpaces() { + public void TestPreserveSpaces() + { String[] samplesWithSpaces = { " POI", "POI ", " POI ", "\nPOI", "\n\nPOI \n", - }; + }; foreach (String str in samplesWithSpaces) { using(var swb = _testDataProvider.CreateWorkbook()) @@ -69,18 +70,27 @@ public void TestPreserveSpaces() { XSSFCell xCell = xwb.GetSheetAt(0).GetRow(0).GetCell(0) as XSSFCell; CT_Rst is1 = xCell.GetCTCell().@is; + ClassicAssert.IsNotNull(is1); //XmlCursor c = is1.NewCursor(); //c.ToNextToken(); //String t = c.GetAttributeText(new QName("http://www.w3.org/XML/1998/namespace", "space")); //c.Dispose(); - + ClassicAssert.IsTrue(is1.XmlText.Contains("xml:space=\"preserve\"")); //write is1 to xml stream writer ,get the xml text and parse it and get space attr. //ClassicAssert.AreEqual("preserve", t, "expected xml:spaces=\"preserve\" \"" + str + "\""); } } } } + + [Test] + public void TestSetErrorValeFormula() + { + Assume.That(false, "This test is disabled because it fails for SXSSF because " + + "handling of errors in formulas is slightly different than in XSSF, " + + "but this proved to be non-trivial to solve..."); + } } } \ No newline at end of file diff --git a/testcases/ooxml/XSSF/Streaming/TestSXSSFFormulaEvaluation.cs b/testcases/ooxml/XSSF/Streaming/TestSXSSFFormulaEvaluation.cs index 3017a84e4..ca6f7de2b 100644 --- a/testcases/ooxml/XSSF/Streaming/TestSXSSFFormulaEvaluation.cs +++ b/testcases/ooxml/XSSF/Streaming/TestSXSSFFormulaEvaluation.cs @@ -24,6 +24,7 @@ namespace TestCases.XSSF.Streaming using NPOI.XSSF.Streaming; using NPOI.XSSF.UserModel; using NUnit.Framework;using NUnit.Framework.Legacy; + using TestCases.SS.UserModel; /** * Formula Evaluation with SXSSF. @@ -33,10 +34,12 @@ namespace TestCases.XSSF.Streaming * from the cell are in the current window */ [TestFixture] - public class TestSXSSFFormulaEvaluation + public class TestSXSSFFormulaEvaluation : BaseTestFormulaEvaluator { - public static SXSSFITestDataProvider _testDataProvider = SXSSFITestDataProvider.instance; - + public TestSXSSFFormulaEvaluation() + : base(SXSSFITestDataProvider.instance) + { + } /** * EvaluateAll will normally fail, as any reference or * formula outside of the window will fail, and any @@ -185,8 +188,14 @@ public void TestEvaluateSimple() c.CellFormula = (/*setter*/"CONCATENATE(\"hello\",\" \",\"world\")"); eval.EvaluateFormulaCell(c); ClassicAssert.AreEqual("hello world", c.StringCellValue); + } - + [Test] + public override void TestUpdateCachedFormulaResultFromErrorToNumber_bug46479() + { + Assume.That(false, "This test is disabled because it fails for SXSSF because " + + "handling of errors in formulas is slightly different than in XSSF, " + + "but this proved to be non-trivial to solve..."); } } diff --git a/testcases/ooxml/XSSF/Streaming/TestSXSSFWorkbook.cs b/testcases/ooxml/XSSF/Streaming/TestSXSSFWorkbook.cs index addbfcecd..e72af5412 100644 --- a/testcases/ooxml/XSSF/Streaming/TestSXSSFWorkbook.cs +++ b/testcases/ooxml/XSSF/Streaming/TestSXSSFWorkbook.cs @@ -385,7 +385,6 @@ public void Bug53515a() out1.Delete(); for(int i = 0; i < 2; i++) { - Console.WriteLine("Iteration " + i); SXSSFWorkbook wb; if(out1.Exists) { diff --git a/testcases/ooxml/XSSF/UserModel/BaseTestXSSFPivotTable.cs b/testcases/ooxml/XSSF/UserModel/BaseTestXSSFPivotTable.cs index be8eec1a4..eead29b11 100644 --- a/testcases/ooxml/XSSF/UserModel/BaseTestXSSFPivotTable.cs +++ b/testcases/ooxml/XSSF/UserModel/BaseTestXSSFPivotTable.cs @@ -78,17 +78,9 @@ public void TestAddRowLabelToPivotTable() [Test] public void TestAddRowLabelOutOfRangeThrowsException() { - int columnIndex = 5; - - try - { - pivotTable.AddRowLabel(columnIndex); - } - catch (IndexOutOfRangeException) - { - return; - } - Assert.Fail(); + ClassicAssert.Throws(()=>{ + pivotTable.AddRowLabel(5); + }); } /** @@ -199,17 +191,9 @@ public void TestColumnLabelSetCustomName() [Test] public void TestAddColumnLabelOutOfRangeThrowsException() { - int columnIndex = 5; - - try - { - pivotTable.AddColumnLabel(DataConsolidateFunction.SUM, columnIndex); - } - catch (IndexOutOfRangeException) - { - return; - } - Assert.Fail(); + ClassicAssert.Throws(()=>{ + pivotTable.AddColumnLabel(DataConsolidateFunction.SUM, 5); + }); } /** @@ -233,18 +217,9 @@ public void TestAddDataColumn() [Test] public void TestAddDataColumnOutOfRangeThrowsException() { - int columnIndex = 5; - bool isDataField = true; - - try - { - pivotTable.AddDataColumn(columnIndex, isDataField); - } - catch (IndexOutOfRangeException) - { - return; - } - Assert.Fail(); + ClassicAssert.Throws(()=>{ + pivotTable.AddDataColumn(5, true); + }); } /** @@ -269,16 +244,9 @@ public void TestAddReportFilter() [Test] public void TestAddReportFilterOutOfRangeThrowsException() { - int columnIndex = 5; - try - { - pivotTable.AddReportFilter(columnIndex); - } - catch (IndexOutOfRangeException) - { - return; - } - Assert.Fail(); + ClassicAssert.Throws(()=>{ + pivotTable.AddReportFilter(5); + }); } /** diff --git a/testcases/ooxml/XSSF/UserModel/TestMatrixFormulasFromXMLSpreadsheet.cs b/testcases/ooxml/XSSF/UserModel/TestMatrixFormulasFromXMLSpreadsheet.cs index 62f53d387..6874e7b88 100644 --- a/testcases/ooxml/XSSF/UserModel/TestMatrixFormulasFromXMLSpreadsheet.cs +++ b/testcases/ooxml/XSSF/UserModel/TestMatrixFormulasFromXMLSpreadsheet.cs @@ -36,7 +36,8 @@ namespace TestCases.XSSF.UserModel [TestFixture] public sealed class TestMatrixFormulasFromXMLSpreadsheet { - + private static POILogger LOG = POILogFactory.GetLogger(typeof(TestMatrixFormulasFromXMLSpreadsheet)); + private static XSSFWorkbook workbook; private static ISheet sheet; private static IFormulaEvaluator evaluator; @@ -211,14 +212,14 @@ private static string GetTargetFunctionName(IRow r) { if(r == null) { - Console.WriteLine("Warning - given null row, can't figure out function name"); + LOG.Log(POILogger.WARN, "Warning - given null row, can't figure out function name"); return null; } ICell cell = r.GetCell(Navigator.START_OPERATORS_COL_INDEX); - Console.WriteLine(Navigator.START_OPERATORS_COL_INDEX); + LOG.Log(POILogger.DEBUG, Navigator.START_OPERATORS_COL_INDEX); if(cell == null) { - Console.WriteLine("Warning - Row " + r.RowNum + " has no cell " + Navigator.START_OPERATORS_COL_INDEX + ", can't figure out function name"); + LOG.Log(POILogger.WARN, "Warning - Row " + r.RowNum + " has no cell " + Navigator.START_OPERATORS_COL_INDEX + ", can't figure out function name"); return null; } if(cell.CellType == CellType.Blank) diff --git a/testcases/ooxml/XSSF/UserModel/TestUnfixedBugs.cs b/testcases/ooxml/XSSF/UserModel/TestUnfixedBugs.cs index fd46324c5..090f85fc5 100644 --- a/testcases/ooxml/XSSF/UserModel/TestUnfixedBugs.cs +++ b/testcases/ooxml/XSSF/UserModel/TestUnfixedBugs.cs @@ -85,7 +85,6 @@ public void Test54071() IWorkbook workbook = XSSFTestDataSamples.OpenSampleWorkbook("54071.xlsx"); ISheet sheet = workbook.GetSheetAt(0); int rows = sheet.PhysicalNumberOfRows; - Console.WriteLine(">> file rows is:" + (rows - 1) + " <<"); IRow title = sheet.GetRow(0); DateTime? prev = null; @@ -100,7 +99,7 @@ public void Test54071() { // here the output will produce ...59 or ...58 for the rows, probably POI is // doing some different rounding or some other small difference... - Console.WriteLine("==Time:" + cell.DateCellValue); + //Console.WriteLine("==Time:" + cell.DateCellValue); if (prev != null) { ClassicAssert.AreEqual(prev, cell.DateCellValue); diff --git a/testcases/ooxml/XSSF/UserModel/TestXSSFBugs.cs b/testcases/ooxml/XSSF/UserModel/TestXSSFBugs.cs index c8a64022e..082a54e09 100644 --- a/testcases/ooxml/XSSF/UserModel/TestXSSFBugs.cs +++ b/testcases/ooxml/XSSF/UserModel/TestXSSFBugs.cs @@ -2957,7 +2957,6 @@ public void Test50755_workday_formula_example() if (cell.CellType == CellType.Formula) { String formula = cell.CellFormula; - //System.out.println("formula: " + formula); ClassicAssert.IsNotNull(formula); ClassicAssert.IsTrue(formula.Contains("WORKDAY")); } @@ -3100,7 +3099,7 @@ public void Test51998() ISheet newSheet = wb.CreateSheet(); //Sheet newSheet = wb.createSheet(sheetName); int newSheetIndex = wb.GetSheetIndex(newSheet); - //System.out.println(newSheetIndex); + wb.SetSheetName(newSheetIndex, sheetName); wb.SetSheetOrder(sheetName, sheetIndex); } @@ -3199,8 +3198,6 @@ public void Test57236() { ICell cell = row.GetCell(cellNum); String fmtCellValue = formatter.FormatCellValue(cell); - - //System.out.Println("Cell: " + fmtCellValue); ClassicAssert.IsNotNull(fmtCellValue); ClassicAssert.IsFalse(fmtCellValue.Equals("0")); } @@ -3257,7 +3254,7 @@ public void Test59132() row = worksheet.GetRow(2); cell = row.GetCell(1); ClassicAssert.AreEqual(CellType.Blank, cell.CellType); - ClassicAssert.AreEqual(CellType.Unknown, evaluator.EvaluateFormulaCell(cell)); + ClassicAssert.AreEqual(CellType._None, evaluator.EvaluateFormulaCell(cell)); // A3 row = worksheet.GetRow(2); cell = row.GetCell(0); diff --git a/testcases/ooxml/XSSF/UserModel/TestXSSFColumn.cs b/testcases/ooxml/XSSF/UserModel/TestXSSFColumn.cs index bc1ef76ec..2e313bf28 100644 --- a/testcases/ooxml/XSSF/UserModel/TestXSSFColumn.cs +++ b/testcases/ooxml/XSSF/UserModel/TestXSSFColumn.cs @@ -395,7 +395,7 @@ public void CreateCell_WithValidIndexVariousTypes_CellsCreatedWithCorrectTypes() ClassicAssert.AreEqual(CellType.Blank, sheet.GetRow(4).GetCell(columnIndex).CellType); // Numeric cell with no data will return blank by default ClassicAssert.AreEqual(CellType.String, sheet.GetRow(5).GetCell(columnIndex).CellType); - _ = Assert.Throws(() => column.CreateCell(6, CellType.Unknown)); + _ = Assert.Throws(() => column.CreateCell(6, CellType._None)); ClassicAssert.IsNull(sheet.GetRow(6)); ClassicAssert.IsNull(column.GetCell(6)); diff --git a/testcases/ooxml/XSSF/UserModel/TestXSSFFormulaEvaluation.cs b/testcases/ooxml/XSSF/UserModel/TestXSSFFormulaEvaluation.cs index 236fb1965..d39c2a788 100644 --- a/testcases/ooxml/XSSF/UserModel/TestXSSFFormulaEvaluation.cs +++ b/testcases/ooxml/XSSF/UserModel/TestXSSFFormulaEvaluation.cs @@ -386,296 +386,6 @@ public void TestMultiSheetAreasHSSFandXSSF() wb1.Close(); } - [Test] - public void TestMultisheetFormulaEval() - { - XSSFWorkbook wb = new XSSFWorkbook(); - try - { - XSSFSheet sheet1 = wb.CreateSheet("Sheet1") as XSSFSheet; - XSSFSheet sheet2 = wb.CreateSheet("Sheet2") as XSSFSheet; - XSSFSheet sheet3 = wb.CreateSheet("Sheet3") as XSSFSheet; - - // sheet1 A1 - XSSFCell cell = sheet1.CreateRow(0).CreateCell(0) as XSSFCell; - cell.SetCellType(CellType.Numeric); - cell.SetCellValue(1.0); - - // sheet2 A1 - cell = sheet2.CreateRow(0).CreateCell(0) as XSSFCell; - cell.SetCellType(CellType.Numeric); - cell.SetCellValue(1.0); - - // sheet2 B1 - cell = sheet2.GetRow(0).CreateCell(1) as XSSFCell; - cell.SetCellType(CellType.Numeric); - cell.SetCellValue(1.0); - - // sheet3 A1 - cell = sheet3.CreateRow(0).CreateCell(0) as XSSFCell; - cell.SetCellType(CellType.Numeric); - cell.SetCellValue(1.0); - - // sheet1 A2 formulae - cell = sheet1.CreateRow(1).CreateCell(0) as XSSFCell; - cell.SetCellType(CellType.Formula); - cell.CellFormula = (/*setter*/"SUM(Sheet1:Sheet3!A1)"); - - // sheet1 A3 formulae - cell = sheet1.CreateRow(2).CreateCell(0) as XSSFCell; - cell.SetCellType(CellType.Formula); - cell.CellFormula = (/*setter*/"SUM(Sheet1:Sheet3!A1:B1)"); - - wb.GetCreationHelper().CreateFormulaEvaluator().EvaluateAll(); - - cell = sheet1.GetRow(1).GetCell(0) as XSSFCell; - ClassicAssert.AreEqual(3.0, cell.NumericCellValue, 0); - - cell = sheet1.GetRow(2).GetCell(0) as XSSFCell; - ClassicAssert.AreEqual(4.0, cell.NumericCellValue, 0); - } - finally - { - wb.Close(); - } - } - - [Test] - public void TestBug55843() - { - XSSFWorkbook wb = new XSSFWorkbook(); - try - { - XSSFSheet sheet = wb.CreateSheet("test") as XSSFSheet; - XSSFRow row = sheet.CreateRow(0) as XSSFRow; - XSSFRow row2 = sheet.CreateRow(1) as XSSFRow; - XSSFCell cellA2 = row2.CreateCell(0, CellType.Formula) as XSSFCell; - XSSFCell cellB1 = row.CreateCell(1, CellType.Numeric) as XSSFCell; - cellB1.SetCellValue(10); - XSSFFormulaEvaluator formulaEvaluator = wb.GetCreationHelper().CreateFormulaEvaluator() as XSSFFormulaEvaluator; - cellA2.SetCellFormula("IF(B1=0,\"\",((ROW()-ROW(A$1))*12))"); - CellValue Evaluate = formulaEvaluator.Evaluate(cellA2); - System.Console.WriteLine(Evaluate); - ClassicAssert.AreEqual("12", Evaluate.FormatAsString()); - - cellA2.CellFormula = (/*setter*/"IF(NOT(B1=0),((ROW()-ROW(A$1))*12),\"\")"); - CellValue EvaluateN = formulaEvaluator.Evaluate(cellA2); - System.Console.WriteLine(EvaluateN); - - ClassicAssert.AreEqual(Evaluate.ToString(), EvaluateN.ToString()); - ClassicAssert.AreEqual("12", EvaluateN.FormatAsString()); - } - finally - { - wb.Close(); - } - } - - [Test] - public void TestBug55843a() - { - XSSFWorkbook wb = new XSSFWorkbook(); - try - { - XSSFSheet sheet = wb.CreateSheet("test") as XSSFSheet; - XSSFRow row = sheet.CreateRow(0) as XSSFRow; - XSSFRow row2 = sheet.CreateRow(1) as XSSFRow; - XSSFCell cellA2 = row2.CreateCell(0, CellType.Formula) as XSSFCell; - XSSFCell cellB1 = row.CreateCell(1, CellType.Numeric) as XSSFCell; - cellB1.SetCellValue(10); - XSSFFormulaEvaluator formulaEvaluator = wb.GetCreationHelper().CreateFormulaEvaluator() as XSSFFormulaEvaluator; - cellA2.SetCellFormula("IF(B1=0,\"\",((ROW(A$1))))"); - CellValue Evaluate = formulaEvaluator.Evaluate(cellA2); - System.Console.WriteLine(Evaluate); - ClassicAssert.AreEqual("1", Evaluate.FormatAsString()); - - cellA2.CellFormula = (/*setter*/"IF(NOT(B1=0),((ROW(A$1))),\"\")"); - CellValue EvaluateN = formulaEvaluator.Evaluate(cellA2); - System.Console.WriteLine(EvaluateN); - - ClassicAssert.AreEqual(Evaluate.ToString(), EvaluateN.ToString()); - ClassicAssert.AreEqual("1", EvaluateN.FormatAsString()); - } - finally - { - wb.Close(); - } - } - - [Test] - public void TestBug55843b() - { - XSSFWorkbook wb = new XSSFWorkbook(); - try - { - XSSFSheet sheet = wb.CreateSheet("test") as XSSFSheet; - XSSFRow row = sheet.CreateRow(0) as XSSFRow; - XSSFRow row2 = sheet.CreateRow(1) as XSSFRow; - XSSFCell cellA2 = row2.CreateCell(0, CellType.Formula) as XSSFCell; - XSSFCell cellB1 = row.CreateCell(1, CellType.Numeric) as XSSFCell; - cellB1.SetCellValue(10); - XSSFFormulaEvaluator formulaEvaluator = wb.GetCreationHelper().CreateFormulaEvaluator() as XSSFFormulaEvaluator; - - cellA2.SetCellFormula("IF(B1=0,\"\",((ROW())))"); - CellValue Evaluate = formulaEvaluator.Evaluate(cellA2); - System.Console.WriteLine(Evaluate); - ClassicAssert.AreEqual("2", Evaluate.FormatAsString()); - - cellA2.CellFormula = (/*setter*/"IF(NOT(B1=0),((ROW())),\"\")"); - CellValue EvaluateN = formulaEvaluator.Evaluate(cellA2); - System.Console.WriteLine(EvaluateN); - - ClassicAssert.AreEqual(Evaluate.ToString(), EvaluateN.ToString()); - ClassicAssert.AreEqual("2", EvaluateN.FormatAsString()); - } - finally - { - wb.Close(); - } - } - - [Test] - public void TestBug55843c() - { - XSSFWorkbook wb = new XSSFWorkbook(); - try - { - XSSFSheet sheet = wb.CreateSheet("test") as XSSFSheet; - XSSFRow row = sheet.CreateRow(0) as XSSFRow; - XSSFRow row2 = sheet.CreateRow(1) as XSSFRow; - XSSFCell cellA2 = row2.CreateCell(0, CellType.Formula) as XSSFCell; - XSSFCell cellB1 = row.CreateCell(1, CellType.Numeric) as XSSFCell; - cellB1.SetCellValue(10); - XSSFFormulaEvaluator formulaEvaluator = wb.GetCreationHelper().CreateFormulaEvaluator() as XSSFFormulaEvaluator; - - cellA2.CellFormula = (/*setter*/"IF(NOT(B1=0),((ROW())))"); - CellValue EvaluateN = formulaEvaluator.Evaluate(cellA2); - System.Console.WriteLine(EvaluateN); - ClassicAssert.AreEqual("2", EvaluateN.FormatAsString()); - } - finally - { - wb.Close(); - } - } - - [Test] - public void TestBug55843d() - { - XSSFWorkbook wb = new XSSFWorkbook(); - try - { - XSSFSheet sheet = wb.CreateSheet("test") as XSSFSheet; - XSSFRow row = sheet.CreateRow(0) as XSSFRow; - XSSFRow row2 = sheet.CreateRow(1) as XSSFRow; - XSSFCell cellA2 = row2.CreateCell(0, CellType.Formula) as XSSFCell; - XSSFCell cellB1 = row.CreateCell(1, CellType.Numeric) as XSSFCell; - cellB1.SetCellValue(10); - XSSFFormulaEvaluator formulaEvaluator = wb.GetCreationHelper().CreateFormulaEvaluator() as XSSFFormulaEvaluator; - - cellA2.CellFormula = (/*setter*/"IF(NOT(B1=0),((ROW())),\"\")"); - CellValue EvaluateN = formulaEvaluator.Evaluate(cellA2); - System.Console.WriteLine(EvaluateN); - ClassicAssert.AreEqual("2", EvaluateN.FormatAsString()); - } - finally - { - wb.Close(); - } - } - - [Test] - public void TestBug55843e() - { - XSSFWorkbook wb = new XSSFWorkbook(); - try - { - XSSFSheet sheet = wb.CreateSheet("test") as XSSFSheet; - XSSFRow row = sheet.CreateRow(0) as XSSFRow; - XSSFRow row2 = sheet.CreateRow(1) as XSSFRow; - XSSFCell cellA2 = row2.CreateCell(0, CellType.Formula) as XSSFCell; - XSSFCell cellB1 = row.CreateCell(1, CellType.Numeric) as XSSFCell; - cellB1.SetCellValue(10); - XSSFFormulaEvaluator formulaEvaluator = wb.GetCreationHelper().CreateFormulaEvaluator() as XSSFFormulaEvaluator; - - cellA2.SetCellFormula("IF(B1=0,\"\",((ROW())))"); - CellValue Evaluate = formulaEvaluator.Evaluate(cellA2); - System.Console.WriteLine(Evaluate); - ClassicAssert.AreEqual("2", Evaluate.FormatAsString()); - } - finally - { - wb.Close(); - } - } - - - [Test] - public void TestBug55843f() - { - XSSFWorkbook wb = new XSSFWorkbook(); - try - { - XSSFSheet sheet = wb.CreateSheet("test") as XSSFSheet; - XSSFRow row = sheet.CreateRow(0) as XSSFRow; - XSSFRow row2 = sheet.CreateRow(1) as XSSFRow; - XSSFCell cellA2 = row2.CreateCell(0, CellType.Formula) as XSSFCell; - XSSFCell cellB1 = row.CreateCell(1, CellType.Numeric) as XSSFCell; - cellB1.SetCellValue(10); - XSSFFormulaEvaluator formulaEvaluator = wb.GetCreationHelper().CreateFormulaEvaluator() as XSSFFormulaEvaluator; - - cellA2.SetCellFormula("IF(B1=0,\"\",IF(B1=10,3,4))"); - CellValue Evaluate = formulaEvaluator.Evaluate(cellA2); - System.Console.WriteLine(Evaluate); - ClassicAssert.AreEqual("3", Evaluate.FormatAsString()); - } - finally - { - wb.Close(); - } - } - [Test] - public void TestBug56655() - { - IWorkbook wb = new XSSFWorkbook(); - ISheet sheet = wb.CreateSheet(); - - setCellFormula(sheet, 0, 0, "#VALUE!"); - setCellFormula(sheet, 0, 1, "SUMIFS(A:A,A:A,#VALUE!)"); - - wb.GetCreationHelper().CreateFormulaEvaluator().EvaluateAll(); - - ClassicAssert.AreEqual(CellType.Error, getCell(sheet, 0, 0).CachedFormulaResultType); - ClassicAssert.AreEqual(FormulaError.VALUE.Code, getCell(sheet, 0, 0).ErrorCellValue); - ClassicAssert.AreEqual(CellType.Error, getCell(sheet, 0, 1).CachedFormulaResultType); - ClassicAssert.AreEqual(FormulaError.VALUE.Code, getCell(sheet, 0, 1).ErrorCellValue); - - wb.Close(); - } - [Test] - public void TestBug56655a() - { - IWorkbook wb = new XSSFWorkbook(); - ISheet sheet = wb.CreateSheet(); - - setCellFormula(sheet, 0, 0, "B1*C1"); - sheet.GetRow(0).CreateCell(1).SetCellValue("A"); - setCellFormula(sheet, 1, 0, "B1*C1"); - sheet.GetRow(1).CreateCell(1).SetCellValue("A"); - setCellFormula(sheet, 0, 3, "SUMIFS(A:A,A:A,A2)"); - - wb.GetCreationHelper().CreateFormulaEvaluator().EvaluateAll(); - - ClassicAssert.AreEqual(CellType.Error, getCell(sheet, 0, 0).CachedFormulaResultType); - ClassicAssert.AreEqual(FormulaError.VALUE.Code, getCell(sheet, 0, 0).ErrorCellValue); - ClassicAssert.AreEqual(CellType.Error, getCell(sheet, 1, 0).CachedFormulaResultType); - ClassicAssert.AreEqual(FormulaError.VALUE.Code, getCell(sheet, 1, 0).ErrorCellValue); - ClassicAssert.AreEqual(CellType.Error, getCell(sheet, 0, 3).CachedFormulaResultType); - ClassicAssert.AreEqual(FormulaError.VALUE.Code, getCell(sheet, 0, 3).ErrorCellValue); - - wb.Close(); - } - // bug 57721 [Test] public void structuredReferences() @@ -699,35 +409,6 @@ private static void verifyAllFormulasInWorkbookCanBeEvaluated(String sampleWorkb } - /** - * @param row 0-based - * @param column 0-based - */ - private void setCellFormula(ISheet sheet, int row, int column, String formula) - { - IRow r = sheet.GetRow(row); - if (r == null) - { - r = sheet.CreateRow(row); - } - ICell cell = r.GetCell(column); - if (cell == null) - { - cell = r.CreateCell(column); - } - cell.SetCellType(CellType.Formula); - cell.CellFormula = (formula); - } - - /** - * @param rowNo 0-based - * @param column 0-based - */ - private ICell getCell(ISheet sheet, int rowNo, int column) - { - return sheet.GetRow(rowNo).GetCell(column); - } - [Test] public void Test59736() { diff --git a/testcases/ooxml/XSSF/UserModel/TestXSSFSheet.cs b/testcases/ooxml/XSSF/UserModel/TestXSSFSheet.cs index 24485db83..658b08ed5 100644 --- a/testcases/ooxml/XSSF/UserModel/TestXSSFSheet.cs +++ b/testcases/ooxml/XSSF/UserModel/TestXSSFSheet.cs @@ -2484,7 +2484,7 @@ protected void testCopyOneRow(String copyRowsTestWorkbook) ClassicAssert.AreEqual("J7", new CellReference(cell).FormatAsString()); ClassicAssert.AreEqual(CellType.Formula, cell.CellType, "[Cell Formula] J7 cell type"); ClassicAssert.AreEqual("5+2", cell.CellFormula, "[Cell Formula] J7 cell formula"); - Console.WriteLine("Cell formula evaluation currently unsupported"); + //Console.WriteLine("Cell formula evaluation currently unsupported"); // Cell Formula with Reference // Formula row references should be adjusted by destRowNum-srcRowNum @@ -2511,7 +2511,7 @@ protected void testCopyOneRow(String copyRowsTestWorkbook) // Array Formula cell = CellUtil.GetCell(destRow, col++); - Console.WriteLine("Array formulas currently unsupported"); + //Console.WriteLine("Array formulas currently unsupported"); // FIXME: Array Formula set with Sheet.setArrayFormula() instead of cell.setFormula() /* ClassicAssert.AreEqual("[Array Formula] N7 cell type", CellType.Formula, cell.CellType); diff --git a/testcases/ooxml/XSSF/UserModel/TestXSSFSheetMergeRegions.cs b/testcases/ooxml/XSSF/UserModel/TestXSSFSheetMergeRegions.cs index 865ede1a0..de3a6f12f 100644 --- a/testcases/ooxml/XSSF/UserModel/TestXSSFSheetMergeRegions.cs +++ b/testcases/ooxml/XSSF/UserModel/TestXSSFSheetMergeRegions.cs @@ -17,6 +17,7 @@ limitations under the License. namespace TestCases.XSSF.UserModel { using NPOI.SS.Util; + using NPOI.Util; using NPOI.XSSF; using NPOI.XSSF.UserModel; using NUnit.Framework;using NUnit.Framework.Legacy; @@ -26,6 +27,7 @@ namespace TestCases.XSSF.UserModel [TestFixture] public class TestXSSFSheetMergeRegions { + private static POILogger LOG = POILogFactory.GetLogger(typeof(TestXSSFSheetMergeRegions)); [Test] public void TestMergeRegionsSpeed() { @@ -43,7 +45,7 @@ public void TestMergeRegionsSpeed() { break; } - System.Console.WriteLine("Retry " + i + " because run-time is too high: " + millis); + LOG.Log(POILogger.INFO,"Retry " + i + " because run-time is too high: " + millis); } // This time is typically ~800ms, versus ~7800ms to iterate getMergedRegion(int). diff --git a/testcases/ooxml/XWPF/UserModel/TestXWPFPictureData.cs b/testcases/ooxml/XWPF/UserModel/TestXWPFPictureData.cs index 43696cf52..513ee39c3 100644 --- a/testcases/ooxml/XWPF/UserModel/TestXWPFPictureData.cs +++ b/testcases/ooxml/XWPF/UserModel/TestXWPFPictureData.cs @@ -180,9 +180,9 @@ public void TestBug51770() { if (paragraph.Document != null) { - System.Console.WriteLine(picture.GetCTPicture()); XWPFPictureData data = picture.GetPictureData(); - if (data != null) System.Console.WriteLine(data.FileName); + if (data != null) + Assert.Fail("Should have returned null: "+ data.FileName); } } }