Class NameCollection

NameCollection class

Represents a collection of all the Name objects in the spreadsheet.

public class NameCollection : CollectionBase<Name>

Properties

NameDescription
Capacity { get; set; }
Count { get; }
Item { get; }Gets the Name element at the specified index. (2 indexers)
Item { get; set; }

Methods

NameDescription
Add(string)Defines a new name.
BinarySearch(Name)
BinarySearch(Name, IComparer<Name>)
BinarySearch(int, int, Name, IComparer<Name>)
Clear()Remove all defined names which are not referenced by the formulas and data source. If the defined name is referred, we only set Name.ReferTo as null and hide them. (2 methods)
Contains(Name)
CopyTo(Name[])
CopyTo(Name[], int)
CopyTo(int, Name[], int, int)
Exists(Predicate<Name>)
Filter(NameScopeType, int)Gets all defined name by scope.
Find(Predicate<Name>)
FindAll(Predicate<Name>)
FindIndex(Predicate<Name>)
FindIndex(int, Predicate<Name>)
FindIndex(int, int, Predicate<Name>)
FindLast(Predicate<Name>)
FindLastIndex(Predicate<Name>)
FindLastIndex(int, Predicate<Name>)
FindLastIndex(int, int, Predicate<Name>)
GetEnumerator()
IndexOf(Name)
IndexOf(Name, int)
IndexOf(Name, int, int)
LastIndexOf(Name)
LastIndexOf(Name, int)
LastIndexOf(Name, int, int)
Remove(string)Remove the name.
Remove(string[])Remove an array of name
RemoveAt(int)Remove the name at the specific index. (2 methods)
RemoveDuplicateNames()Remove the duplicate defined names
Sort()Sorts defined names.

Examples

[C#]

namespace Demos
{
    using Aspose.Cells;
    using System;

    public class NameCollectionDemo
    {
        public static void NameCollectionExample()
        {
            // Create a new workbook
            Workbook workbook = new Workbook();
            
            // Access the worksheet collection
            WorksheetCollection worksheets = workbook.Worksheets;
            
            // Access the name collection
            NameCollection names = worksheets.Names;
            
            // Add a new name to the collection
            int nameIndex = names.Add("MyNamedRange");
            Name name = names[nameIndex];
            
            // Set the refers to property for the name
            name.RefersTo = "=Sheet1!$A$1:$A$10";
            
            // Access and modify properties of the name
            name.Comment = "This is a named range for demonstration.";
            name.IsVisible = true;
            
            // Add another name
            int anotherNameIndex = names.Add("AnotherNamedRange");
            Name anotherName = names[anotherNameIndex];
            anotherName.RefersTo = "=Sheet1!$B$1:$B$10";
            
            // Remove a name by text
            names.Remove("AnotherNamedRange");
            
            // Remove a name by index
            names.RemoveAt(nameIndex);
            
            // Clear all names
            names.Clear();
            
            // Save the workbook
            workbook.Save("NameCollectionExample.xlsx");

            return;
        }
    }
}

See Also