public void TestRemoveSheet() { // Test removing a sheet maintains the named ranges correctly XSSFWorkbook wb = new XSSFWorkbook(); wb.CreateSheet("Sheet1"); wb.CreateSheet("Sheet2"); XSSFName sheet1Name = wb.CreateName() as XSSFName; sheet1Name.NameName = "name1"; sheet1Name.SheetIndex = 0; sheet1Name.RefersToFormula = "Sheet1!$A$1"; XSSFName sheet2Name = wb.CreateName() as XSSFName; sheet2Name.NameName = "name1"; sheet2Name.SheetIndex = 1; sheet2Name.RefersToFormula = "Sheet2!$A$1"; Assert.IsTrue(wb.GetAllNames().Contains(sheet1Name)); Assert.IsTrue(wb.GetAllNames().Contains(sheet2Name)); Assert.AreEqual(2, wb.GetNames("name1").Count); Assert.AreEqual(sheet1Name, wb.GetNames("name1")[0]); Assert.AreEqual(sheet2Name, wb.GetNames("name1")[1]); // Remove sheet1, we should only have sheet2Name now wb.RemoveSheetAt(0); Assert.IsFalse(wb.GetAllNames().Contains(sheet1Name)); Assert.IsTrue(wb.GetAllNames().Contains(sheet2Name)); Assert.AreEqual(1, wb.GetNames("name1").Count); Assert.AreEqual(sheet2Name, wb.GetNames("name1")[0]); // Check by index as well for sanity Assert.AreEqual(1, wb.NumberOfNames); Assert.AreEqual(0, wb.GetNameIndex("name1")); Assert.AreEqual(sheet2Name, wb.GetNameAt(0)); wb.Close(); }
public void TestSetNameName() { // Test that renaming named ranges doesn't break our new named range map XSSFWorkbook wb = new XSSFWorkbook(); wb.CreateSheet("First Sheet"); // Two named ranges called "name1", one scoped to sheet1 and one globally XSSFName nameSheet1 = wb.CreateName() as XSSFName; nameSheet1.NameName = "name1"; nameSheet1.RefersToFormula = "'First Sheet'!$A$1"; nameSheet1.SheetIndex = 0; XSSFName nameGlobal = wb.CreateName() as XSSFName; nameGlobal.NameName = "name1"; nameGlobal.RefersToFormula = "'First Sheet'!$B$1"; // Rename sheet-scoped name to "name2", check everything is updated properly // and that the other name is unaffected nameSheet1.NameName = "name2"; Assert.AreEqual(1, wb.GetNames("name1").Count); Assert.AreEqual(1, wb.GetNames("name2").Count); Assert.AreEqual(nameGlobal, wb.GetName("name1")); Assert.AreEqual(nameSheet1, wb.GetName("name2")); // Rename the other name to "name" and check everything again nameGlobal.NameName = "name2"; Assert.AreEqual(0, wb.GetNames("name1").Count); Assert.AreEqual(2, wb.GetNames("name2").Count); Assert.IsTrue(wb.GetNames("name2").Contains(nameGlobal)); Assert.IsTrue(wb.GetNames("name2").Contains(nameSheet1)); wb.Close(); }