Manipulate Named Range in a Workbook
Possible Usage Scenarios
Aspose.Cells supports the manipulation of existing named ranges. All the existing named ranges can be accessed from Workbook.GetWorksheets().GetNames() collection. Once, you access the named range, you can change its various methods e.g. GetFullText and GetRefersTo.
Manipulate Named Range in a Workbook
The following sample code reads the first named-range inside the source excel file and prints its FullText and RefersTo properties on the console. After that, it modifies RefersTo
property and saves the output excel file.
Sample Code
Aspose::Cells::Startup(); | |
//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C | |
//Path of input | |
U16String dirPath(u""); | |
//Path of output | |
U16String outPath(u""); | |
//Path of input excel file | |
U16String sampleManipulateNamedRangeInWorkbook = dirPath + u"sampleManipulateNamedRangeInWorkbook.xlsx"; | |
//Path of output excel file | |
U16String outputManipulateNamedRangeInWorkbook = outPath + u"outputManipulateNamedRangeInWorkbook.xlsx"; | |
//Create a workbook | |
Workbook wb(sampleManipulateNamedRangeInWorkbook); | |
//Read the named range created above from names collection | |
Name nm = wb.GetWorksheets().GetNames().Get(0); | |
//Print its FullText and RefersTo members | |
std::cout << "Full Text : " << nm.GetFullText().ToUtf8() << std::endl; | |
std::cout << "Refers To : " << nm.GetRefersTo().ToUtf8() << std::endl; | |
//Manipulate the RefersTo property of NamedRange | |
nm.SetRefersTo(u"=Sheet1!$D$5:$J$10"); | |
//Save the workbook in xlsx format | |
wb.Save(outputManipulateNamedRangeInWorkbook, SaveFormat::Xlsx); | |
Aspose::Cells::Cleanup() |
Console Output
The following console output prints the values of FullText and RefersTo members of the existing Named Range in the above code.
Full Text: TestRange
Refers To: =Sheet1!$D$3:$G$6