Example #1
0
        public void TestNames()
        {
            HSSFWorkbook wb = new HSSFWorkbook();

            try
            {
                wb.GetNameAt(0);
                Assert.Fail("Fails without any defined names");
            }
            catch (ArgumentException e)
            {
                //Assert.IsTrue(e.Message.Contains("no defined names"), e.Message);
            }

            HSSFName name = (HSSFName)wb.CreateName();
            Assert.IsNotNull(name);

            Assert.IsNull(wb.GetName("somename"));

            name.NameName = ("myname");
            Assert.IsNotNull(wb.GetName("myname"));

            Assert.AreEqual(0, wb.GetNameIndex(name));
            Assert.AreEqual(0, wb.GetNameIndex("myname"));

            try
            {
                wb.GetNameAt(5);
                Assert.Fail("Fails without any defined names");
            }
            catch (ArgumentException e)
            {
                //Assert.IsTrue(e.Message.Contains("outside the allowable range"), e.Message);
            }

            try
            {
                wb.GetNameAt(-3);
                Assert.Fail("Fails without any defined names");
            }
            catch (ArgumentException e)
            {
                //Assert.IsTrue(e.Message.Contains("outside the allowable range"), e.Message);
            }
        }
Example #2
0
        public void Test46664()
        {
            HSSFWorkbook wb = new HSSFWorkbook();
            ISheet sheet = wb.CreateSheet("new_sheet");
            IRow row = sheet.CreateRow((short)0);
            row.CreateCell(0).SetCellValue(new HSSFRichTextString("Column A"));
            row.CreateCell(1).SetCellValue(new HSSFRichTextString("Column B"));
            row.CreateCell(2).SetCellValue(new HSSFRichTextString("Column C"));
            row.CreateCell(3).SetCellValue(new HSSFRichTextString("Column D"));
            row.CreateCell(4).SetCellValue(new HSSFRichTextString("Column E"));
            row.CreateCell(5).SetCellValue(new HSSFRichTextString("Column F"));

            //set print area from column a to column c (on first row)
            wb.SetPrintArea(
                    0, //sheet index
                    0, //start column
                    2, //end column
                    0, //start row
                    0  //end row
            );

            wb = WriteOutAndReadBack(wb);

            // Ensure the tab index
            TabIdRecord tr = null;
            foreach (Record r in wb.Workbook.Records)
            {
                if (r is TabIdRecord)
                {
                    tr = (TabIdRecord)r;
                }
            }
            Assert.IsNotNull(tr);
            Assert.AreEqual(1, tr._tabids.Length);
            Assert.AreEqual(0, tr._tabids[0]);

            // Ensure the print setup
            Assert.AreEqual("new_sheet!$A$1:$C$1", wb.GetPrintArea(0));
            Assert.AreEqual("new_sheet!$A$1:$C$1", wb.GetName("Print_Area").RefersToFormula);

            // Needs reference not value
            NameRecord nr = wb.Workbook.GetNameRecord(
                  wb.GetNameIndex("Print_Area")
            );
            Assert.AreEqual("Print_Area", nr.NameText);
            Assert.AreEqual(1, nr.NameDefinition.Length);
            Assert.AreEqual(
                  "new_sheet!$A$1:$C$1",
                  ((Area3DPtg)nr.NameDefinition[0]).ToFormulaString(HSSFEvaluationWorkbook.Create(wb))
            );
            // TODO - fix me to be Reference not Value!
            //Assert.AreEqual('R', nr.NameDefinition[0].RVAType);
        }
Example #3
0
        public void TestSetSheetOrderHSSF()
        {
            IWorkbook wb = new HSSFWorkbook();
            ISheet s1 = wb.CreateSheet("first sheet");
            ISheet s2 = wb.CreateSheet("other sheet");

            IName name1 = wb.CreateName();
            name1.NameName = (/*setter*/"name1");
            name1.RefersToFormula = (/*setter*/"'first sheet'!D1");

            IName name2 = wb.CreateName();
            name2.NameName = (/*setter*/"name2");
            name2.RefersToFormula = (/*setter*/"'other sheet'!C1");


            IRow s1r1 = s1.CreateRow(2);
            ICell c1 = s1r1.CreateCell(3);
            c1.SetCellValue(30);
            ICell c2 = s1r1.CreateCell(2);
            c2.CellFormula = (/*setter*/"SUM('other sheet'!C1,'first sheet'!C1)");

            IRow s2r1 = s2.CreateRow(0);
            ICell c3 = s2r1.CreateCell(1);
            c3.CellFormula = (/*setter*/"'first sheet'!D3");
            ICell c4 = s2r1.CreateCell(2);
            c4.CellFormula = (/*setter*/"'other sheet'!D3");

            // conditional formatting
            ISheetConditionalFormatting sheetCF = s1.SheetConditionalFormatting;

            IConditionalFormattingRule rule1 = sheetCF.CreateConditionalFormattingRule(
                    ComparisonOperator.Between, "'first sheet'!D1", "'other sheet'!D1");

            IConditionalFormattingRule[] cfRules = { rule1 };

            CellRangeAddress[] regions = { new CellRangeAddress(2, 4, 0, 0), // A3:A5
        };
            sheetCF.AddConditionalFormatting(regions, cfRules);

            wb.SetSheetOrder("other sheet", 0);

            // names
            Assert.AreEqual("'first sheet'!D1", wb.GetName("name1").RefersToFormula);
            Assert.AreEqual("'other sheet'!C1", wb.GetName("name2").RefersToFormula);

            // cells
            Assert.AreEqual("SUM('other sheet'!C1,'first sheet'!C1)", c2.CellFormula);
            Assert.AreEqual("'first sheet'!D3", c3.CellFormula);
            Assert.AreEqual("'other sheet'!D3", c4.CellFormula);

            // conditional formatting
            IConditionalFormatting cf = sheetCF.GetConditionalFormattingAt(0);
            Assert.AreEqual("'first sheet'!D1", cf.GetRule(0).Formula1);
            Assert.AreEqual("'other sheet'!D1", cf.GetRule(0).Formula2);
        }
Example #4
0
 private void expectName(HSSFWorkbook wb, String name, String expect)
 {
     Assert.AreEqual(expect, wb.GetName(name).RefersToFormula);
 }