Delete Named Ranges with C++
Introduction
If there are too many defined names or named ranges in the Excel files, we have to clear some for they are not referred again.
Remove Named Range in MS Excel
To remove a named range from Excel, you can follow these steps:
- Open Microsoft Excel and open the workbook that contains the named range.
- Go to the “Formulas” tab in the Excel ribbon.
- Click on the “Name Manager” button in the “Defined Names” group. This will open the Name Manager dialog box.
- In the Name Manager dialog box, select the named range you want to remove.
- Click on the “Delete” button. Confirm the deletion when prompted.
- Click on the “Close” button to close the Name Manager dialog box.
- Save the workbook to retain the changes.
Deletes Named Range using Aspose.Cells for C++
With Aspose.Cells for C++, you can remove named ranges or defined names by text or index in the list.
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// Source directory path
U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
// Output directory path
U16String outDir(u"..\\Data\\02_OutputDirectory\\");
// Path of input Excel file
U16String inputFilePath = srcDir + u"Book1.xlsx";
// Path of output Excel file
U16String outputFilePath = outDir + u"Book2.xlsx";
// Instantiate a new Workbook
Workbook workbook(inputFilePath);
// Get all the worksheets in the book
WorksheetCollection worksheets = workbook.GetWorksheets();
// Delete a named range by text
worksheets.GetNames().Remove(u"NamedRange");
// Delete a defined name by index
worksheets.GetNames().RemoveAt(0);
// Save the workbook to retain the changes
workbook.Save(outputFilePath);
std::cout << "Named ranges removed successfully!" << std::endl;
Aspose::Cells::Cleanup();
}
Note: if the defined name is referred by formulas, it could not be removed. We only can remove the formula of the defined name.
Removes Some Named Ranges
When we remove a defined name, we have to check whether it is referred by all formulas in the file. In order to improve the performance of removing named ranges, we can remove some together.
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// Source directory path
U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
// Output directory path
U16String outDir(u"..\\Data\\02_OutputDirectory\\");
// Path of input excel file
U16String inputFilePath = srcDir + u"Book1.xlsx";
// Path of output excel file
U16String outputFilePath = outDir + u"Book2.xlsx";
// Instantiate a new Workbook
Workbook workbook(inputFilePath);
// Get all the worksheets in the book
WorksheetCollection worksheets = workbook.GetWorksheets();
// Delete some defined names
Vector<U16String> namesToRemove = { u"NamedRange1", u"NamedRange2" };
worksheets.GetNames().Remove(namesToRemove);
// Save the workbook to retain the changes
workbook.Save(outputFilePath);
std::cout << "Named ranges removed successfully!" << std::endl;
Aspose::Cells::Cleanup();
}
Remove Duplicate Defined Names
Some Excel files corrupt because some defined names are duplicate. So we can remove these duplicate names to repair the file.
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// Source directory path
U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
// Output directory path
U16String outDir(u"..\\Data\\02_OutputDirectory\\");
// Path of input Excel file
U16String inputFilePath = srcDir + u"Book1.xlsx";
// Path of output Excel file
U16String outputFilePath = outDir + u"Book2.xlsx";
// Instantiate a new Workbook
Workbook workbook(inputFilePath);
// Get all the worksheets in the book
WorksheetCollection worksheets = workbook.GetWorksheets();
// Delete some defined names
worksheets.GetNames().RemoveDuplicateNames();
// Save the workbook to retain the changes
workbook.Save(outputFilePath);
std::cout << "Workbook saved successfully after removing duplicate names!" << std::endl;
Aspose::Cells::Cleanup();
}