Use Named Ranges
Contents
[
Hide
]
Normally, you use the labels of columns and rows on a worksheet to refer to the cells within those columns and rows. 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 to represent a cell, range of cells, formula, or constant value. 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 are among the most powerful features of Microsoft. Users may assign a name to a named range so that this range of cells can be referred with its name in the formulas. Aspose.Cells.GridDesktop does support this feature.
Adding/Referencing Named Ranges in Formulas
The GridDesktop control does support to import/export named ranges in the Excel files, it provides two classes (Name and NameCollection) to work with named ranges.
Following code snippet will help you how to use them.
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 | |
// Clear the Worsheets first | |
_grid.Clear(); | |
// The path to the documents directory. | |
string dataDir = Utils.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Specifying the path of Excel file using ImportExcelFile method of the control | |
_grid.ImportExcelFile(dataDir + "book1.xlsx"); | |
// Apply a formula to a cell that refers to a named range "Rang1" | |
_grid.Worksheets[0].Cells["G6"].SetCellValue("=SUM(Range1)"); | |
// Add a new named range "MyRange" with based area A2:B5 | |
int index = _grid.Names.Add("MyRange", "Sheet1!A2:B5"); | |
// Apply a formula to G7 cell | |
_grid.Worksheets[0].Cells["G7"].SetCellValue("=SUM(MyRange)"); | |
// Calculate the results of the formulas | |
_grid.RunAllFormulas(); | |
// Save the Excel file | |
_grid.ExportExcelFile(dataDir + @"ouputBook1_out.xlsx"); |