Add and Reference Named Ranges
Contents
[
Hide
]
Normally, column and row labels are used to uniquely refer to cells. But you can create descriptive names to represent cells, ranges of cells, formulas, or constant values. The word Name may refer to a string of characters that represents a cell, range of cells, formula, or constant value. For example, use easy-to-understand names, such as Products, to refer to hard to understand ranges, such as Sales!C20:C30. Labels can be used in formulas that refer to data on the same worksheet; if you want to represent a range on another worksheet, you may use a name. Named ranges is one of the most powerful features of Microsoft Excel. Users can assign a name to a range and use that name in formulas. Aspose.Cells.GridWeb supports this feature.
Adding/Referencing Named Ranges in Formulas
The GridWeb control provides two classes (GridName and GridNameCollection) for working with named ranges. The following code snippet will help you understand how to create the Named Range and access it in the formulas.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Inserting dummy data | |
GridWeb1.WorkSheets[0].Cells["B1"].PutValue(100); | |
GridWeb1.WorkSheets[0].Cells["B2"].PutValue(200); | |
GridWeb1.WorkSheets[0].Cells["B3"].PutValue(300); | |
GridWeb1.WorkSheets[0].Cells["B4"].PutValue(400); | |
// Add a new named range "MyRange" with based area B1:B4 | |
GridWeb1.WorkSheets.Names.Add("MyRange", "Sheet1!B1:B4"); | |
// Apply a formula to a cell that refers to a named range "MyRange" | |
GridWeb1.WorkSheets[0].Cells["A1"].Formula = "=SUM(MyRange)"; | |
// Apply a formula to A2 cell | |
GridWeb1.WorkSheets[0].Cells["A2"].Formula = "=Average(MyRange)"; | |
// Calculate the results of the formulas | |
GridWeb1.WorkSheets.CalculateFormula(); |