diff --git a/main/SS/Formula/FormulaShifter.cs b/main/SS/Formula/FormulaShifter.cs index d421de740..cab7fe93d 100644 --- a/main/SS/Formula/FormulaShifter.cs +++ b/main/SS/Formula/FormulaShifter.cs @@ -1005,13 +1005,19 @@ private Ptg RowCopyRefPtg(RefPtgBase rptg) int refRow = rptg.Row; if (rptg.IsRowRelative) { + // check new location where the ref is located int destRowIndex = _firstMovedIndex + _amountToMove; if (destRowIndex < 0 || _version.LastRowIndex < destRowIndex) { return CreateDeletedRef(rptg); } - - rptg.Row = refRow + _amountToMove; + // check new location where the ref points to + int newRowIndex = refRow + _amountToMove; + if(newRowIndex < 0 || _version.LastRowIndex < newRowIndex) + { + return CreateDeletedRef(rptg); + } + rptg.Row = newRowIndex; return rptg; } diff --git a/main/SS/UserModel/FractionFormat.cs b/main/SS/UserModel/FractionFormat.cs index c2bbc2a6c..db8e609df 100644 --- a/main/SS/UserModel/FractionFormat.cs +++ b/main/SS/UserModel/FractionFormat.cs @@ -173,7 +173,7 @@ public String Format(string num) } //if whole part has to go into the numerator - if ("".Equals(wholePartFormatString)) + if (string.IsNullOrEmpty(wholePartFormatString)) { int trueNum = (fract.Denominator * (int)wholePart) + fract.Numerator; sb1.Append(trueNum).Append("/").Append(fract.Denominator); diff --git a/main/Util/IntList.cs b/main/Util/IntList.cs index 00b4dc455..ff783ff50 100644 --- a/main/Util/IntList.cs +++ b/main/Util/IntList.cs @@ -46,7 +46,7 @@ public class IntList { private int[] _array; private int _limit; - private int fillval = 0; + private static int _default_size = 128; /// @@ -57,10 +57,10 @@ public IntList() { } - public IntList(int InitialCapacity) - : this(InitialCapacity, 0) + public IntList(int initialCapacity) { - + _array = new int[initialCapacity]; + _limit = 0; } /// @@ -74,30 +74,6 @@ public IntList(IntList list) _limit = list._limit; } - /// - /// create an IntList with a predefined Initial size - /// - /// the size for the internal array - /// - public IntList(int initialCapacity, int fillvalue) - { - _array = new int[initialCapacity]; - if (fillval != 0) - { - fillval = fillvalue; - FillArray(fillval, _array, 0); - } - _limit = 0; - } - - private static void FillArray(int val, int[] array, int index) - { - for (int k = index; k < array.Length; k++) - { - array[k] = val; - } - } - /// /// add the specfied value at the specified index /// @@ -580,11 +556,6 @@ private void growArray(int new_size) : new_size; int[] new_array = new int[size]; - if (fillval != 0) - { - FillArray(fillval, new_array, _array.Length); - } - Array.Copy(_array, 0, new_array, 0, _limit); _array = new_array; } diff --git a/ooxml/XSSF/Extractor/XSSFExportToXml.cs b/ooxml/XSSF/Extractor/XSSFExportToXml.cs index 9f9332c17..7e05fcc2b 100644 --- a/ooxml/XSSF/Extractor/XSSFExportToXml.cs +++ b/ooxml/XSSF/Extractor/XSSFExportToXml.cs @@ -494,38 +494,38 @@ private static int IndexOfElementInComplexType(String elementName, XmlNode compl { return -1; } - XmlNodeList list = complexType.ChildNodes; + int indexOf = -1; + int i = 0; + XmlNode node = complexType.FirstChild; + String elementNameWithoutNamespace = RemoveNamespace(elementName); - for (int i = 0; i < list.Count; i++) + while(node != null) { - XmlNode node = list[i]; - if (node is XmlElement) - { - if (node.LocalName.Equals("element")) + if(node is XmlElement && "element".Equals(node.LocalName)) { + XmlNode element = GetNameOrRefElement(node); + if(element.Value.Equals(elementNameWithoutNamespace)) { - XmlNode element = GetNameOrRefElement(node); - if (element.Value.Equals(RemoveNamespace(elementName))) - { - indexOf = i; - break; - } - + indexOf = i; + break; } } + i++; + node = node.NextSibling; } + return indexOf; } private static XmlNode GetNameOrRefElement(XmlNode node) { - XmlNode returnNode = node.Attributes.GetNamedItem("name"); + XmlNode returnNode = node.Attributes.GetNamedItem("ref"); if(returnNode != null) { return returnNode; } - return node.Attributes.GetNamedItem("ref"); + return node.Attributes.GetNamedItem("name"); } private static XmlNode GetComplexTypeForElement(String elementName, XmlNode xmlSchema, XmlNode localComplexTypeRootNode) @@ -549,37 +549,33 @@ private static String GetComplexTypeNameFromChildren(XmlNode localComplexTypeRoo { return ""; } - XmlNodeList list = localComplexTypeRootNode.ChildNodes; + XmlNode node = localComplexTypeRootNode.FirstChild; String complexTypeName = ""; - for(int i = 0; i < list.Count; i++) + while(node != null) { - XmlNode node = list[i]; - if(node is XmlElement) + if(node is XmlElement && "element".Equals(node.LocalName)) { - if(node.LocalName.Equals("element")) + XmlNode nameAttribute = node.Attributes.GetNamedItem("name"); + if(nameAttribute.Value.Equals(elementNameWithoutNamespace)) { - XmlNode nameAttribute = node.Attributes.GetNamedItem("name"); - if(nameAttribute.Value.Equals(elementNameWithoutNamespace)) + XmlNode complexTypeAttribute = node.Attributes.GetNamedItem("type"); + if(complexTypeAttribute != null) { - XmlNode complexTypeAttribute = node.Attributes.GetNamedItem("type"); - if(complexTypeAttribute != null) - { - complexTypeName = complexTypeAttribute.Value; - break; - } + complexTypeName = complexTypeAttribute.Value; + break; } } } + node = node.NextSibling; } return complexTypeName; } private static XmlNode GetComplexTypeNodeFromSchemaChildren(XmlNode xmlSchema, XmlNode complexTypeNode, String complexTypeName) { - XmlNodeList complexTypeList = xmlSchema.ChildNodes; - for(int i = 0; i < complexTypeList.Count; i++) + XmlNode node = xmlSchema.FirstChild; + while(node != null) { - XmlNode node = complexTypeList[i]; if(node is XmlElement) { if(node.LocalName.Equals("complexType")) @@ -587,29 +583,28 @@ private static XmlNode GetComplexTypeNodeFromSchemaChildren(XmlNode xmlSchema, X XmlNode nameAttribute = node.Attributes.GetNamedItem("name"); if(nameAttribute.Value.Equals(complexTypeName)) { - - XmlNodeList complexTypeChildList = node.ChildNodes; - for(int j = 0; j < complexTypeChildList.Count; j++) + XmlNode sequence = node.FirstChild; + while(sequence != null) { - XmlNode sequence = complexTypeChildList[j]; - if(sequence is XmlElement) { - if(sequence.LocalName.Equals("sequence")) + String localName = sequence.LocalName; + if("sequence".Equals(localName) || "all".Equals(localName)) { complexTypeNode = sequence; break; } } + sequence = sequence.NextSibling; } if(complexTypeNode != null) { break; } - } } } + node = node.NextSibling; } return complexTypeNode; } diff --git a/ooxml/XSSF/Model/StylesTable.cs b/ooxml/XSSF/Model/StylesTable.cs index 72f123ea8..1afeedf2e 100644 --- a/ooxml/XSSF/Model/StylesTable.cs +++ b/ooxml/XSSF/Model/StylesTable.cs @@ -926,7 +926,8 @@ public XSSFCellStyle CreateCellStyle() /** * Finds a font that matches the one with the supplied attributes */ - public XSSFFont FindFont(bool bold, short color, short fontHeight, String name, bool italic, bool strikeout, FontSuperScript typeOffset, FontUnderlineType underline) + public XSSFFont FindFont(bool bold, short color, short fontHeight, String name, bool italic, bool strikeout, + FontSuperScript typeOffset, FontUnderlineType underline) { foreach (XSSFFont font in fonts) { @@ -945,6 +946,29 @@ public XSSFFont FindFont(bool bold, short color, short fontHeight, String name, return null; } + /// + /// Finds a font that matches the one with the supplied attributes, + /// where color is the actual Color-value, not the indexed color + /// + public XSSFFont FindFont(bool bold, IColor color, short fontHeight, string name, bool italic, bool strikeout, + FontSuperScript typeOffset, FontUnderlineType underline) + { + foreach(XSSFFont font in fonts) + { + if((font.IsBold == bold) + && font.GetXSSFColor().Equals(color) + && font.FontHeight == fontHeight + && font.FontName.Equals(name) + && font.IsItalic == italic + && font.IsStrikeout == strikeout + && font.TypeOffset == typeOffset + && font.Underline == underline) + { + return font; + } + } + return null; + } /// /// default or custom indexed color to RGB mapping /// diff --git a/ooxml/XSSF/UserModel/XSSFBorderFormatting.cs b/ooxml/XSSF/UserModel/XSSFBorderFormatting.cs index 481a33b74..49b2253dd 100644 --- a/ooxml/XSSF/UserModel/XSSFBorderFormatting.cs +++ b/ooxml/XSSF/UserModel/XSSFBorderFormatting.cs @@ -171,7 +171,7 @@ public short TopBorderColor { get { - return GetIndexedColor(RightBorderColorColor as XSSFColor); + return GetIndexedColor(TopBorderColorColor as XSSFColor); } set { diff --git a/ooxml/XSSF/UserModel/XSSFFont.cs b/ooxml/XSSF/UserModel/XSSFFont.cs index 5e9eb65df..b3cd2341c 100644 --- a/ooxml/XSSF/UserModel/XSSFFont.cs +++ b/ooxml/XSSF/UserModel/XSSFFont.cs @@ -629,6 +629,7 @@ public void SetFamily(FontFamily family) * @return unique index number of the underlying record this Font represents (probably you don't care * unless you're comparing which one is which) */ + [Obsolete] public short Index { get @@ -636,6 +637,13 @@ public short Index return _index; } } + public int IndexAsInt + { + get + { + return _index; + } + } public override int GetHashCode() { diff --git a/openxml4Net/OPC/PackagingUriHelper.cs b/openxml4Net/OPC/PackagingUriHelper.cs index 361b9c036..cf3c9671d 100644 --- a/openxml4Net/OPC/PackagingUriHelper.cs +++ b/openxml4Net/OPC/PackagingUriHelper.cs @@ -262,13 +262,9 @@ public static Uri Combine(Uri prefix, Uri suffix) */ public static String Combine(String prefix, String suffix) { - if (!prefix.EndsWith("" + FORWARD_SLASH_CHAR) - && !suffix.StartsWith("" + FORWARD_SLASH_CHAR)) + if (!prefix.EndsWith(FORWARD_SLASH_STRING) && !suffix.StartsWith(FORWARD_SLASH_STRING)) return prefix + FORWARD_SLASH_CHAR + suffix; - else if ((!prefix.EndsWith("" + FORWARD_SLASH_CHAR) - && suffix.StartsWith("" + FORWARD_SLASH_CHAR) || (prefix - .EndsWith("" + FORWARD_SLASH_CHAR) && !suffix.StartsWith("" - + FORWARD_SLASH_CHAR)))) + else if (prefix.EndsWith(FORWARD_SLASH_STRING) ^ suffix.StartsWith(FORWARD_SLASH_STRING)) return prefix + suffix; else return ""; diff --git a/testcases/main/HSSF/UserModel/TestBugs.cs b/testcases/main/HSSF/UserModel/TestBugs.cs index ca2a09167..a29fb3fb9 100644 --- a/testcases/main/HSSF/UserModel/TestBugs.cs +++ b/testcases/main/HSSF/UserModel/TestBugs.cs @@ -3520,6 +3520,34 @@ public void Test61300() }); } + [Test] + public void Test51262() + { + HSSFWorkbook wb = HSSFTestDataSamples.OpenSampleWorkbook("51262.xls"); + ISheet sheet = wb.GetSheetAt(0); + IRow row = sheet.GetRow(2); + + ICell cell = row.GetCell(1); + ICellStyle style = cell.CellStyle; + ClassicAssert.AreEqual(26, style.FontIndex); + + row = sheet.GetRow(3); + cell = row.GetCell(1); + style = cell.CellStyle; + ClassicAssert.AreEqual(28, style.FontIndex); + + // check the two fonts + HSSFFont font = wb.GetFontAt((short) 26) as HSSFFont; + ClassicAssert.IsTrue(font.IsBold); + ClassicAssert.AreEqual(10, font.FontHeightInPoints); + ClassicAssert.AreEqual("\uFF2D\uFF33 \uFF30\u30B4\u30B7\u30C3\u30AF", font.FontName); + + font = wb.GetFontAt((short) 28) as HSSFFont; + ClassicAssert.IsTrue(font.IsBold); + ClassicAssert.AreEqual(10, font.FontHeightInPoints); + ClassicAssert.AreEqual("\uFF2D\uFF33 \uFF30\u30B4\u30B7\u30C3\u30AF", font.FontName); + } + // follow https://svn.apache.org/viewvc?view=revision&revision=1896552 to write a unit test for this fix. [Test] public void Test52447() diff --git a/testcases/main/Util/TestIntList.cs b/testcases/main/Util/TestIntList.cs index e0420aebd..bebc31f77 100644 --- a/testcases/main/Util/TestIntList.cs +++ b/testcases/main/Util/TestIntList.cs @@ -201,6 +201,18 @@ public void TestAddAll() ClassicAssert.AreEqual(list.Get(4), empty.Get(9)); ClassicAssert.AreEqual(list.Get(4), empty.Get(14)); } + + [Test] + public void AestAddAllGrow() + { + IntList list = new IntList(0); + IntList addList = new IntList(0); + addList.Add(1); + addList.Add(2); + + ClassicAssert.IsTrue(list.AddAll(0, addList)); + } + [Test] public void TestClear() { @@ -240,7 +252,7 @@ public void TestContains() } else { - ClassicAssert.IsTrue(!list.Contains(j)); + ClassicAssert.IsFalse(list.Contains(j)); } } } @@ -260,10 +272,10 @@ public void TestContainsAll() ClassicAssert.IsTrue(list.ContainsAll(list2)); list2.Add(10); ClassicAssert.IsTrue(list2.ContainsAll(list)); - ClassicAssert.IsTrue(!list.ContainsAll(list2)); + ClassicAssert.IsFalse(list.ContainsAll(list2)); list.Add(11); - ClassicAssert.IsTrue(!list2.ContainsAll(list)); - ClassicAssert.IsTrue(!list.ContainsAll(list2)); + ClassicAssert.IsFalse(list2.ContainsAll(list)); + ClassicAssert.IsFalse(list.ContainsAll(list2)); } [Test] public void TestEquals() @@ -271,7 +283,7 @@ public void TestEquals() IntList list = new IntList(); ClassicAssert.AreEqual(list, list); - ClassicAssert.IsTrue(!list.Equals(null)); + ClassicAssert.IsFalse(list.Equals(null)); IntList list2 = new IntList(200); ClassicAssert.IsTrue(list.Equals(list2));//ClassicAssert.AreEqual(list, list2); @@ -281,14 +293,15 @@ public void TestEquals() list.Add(1); list2.Add(1); list2.Add(0); - ClassicAssert.IsTrue(!list.Equals(list2)); + ClassicAssert.IsFalse(list.Equals(list2)); list2.RemoveValue(1); list2.Add(1); ClassicAssert.IsTrue(list.Equals(list2));//ClassicAssert.AreEqual(list, list2); ClassicAssert.IsTrue(list2.Equals(list));//ClassicAssert.AreEqual(list2, list); + ClassicAssert.AreEqual(list.GetHashCode(), list.GetHashCode()); list2.Add(2); - ClassicAssert.IsTrue(!list.Equals(list2)); - ClassicAssert.IsTrue(!list2.Equals(list)); + ClassicAssert.IsFalse(list.Equals(list2)); + ClassicAssert.IsFalse(list2.Equals(list)); } [Test] public void TestGet() @@ -352,9 +365,9 @@ public void TestIsEmpty() list1.Add(1); list2.Add(2); list3 = new IntList(list2); - ClassicAssert.IsTrue(!list1.IsEmpty()); - ClassicAssert.IsTrue(!list2.IsEmpty()); - ClassicAssert.IsTrue(!list3.IsEmpty()); + ClassicAssert.IsFalse(list1.IsEmpty()); + ClassicAssert.IsFalse(list2.IsEmpty()); + ClassicAssert.IsFalse(list3.IsEmpty()); list1.Clear(); list2.Remove(0); list3.RemoveValue(2); @@ -433,7 +446,7 @@ public void TestRemoveValue() ClassicAssert.IsTrue(list.RemoveValue(j)); ClassicAssert.IsTrue(list.RemoveValue(j)); } - ClassicAssert.IsTrue(!list.RemoveValue(j)); + ClassicAssert.IsFalse(list.RemoveValue(j)); } } [Test] @@ -460,16 +473,22 @@ public void TestRemoveAll() listOdd.Add(j); } } - list.RemoveAll(listEven); - //ClassicAssert.AreEqual(list, listOdd); - ClassicAssert.IsTrue(list.Equals(listOdd)); - list.RemoveAll(listOdd); + ClassicAssert.IsTrue(list.RemoveAll(listEven)); + ClassicAssert.AreEqual(list, listOdd); + + ClassicAssert.IsTrue(list.RemoveAll(listOdd)); ClassicAssert.IsTrue(list.IsEmpty()); - listCopy.RemoveAll(listOdd); - //ClassicAssert.AreEqual(listCopy, listEven); - ClassicAssert.IsTrue(listCopy.Equals(listEven)); - listCopy.RemoveAll(listEven); + + ClassicAssert.IsTrue(listCopy.RemoveAll(listOdd)); + ClassicAssert.AreEqual(listCopy, listEven); + + ClassicAssert.IsTrue(listCopy.RemoveAll(listEven)); ClassicAssert.IsTrue(listCopy.IsEmpty()); + + ClassicAssert.IsFalse(list.RemoveAll(listEven)); + ClassicAssert.IsFalse(list.RemoveAll(listOdd)); + ClassicAssert.IsFalse(listCopy.RemoveAll(listEven)); + ClassicAssert.IsFalse(listCopy.RemoveAll(listEven)); } [Test] public void TestRetainAll() @@ -495,16 +514,22 @@ public void TestRetainAll() listOdd.Add(j); } } - list.RetainAll(listOdd); - //ClassicAssert.AreEqual(list, listOdd); - ClassicAssert.IsTrue(list.Equals(listOdd)); - list.RetainAll(listEven); + ClassicAssert.IsTrue(list.RetainAll(listOdd)); + ClassicAssert.AreEqual(list, listOdd); + + ClassicAssert.IsTrue(list.RetainAll(listEven)); ClassicAssert.IsTrue(list.IsEmpty()); - listCopy.RetainAll(listEven); - //ClassicAssert.AreEqual(listCopy, listEven); - ClassicAssert.IsTrue(listCopy.Equals(listEven)); - listCopy.RetainAll(listOdd); + + ClassicAssert.IsTrue(listCopy.RetainAll(listEven)); + ClassicAssert.AreEqual(listCopy, listEven); + + ClassicAssert.IsTrue(listCopy.RetainAll(listOdd)); ClassicAssert.IsTrue(listCopy.IsEmpty()); + + ClassicAssert.IsFalse(list.RetainAll(listOdd)); + ClassicAssert.IsFalse(list.RetainAll(listEven)); + ClassicAssert.IsFalse(listCopy.RetainAll(listEven)); + ClassicAssert.IsFalse(listCopy.RetainAll(listOdd)); } [Test] public void TestSet() diff --git a/testcases/ooxml/XSSF/UserModel/TestXSSFBugs.cs b/testcases/ooxml/XSSF/UserModel/TestXSSFBugs.cs index 082a54e09..3c65c825b 100644 --- a/testcases/ooxml/XSSF/UserModel/TestXSSFBugs.cs +++ b/testcases/ooxml/XSSF/UserModel/TestXSSFBugs.cs @@ -19,9 +19,11 @@ limitations under the License. using NPOI.OpenXml4Net.OPC; using NPOI.OpenXmlFormats.Spreadsheet; using NPOI.POIFS.FileSystem; +using NPOI.SS; using NPOI.SS.Formula; using NPOI.SS.Formula.Eval; using NPOI.SS.Formula.Functions; +using NPOI.SS.Formula.PTG; using NPOI.SS.UserModel; using NPOI.SS.Util; using NPOI.Util; @@ -310,7 +312,7 @@ public void Bug48539() * a theme is used */ [Test] - public void Test48779() + public void Bug48779() { XSSFWorkbook wb = XSSFTestDataSamples.OpenSampleWorkbook("48779.xlsx"); XSSFCell cell = wb.GetSheetAt(0).GetRow(0).GetCell(0) as XSSFCell; @@ -1567,7 +1569,7 @@ public void Test51710() * Bug 53101: */ [Test] - public void Test5301() + public void Bug5301() { IWorkbook wb = XSSFTestDataSamples.OpenSampleWorkbook("53101.xlsx"); IFormulaEvaluator Evaluator = @@ -3484,7 +3486,7 @@ public void Test53611() // we currently only populate the dimension during writing out // to avoid having to iterate all rows/cells in each add/remove of a row or cell - IOUtils.Write(wb, new NullOutputStream()); + wb.Write(new NullOutputStream()); ClassicAssert.AreEqual("B2:I5", ((XSSFSheet)sheet).GetCTWorksheet().dimension.@ref); @@ -3509,6 +3511,59 @@ public void Bug61063() wb.Close(); } + [Test] + public void Bug61516() + { + string initialFormula = "A1"; + string expectedFormula = "#REF!"; // from ms excel + + IWorkbook wb = new XSSFWorkbook(); + ISheet sheet = wb.CreateSheet("sheet1"); + sheet.CreateRow(0).CreateCell(0).SetCellValue(1); // A1 = 1 + + { + ICell c3 = sheet.CreateRow(2).CreateCell(2); + c3.SetCellFormula(initialFormula); // C3 = =A1 + IFormulaEvaluator evaluator = wb.GetCreationHelper().CreateFormulaEvaluator(); + CellValue cellValue = evaluator.Evaluate(c3); + ClassicAssert.AreEqual(1, cellValue.NumberValue, 0.0001); + } + + { + FormulaShifter formulaShifter = FormulaShifter.CreateForRowCopy(0, "sheet1", 2/*firstRowToShift*/, 2/*lastRowToShift*/ + , -1/*step*/, SpreadsheetVersion.EXCEL2007); // parameters 2, 2, -1 should mean : Move row range [2-2] one level up + XSSFEvaluationWorkbook fpb = XSSFEvaluationWorkbook.Create((XSSFWorkbook) wb); + Ptg[] ptgs = FormulaParser.Parse(initialFormula, fpb, FormulaType.Cell, 0); // [A1] + formulaShifter.AdjustFormula(ptgs, 0); // adjusted to [A] + string shiftedFmla = FormulaRenderer.ToFormulaString(fpb, ptgs); //A + //Console.WriteLine(String.format("initial formula : A1; expected formula value After shifting up : #REF!; actual formula value : %s", shiftedFmla)); + ClassicAssert.AreEqual(expectedFormula, shiftedFmla, + "On copy we expect the formula to be adjusted, in this case it would point to row -1, which is an invalid REF"); + } + + { + FormulaShifter formulaShifter = FormulaShifter.CreateForRowShift(0, "sheet1", 2/*firstRowToShift*/, 2/*lastRowToShift*/ + , -1/*step*/, SpreadsheetVersion.EXCEL2007); // parameters 2, 2, -1 should mean : Move row range [2-2] one level up + XSSFEvaluationWorkbook fpb = XSSFEvaluationWorkbook.Create((XSSFWorkbook) wb); + Ptg[] ptgs = FormulaParser.Parse(initialFormula, fpb, FormulaType.Cell, 0); // [A1] + formulaShifter.AdjustFormula(ptgs, 0); // adjusted to [A] + string shiftedFmla = FormulaRenderer.ToFormulaString(fpb, ptgs); //A + //Console.WriteLine(String.format("initial formula : A1; expected formula value After shifting up : #REF!; actual formula value : %s", shiftedFmla)); + ClassicAssert.AreEqual(initialFormula, shiftedFmla, + "On Move we expect the formula to stay the same, thus expecting the initial formula A1 here"); + } + + sheet.ShiftRows(2, 2, -1); + { + ICell c2 = sheet.GetRow(1).GetCell(2); + ClassicAssert.IsNotNull(c2, "cell C2 needs to exist now"); + ClassicAssert.AreEqual(CellType.Formula, c2.CellType); + ClassicAssert.AreEqual(initialFormula, c2.CellFormula); + IFormulaEvaluator evaluator = wb.GetCreationHelper().CreateFormulaEvaluator(); + CellValue cellValue = evaluator.Evaluate(c2); + ClassicAssert.AreEqual(1, cellValue.NumberValue, 0.0001); + } + } [Test] public void TestBug690() { @@ -3588,5 +3643,7 @@ public void TestCopyEmptyRow() ClassicAssert.IsTrue(shiftedRow.GetCell(3).StringCellValue.Equals("D2")); } } + + } } diff --git a/testcases/ooxml/XSSF/UserModel/TestXSSFFont.cs b/testcases/ooxml/XSSF/UserModel/TestXSSFFont.cs index 5d398bf67..0fb5bb334 100644 --- a/testcases/ooxml/XSSF/UserModel/TestXSSFFont.cs +++ b/testcases/ooxml/XSSF/UserModel/TestXSSFFont.cs @@ -16,13 +16,15 @@ limitations under the License. ==================================================================== */ using NPOI; +using NPOI.OOXML.XSSF.UserModel; using NPOI.OpenXmlFormats.Spreadsheet; using NPOI.SS.UserModel; using NPOI.SS.Util; using NPOI.Util; using NPOI.XSSF; using NPOI.XSSF.UserModel; -using NUnit.Framework;using NUnit.Framework.Legacy; +using NUnit.Framework; +using NUnit.Framework.Legacy; using System.Text; using TestCases.SS.UserModel; @@ -323,5 +325,112 @@ public void TestCanComputeWidthInvalidFont() // Even with invalid fonts we still get back useful data most of the time... SheetUtil.CanComputeColumnWidth(font); } + + /// + /// Test that fonts Get added properly + /// + [Test] + public void TestFindFont() + { + XSSFWorkbook wb = new XSSFWorkbook(); + ClassicAssert.AreEqual(1, wb.NumberOfFonts); + + XSSFSheet s = wb.CreateSheet() as XSSFSheet; + s.CreateRow(0); + s.CreateRow(1); + s.GetRow(0).CreateCell(0); + s.GetRow(1).CreateCell(0); + + ClassicAssert.AreEqual(1, wb.NumberOfFonts); + + XSSFFont f1 = wb.GetFontAt(0) as XSSFFont; + ClassicAssert.IsFalse(f1.IsBold); + + // Check that asking for the same font + // multiple times gives you the same thing. + // Otherwise, our tests wouldn't work! + ClassicAssert.AreSame(wb.GetFontAt(0), wb.GetFontAt(0)); + ClassicAssert.AreEqual( + wb.GetFontAt(0), + wb.GetFontAt(0) + ); + + // Look for a new font we have + // yet to add + ClassicAssert.IsNull( + wb.FindFont( + false, IndexedColors.Indigo.Index, (short) 22, + "Thingy", false, true, (FontSuperScript) 2, (FontUnderlineType) 2 + ) + ); + ClassicAssert.IsNull( + wb.GetStylesSource().FindFont( + false, new XSSFColor(IndexedColors.Indigo, new DefaultIndexedColorMap()), (short) 22, + "Thingy", false, true, (FontSuperScript) 2, (FontUnderlineType) 2 + ) + ); + + XSSFFont nf = wb.CreateFont() as XSSFFont; + ClassicAssert.AreEqual(2, wb.NumberOfFonts); + + ClassicAssert.AreEqual(1, nf.IndexAsInt); + ClassicAssert.AreEqual(nf, wb.GetFontAt(1)); + + nf.IsBold = false; + nf.Color = IndexedColors.Indigo.Index; + nf.FontHeight = 22; + nf.FontName = "Thingy"; + nf.IsItalic = false; + nf.IsStrikeout = true; + nf.TypeOffset = (FontSuperScript) 2; + nf.Underline = (FontUnderlineType) 2; + + ClassicAssert.AreEqual(2, wb.NumberOfFonts); + ClassicAssert.AreEqual(nf, wb.GetFontAt(1)); + + ClassicAssert.IsTrue( + wb.GetFontAt(0) + != + wb.GetFontAt(1) + ); + + // Find it now + ClassicAssert.IsNotNull( + wb.FindFont( + false, IndexedColors.Indigo.Index, (short) 22, + "Thingy", false, true, (FontSuperScript) 2, (FontUnderlineType) 2 + ) + ); + ClassicAssert.IsNotNull( + wb.GetStylesSource().FindFont( + false, new XSSFColor(IndexedColors.Indigo, new DefaultIndexedColorMap()), (short) 22, + "Thingy", false, true, (FontSuperScript) 2, (FontUnderlineType) 2 + ) + ); + + XSSFFont font = wb.FindFont( + false, IndexedColors.Indigo.Index, (short) 22, + "Thingy", false, true, (FontSuperScript) 2, (FontUnderlineType) 2 + ) as XSSFFont; + ClassicAssert.IsNotNull(font); + ClassicAssert.AreEqual( + 1, + font.IndexAsInt + ); + ClassicAssert.AreEqual(nf, + wb.FindFont( + false, IndexedColors.Indigo.Index, (short) 22, + "Thingy", false, true, (FontSuperScript) 2, (FontUnderlineType) 2 + ) + ); + ClassicAssert.AreEqual(nf, + wb.GetStylesSource().FindFont( + false, new XSSFColor(IndexedColors.Indigo, new DefaultIndexedColorMap()), (short) 22, + "Thingy", false, true, (FontSuperScript) 2, (FontUnderlineType) 2 + ) + ); + + wb.Close(); + } } } \ No newline at end of file diff --git a/testcases/test-data/spreadsheet/51262.xls b/testcases/test-data/spreadsheet/51262.xls new file mode 100644 index 000000000..86ad84d4c Binary files /dev/null and b/testcases/test-data/spreadsheet/51262.xls differ