Ejemplo n.º 1
0
 public ExternalName GetExternalName(String nameName, String sheetName, int externalWorkbookNumber)
 {
     if (externalWorkbookNumber > 0)
     {
         // External reference - reference is 1 based, link table is 0 based
         int linkNumber = externalWorkbookNumber - 1;
         ExternalLinksTable linkTable = _uBook.ExternalLinksTable[linkNumber];
         foreach (IName name in linkTable.DefinedNames)
         {
             if (name.NameName.Equals(nameName))
             {
                 // HSSF returns one sheet higher than normal, and various bits
                 //  of the code assume that. So, make us match that behaviour!
                 int nameSheetIndex = name.SheetIndex + 1;
                 // TODO Return a more specialised form of this, see bug #56752
                 // Should include the cached values, for in case that book isn't available
                 // Should support XSSF stuff lookups
                 return(new ExternalName(nameName, -1, nameSheetIndex));
             }
         }
         throw new ArgumentException("Name '" + nameName + "' not found in " +
                                     "reference to " + linkTable.LinkedFileName);
     }
     else
     {
         // Internal reference
         int nameIdx = _uBook.GetNameIndex(nameName);
         return(new ExternalName(nameName, nameIdx, 0));  // TODO Is this right?
     }
 }
Ejemplo n.º 2
0
        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();
        }
Ejemplo n.º 3
0
 public int GetNameIndex(string name)
 {
     return(XssfWorkbook.GetNameIndex(name));
 }