在工作簿中操作命名范围

可能的使用场景

Aspose.Cells支持对现有命名范围进行操作。可以从Workbook.GetWorksheets().GetNames()集合中访问所有现有的命名范围。一旦访问了命名范围,就可以更改它的各种方法,例如GetFullTextGetRefersTo

在工作簿中操作命名范围

以下示例代码读取了源Excel文件中的第一个命名范围,并在控制台上打印了它的FullTextRefersTo属性。之后,修改了RefersTo属性并保存了输出Excel文件

示例代码

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()

控制台输出

以下控制台输出打印了上述代码中现有命名范围FullTextRefersTo成员的值。

 Full Text: TestRange

Refers To: =Sheet1!$D$3:$G$6