Create Workbook and Worksheet Scoped Named Ranges
Microsoft Excel allows users to define named ranges with two different scopes: workbook (also known as global scope) and worksheet.
- Named ranges with a workbook scope can be accessed from any worksheet within that workbook by simply using its name.
- Worksheet scoped named ranges are accessed with the reference of the particular worksheet in which it was created.
Aspose.Cells provides the same functionality as Microsoft Excel for adding workbook and worksheet scoped named ranges. When creating a worksheet scoped named range, the worksheet reference should be used in the named range to specify it as a worksheet scoped named range.
Adding a Named Range with Workbook Scoped
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Create a new Workbook object | |
Workbook workbook = new Workbook(); | |
// Get first worksheet of the workbook | |
Worksheet sheet = workbook.Worksheets[0]; | |
// Get worksheet's cells collection | |
Cells cells = sheet.Cells; | |
// Create a range of Cells from Cell A1 to C10 | |
Range workbookScope = cells.CreateRange("A1", "C10"); | |
// Assign the nsame to workbook scope named range | |
workbookScope.Name = "workbookScope"; | |
// Save the workbook | |
workbook.Save(dataDir+ "WorkbookScope.out.xlsx"); |
Adding a Named Range with Worksheet Scope
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Create a new Workbook object | |
Workbook workbook = new Workbook(); | |
// Get first worksheet of the workbook | |
Worksheet sheet = workbook.Worksheets[0]; | |
// Get worksheet's cells collection | |
Cells cells = sheet.Cells; | |
// Create a range of Cells | |
Range localRange = cells.CreateRange("A1", "C10"); | |
// Assign name to range with sheet raference | |
localRange.Name = "Sheet1!local"; | |
// Save the workbook | |
workbook.Save(dataDir+ "ouput.out.xls"); |